Skip to content
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

add commit hashes to git sources #123

Merged
merged 6 commits into from
Jun 28, 2023
Merged

Conversation

Adhalianna
Copy link

This would close the issue #122 . The suggested approach to serialization of git sources might seem over-engineered but my hope while working on it was to keep it all backwards compatible and to establish a good way of adding more of similar source parameters if needed.

At this very moment I am somewhat sleep deprived so I will probably get back here later to try and explain more if needed.

@Shnatsel
Copy link
Member

Thank you! I will be traveling for the next three days, so I will review this on the 27th. Is that OK?

@Adhalianna
Copy link
Author

I have fixed one thing that slipped from my previous commit. I worry however that this might not be enough to pass all the checks run by GithHub. I have previously on my own machine noticed that the test runs seemed to be nondeterministic (failure at cargo clean) when I was starting with them. I can no longer reproduce that now that I have found some time to sit at it again.

Comment on lines -74 to -98
"Source": {
"description": "Serializes to \"git\", \"local\", \"crates.io\" or \"registry\". Designed to be extensible with other revision control systems, etc.",
"oneOf": [
{
"type": "string",
"enum": [
"CratesIo",
"Git",
"Local",
"Registry"
]
},
{
"type": "object",
"required": [
"Other"
],
"properties": {
"Other": {
"type": "string"
}
},
"additionalProperties": false
}
]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not obvious at first glance but the new schema should be backwards compatible and as far as I am aware even more correct as the values for enum keyword used here are not exactly right (e.g. de-serialization expects crates.io rather than CratesIo).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the schema should be re-generated.

The mismatch between the documentation and the generated schema is a bug, and will need to be fixed, but that doesn't have to be done in this PR.

Comment on lines +81 to +88
{
"description": "\"local\"",
"type": "string"
},
{
"description": "\"registry\"",
"type": "string"
},
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Providing specific accepted values using proper keywords turned out to be too difficult and probably not worth the complexity because of how schemars operates. This is not my first issue with schemars and the crate is not very actively maintained so I must admit I did not even try to formulate the exact problem that manifested here into a bug report on their repository.

Comment on lines +162 to +167
#[serde(with = "compact_enum_variant")]
#[cfg_attr(
feature = "schema",
schemars(schema_with = "compact_enum_variant::schema::<Source, GitSource>",)
)]
Git(GitSource),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This specific fragment changes the Git variant to use the representation specified in the compact_enum_variant.rs file. The attributes did became a little bit complex because of that but besides most of the complexity should be hidden away in that file and in future a similar trick can be used to extend any other enum variant while keeping it all backwards compatible with the way the unit variants are serialized.

That is, a change from:

#[derive(Serialize, Deserialize)]
enum MyEnum{
 V1,
}

to:

#[derive(Serialize, Deserialize)]
#[serde(untagged)]
enum MyEnum {
 #[serde(with = "compact_enum_variant")]
 V1(V1Struct),
}

/// And some impls ... {}

Should be backwards compatible.

Comment on lines 184 to 195
impl From<Source> for String {
/// Provides a lossy conversion for variants with values different than the default
fn from(s: Source) -> String {
match s {
Source::CratesIo => "crates.io".to_owned(),
Source::Git => "git".to_owned(),
Source::Git(_) => "git".to_owned(),
Source::Local => "local".to_owned(),
Source::Registry => "registry".to_owned(),
Source::Other(string) => string,
}
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation along with From<&str> for Source make me feel the most conflicted. They are a part of publicly exposed API so changing their behavior could become problematic. For some reason I feel like I shouldn't just use serde_json and parse/serialize the value for git variant but I am not sure if the feeling is justified.

@Shnatsel
Copy link
Member

Thank you for the PR! I will take a more thorough look in the next few days. Do you have a deadline for this PR to be merged?

@Adhalianna
Copy link
Author

Adhalianna commented Jun 27, 2023 via email

@Shnatsel
Copy link
Member

I see. The timing of my trip was really inconvenient. I'll get this reviewed and merged later tonight then.

@Adhalianna
Copy link
Author

Adhalianna commented Jun 27, 2023 via email

//! ```
//!
//! Changing a unit variant of an enum to wrap a type and use this module for
//! the serialization can be made to be a backwards compatible change.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice documentation!

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum VariantRepr<S: Into<String>, ENUM, INNER> {
// Short stringly-typed representation of the enum variant - a label -
// which assumes all fields are set to their defualts.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// which assumes all fields are set to their defualts.
// which assumes all fields are set to their defaults.

Typo


#[test]
#[should_panic]
fn fail_deserializing_invalid_git_source_variant() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to see test coverage for both success and failure cases!

@Shnatsel Shnatsel merged commit 13dd25d into rust-secure-code:master Jun 28, 2023
4 checks passed
@Shnatsel
Copy link
Member

I might investigate later whether I want to keep around the untagged enum magic. If there is a way to alter the format to keep the deserialization simple and stupid, it might be worth changing the spec to accommodate that. But this is a nice, general, united-tested solution within the confines of the current spec.

Great work, thank you!

@Adhalianna
Copy link
Author

Adhalianna commented Jun 28, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants