-
Notifications
You must be signed in to change notification settings - Fork 229
add docs_rs_crates_io subcrate for interaction / shared types #3357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
syphar
wants to merge
4
commits into
rust-lang:main
Choose a base branch
from
syphar:event-structs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| [package] | ||
| name = "docs_rs_crates_io" | ||
| version = "0.1.0" | ||
| description = "types & logic for the direct integration between docs.rs & crates.io" | ||
|
|
||
| authors.workspace = true | ||
| license.workspace = true | ||
| repository.workspace = true | ||
| edition.workspace = true | ||
|
|
||
| [dependencies] | ||
| chrono = { version = "0.4", features = ["serde"] } | ||
| serde = { version = "1", features = ["derive"] } | ||
|
|
||
| [dev-dependencies] | ||
| serde_json = "1.0" | ||
|
|
||
| [lints] | ||
| workspace = true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| use chrono::{DateTime, Utc}; | ||
| use std::fmt; | ||
|
|
||
| /// A change that can happen to a crate on our index. | ||
| #[derive(Clone, serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug)] | ||
| #[serde(tag = "type", content = "payload", rename_all = "snake_case")] | ||
| pub enum IndexChangeV1 { | ||
| /// A crate version was added. | ||
| Added(CrateVersion), | ||
| /// A crate version was unyanked. | ||
| Unyanked(CrateVersion), | ||
| /// A crate version was yanked. | ||
| Yanked(CrateVersion), | ||
| /// The name of the crate whose file was deleted, which implies all versions were deleted as well. | ||
| CrateDeleted { name: String }, | ||
| /// A crate version was deleted. | ||
| VersionDeleted(CrateVersion), | ||
| } | ||
|
|
||
| impl IndexChangeV1 { | ||
| /// Return the added crate, if this is this kind of change. | ||
| pub fn added(&self) -> Option<&CrateVersion> { | ||
| match self { | ||
| IndexChangeV1::Added(v) => Some(v), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| /// Return the yanked crate, if this is this kind of change. | ||
| pub fn yanked(&self) -> Option<&CrateVersion> { | ||
| match self { | ||
| IndexChangeV1::Yanked(v) => Some(v), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| /// Return the unyanked crate, if this is this kind of change. | ||
| pub fn unyanked(&self) -> Option<&CrateVersion> { | ||
| match self { | ||
| IndexChangeV1::Unyanked(v) => Some(v), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| /// Return the deleted crate, if this is this kind of change. | ||
| pub fn crate_deleted(&self) -> Option<&str> { | ||
| match self { | ||
| IndexChangeV1::CrateDeleted { name } => Some(name.as_str()), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| /// Return the deleted version crate, if this is this kind of change. | ||
| pub fn version_deleted(&self) -> Option<&CrateVersion> { | ||
| match self { | ||
| IndexChangeV1::VersionDeleted(v) => Some(v), | ||
| _ => None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for IndexChangeV1 { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| f.write_str(match *self { | ||
| IndexChangeV1::Added(_) => "added", | ||
| IndexChangeV1::Yanked(_) => "yanked", | ||
| IndexChangeV1::CrateDeleted { .. } => "crate deleted", | ||
| IndexChangeV1::VersionDeleted(_) => "version deleted", | ||
| IndexChangeV1::Unyanked(_) => "unyanked", | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// A conventional event envelope for our events between crates.io & docs.rs | ||
| #[derive(Clone, serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug)] | ||
| pub struct Event<T> { | ||
| /// Unique event identifier for deduplication and tracing. | ||
| pub id: String, | ||
| /// Timestamp when the event occured | ||
| pub occurred_at: DateTime<Utc>, | ||
| /// The typed payload. | ||
| #[serde(flatten)] | ||
| pub change: T, | ||
| } | ||
|
|
||
| /// The first version of the public event wire format. | ||
| pub type IndexChangeEventV1 = Event<IndexChangeV1>; | ||
|
|
||
| /// Pack all information we know about a change made to a version of a crate. | ||
| #[derive(Clone, serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug)] | ||
| pub struct CrateVersion { | ||
| /// The crate name, i.e. `clap`. | ||
| pub name: String, | ||
| /// The semantic version of the crate. | ||
| #[serde(rename = "vers")] | ||
| pub version: String, | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use serde_json::json; | ||
|
|
||
| fn crate_version() -> CrateVersion { | ||
| CrateVersion { | ||
| name: "clap".into(), | ||
| version: "4.5.0".into(), | ||
| } | ||
| } | ||
|
|
||
| fn event(change: IndexChangeV1) -> IndexChangeEventV1 { | ||
| IndexChangeEventV1 { | ||
| id: "evt_123".into(), | ||
| occurred_at: DateTime::parse_from_rfc3339("2026-05-22T12:34:56Z") | ||
| .unwrap() | ||
| .with_timezone(&Utc), | ||
| change, | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn crate_version_serializes_with_vers_field() { | ||
| let event = crate_version(); | ||
|
|
||
| assert_eq!( | ||
| serde_json::to_value(&event).unwrap(), | ||
| json!({ | ||
| "name": "clap", | ||
| "vers": "4.5.0", | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn change_serializes_with_expected_variant_shapes() { | ||
| let crate_version = crate_version(); | ||
|
|
||
| let cases = [ | ||
| ( | ||
| IndexChangeV1::Added(crate_version.clone()), | ||
| json!({ | ||
| "type": "added", | ||
| "payload": { | ||
| "name": "clap", | ||
| "vers": "4.5.0", | ||
| } | ||
| }), | ||
| ), | ||
| ( | ||
| IndexChangeV1::Unyanked(crate_version.clone()), | ||
| json!({ | ||
| "type": "unyanked", | ||
| "payload": { | ||
| "name": "clap", | ||
| "vers": "4.5.0", | ||
| } | ||
| }), | ||
| ), | ||
| ( | ||
| IndexChangeV1::Yanked(crate_version.clone()), | ||
| json!({ | ||
| "type": "yanked", | ||
| "payload": { | ||
| "name": "clap", | ||
| "vers": "4.5.0", | ||
| } | ||
| }), | ||
| ), | ||
| ( | ||
| IndexChangeV1::CrateDeleted { | ||
| name: "old-crate".into(), | ||
| }, | ||
| json!({ | ||
| "type": "crate_deleted", | ||
| "payload": { | ||
| "name": "old-crate" | ||
| } | ||
| }), | ||
| ), | ||
| ( | ||
| IndexChangeV1::VersionDeleted(crate_version), | ||
| json!({ | ||
| "type": "version_deleted", | ||
| "payload": { | ||
| "name": "clap", | ||
| "vers": "4.5.0", | ||
| } | ||
| }), | ||
| ), | ||
| ]; | ||
|
|
||
| for (event, expected) in cases { | ||
| assert_eq!(serde_json::to_value(&event).unwrap(), expected); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn event_serializes_with_minimum_metadata() { | ||
| let event = event(IndexChangeV1::CrateDeleted { | ||
| name: "old-crate".into(), | ||
| }); | ||
|
|
||
| assert_eq!( | ||
| serde_json::to_value(&event).unwrap(), | ||
| json!({ | ||
| "id": "evt_123", | ||
| "occurred_at": "2026-05-22T12:34:56Z", | ||
| "type": "crate_deleted", | ||
| "payload": { | ||
| "name": "old-crate" | ||
| } | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn event_deserializes_rfc3339_occurred_at() { | ||
| let event: IndexChangeEventV1 = serde_json::from_value(json!({ | ||
| "id": "evt_123", | ||
| "occurred_at": "2026-05-22T12:34:56Z", | ||
| "type": "crate_deleted", | ||
| "payload": { | ||
| "name": "old-crate" | ||
| } | ||
| })) | ||
| .unwrap(); | ||
|
|
||
| assert_eq!( | ||
| event.occurred_at, | ||
| DateTime::parse_from_rfc3339("2026-05-22T12:34:56Z") | ||
| .unwrap() | ||
| .with_timezone(&Utc) | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pub mod events; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.