-
Notifications
You must be signed in to change notification settings - Fork 19
Using generated code for manifest serializing/deserializing instead of custom parsers #276
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
P-E-Meunier
wants to merge
1
commit into
helsing-ai:main
Choose a base branch
from
P-E-Meunier:main
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
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 |
|---|---|---|
|
|
@@ -38,7 +38,7 @@ pub const CANARY_EDITION: &str = concat!("0.", env!("CARGO_PKG_VERSION_MINOR")); | |
|
|
||
| /// Edition of the buffrs manifest | ||
| #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] | ||
| #[serde(into = "&str", from = "&str")] | ||
| #[serde(into = "Cow<str>", from = "Cow<str>")] | ||
| pub enum Edition { | ||
| /// The canary edition of manifests | ||
| /// | ||
|
|
@@ -64,9 +64,22 @@ impl Edition { | |
| } | ||
| } | ||
|
|
||
| impl From<&str> for Edition { | ||
| fn from(value: &str) -> Self { | ||
| match value { | ||
| use std::borrow::Cow; | ||
|
|
||
| impl<'a> From<&'a str> for Edition { | ||
| fn from(value: &'a str) -> Self { | ||
| match &*value { | ||
| self::CANARY_EDITION => Self::Canary, | ||
| "0.8" => Self::Canary08, | ||
| "0.7" => Self::Canary07, | ||
| _ => Self::Unknown, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<'a> From<Cow<'a, str>> for Edition { | ||
| fn from(value: Cow<'a, str>) -> Self { | ||
| match &*value { | ||
| self::CANARY_EDITION => Self::Canary, | ||
| "0.8" => Self::Canary08, | ||
| "0.7" => Self::Canary07, | ||
|
|
@@ -86,13 +99,26 @@ impl From<Edition> for &'static str { | |
| } | ||
| } | ||
|
|
||
| impl From<Edition> for Cow<'static, str> { | ||
| fn from(value: Edition) -> Self { | ||
| Cow::Borrowed(match value { | ||
| Edition::Canary => CANARY_EDITION, | ||
| Edition::Canary08 => "0.8", | ||
| Edition::Canary07 => "0.7", | ||
| Edition::Unknown => "unknown", | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// A buffrs manifest format used for serialization and deserialization. | ||
| /// | ||
| /// This contains the exact structure of the `Proto.toml` and skips | ||
| /// empty fields. | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
| #[serde(untagged)] | ||
| enum RawManifest { | ||
| Canary { | ||
| edition: Edition, | ||
| package: Option<PackageManifest>, | ||
| dependencies: DependencyMap, | ||
| }, | ||
|
|
@@ -125,105 +151,44 @@ impl RawManifest { | |
| } | ||
| } | ||
|
|
||
| mod serializer { | ||
| use super::*; | ||
| use serde::{ser::SerializeStruct, Serializer}; | ||
|
|
||
| impl Serialize for RawManifest { | ||
| fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
| where | ||
| S: Serializer, | ||
| { | ||
| match *self { | ||
| RawManifest::Canary { | ||
| ref package, | ||
| ref dependencies, | ||
| } => { | ||
| let mut s = serializer.serialize_struct("Canary", 3)?; | ||
| s.serialize_field("edition", CANARY_EDITION)?; | ||
| s.serialize_field("package", package)?; | ||
| s.serialize_field("dependencies", dependencies)?; | ||
| s.end() | ||
| } | ||
| RawManifest::Unknown { | ||
| ref package, | ||
| ref dependencies, | ||
| } => { | ||
| let mut s = serializer.serialize_struct("Unknown", 2)?; | ||
| s.serialize_field("package", package)?; | ||
| s.serialize_field("dependencies", dependencies)?; | ||
| s.end() | ||
| } | ||
| } | ||
| #[test] | ||
| fn manifest() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please add tests that:
Also (nit) could you group them in a test mod? |
||
| let mut dependencies = HashMap::new(); | ||
| dependencies.insert( | ||
| PackageName::new("foo".to_string()).unwrap(), | ||
| LocalDependencyManifest { | ||
| path: Path::new("/foo/bar").to_path_buf(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| mod deserializer { | ||
| use serde::{ | ||
| de::{self, MapAccess, Visitor}, | ||
| Deserializer, | ||
| .into(), | ||
| ); | ||
| let man = RawManifest::Canary { | ||
| edition: Edition::from(CANARY_EDITION), | ||
| package: None, | ||
| dependencies, | ||
| }; | ||
| let x = toml::to_string(&man).unwrap(); | ||
| assert_eq!( | ||
| x, | ||
| format!( | ||
| "edition = \"{}\"\n\n[dependencies.foo]\npath = \"/foo/bar\"\n", | ||
| CANARY_EDITION | ||
| ) | ||
| ); | ||
|
|
||
| use super::*; | ||
|
|
||
| impl<'de> Deserialize<'de> for RawManifest { | ||
| fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
| where | ||
| D: Deserializer<'de>, | ||
| { | ||
| static FIELDS: &[&str] = &["package", "dependencies"]; | ||
|
|
||
| struct ManifestVisitor; | ||
|
|
||
| impl<'de> Visitor<'de> for ManifestVisitor { | ||
| type Value = RawManifest; | ||
|
|
||
| fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
| formatter.write_str("a buffrs manifest (`Proto.toml`)") | ||
| } | ||
|
|
||
| fn visit_map<V>(self, mut map: V) -> Result<RawManifest, V::Error> | ||
| where | ||
| V: MapAccess<'de>, | ||
| { | ||
| let mut edition: Option<String> = None; | ||
| let mut package: Option<PackageManifest> = None; | ||
| let mut dependencies: Option<HashMap<PackageName, DependencyManifest>> = None; | ||
|
|
||
| while let Some(key) = map.next_key::<String>()? { | ||
| match key.as_str() { | ||
| "package" => package = Some(map.next_value()?), | ||
| "dependencies" => dependencies = Some(map.next_value()?), | ||
| "edition" => edition = Some(map.next_value()?), | ||
| _ => return Err(de::Error::unknown_field(&key, FIELDS)), | ||
| } | ||
| } | ||
|
|
||
| let dependencies = dependencies.unwrap_or_default(); | ||
|
|
||
| let Some(edition) = edition else { | ||
| return Ok(RawManifest::Unknown { | ||
| package, | ||
| dependencies, | ||
| }); | ||
| }; | ||
|
|
||
| match Edition::from(edition.as_str()) { | ||
| Edition::Canary | Edition::Canary08 | Edition::Canary07 => Ok(RawManifest::Canary { | ||
| package, | ||
| dependencies, | ||
| }), | ||
| Edition::Unknown => Err(de::Error::custom( | ||
| format!("unsupported manifest edition, supported editions of {} are: {CANARY_EDITION}", env!("CARGO_PKG_VERSION")) | ||
| )), | ||
| } | ||
| } | ||
| } | ||
| assert_eq!(toml::from_str::<RawManifest>(&x).unwrap(), man); | ||
| } | ||
|
|
||
| deserializer.deserialize_map(ManifestVisitor) | ||
| } | ||
| #[test] | ||
| fn canary() { | ||
| let ed = Edition::from(CANARY_EDITION); | ||
| #[derive(Debug, PartialEq, Serialize, Deserialize)] | ||
| struct Ed { | ||
| ed: Edition, | ||
| } | ||
| let ed = Ed { ed }; | ||
| let x = toml::to_string(&ed).unwrap(); | ||
| assert_eq!(x, format!("ed = \"{}\"\n", CANARY_EDITION)); | ||
| assert_eq!(toml::from_str::<Ed>(&x).unwrap(), ed); | ||
| } | ||
|
|
||
| impl From<Manifest> for RawManifest { | ||
|
|
@@ -236,6 +201,7 @@ impl From<Manifest> for RawManifest { | |
|
|
||
| match manifest.edition { | ||
| Edition::Canary | Edition::Canary08 | Edition::Canary07 => RawManifest::Canary { | ||
| edition: manifest.edition, | ||
| package: manifest.package, | ||
| dependencies, | ||
| }, | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will most likely break stuff.
The old deserialization code mapped multiple different versions to the
RawManifest::Canaryformat. In the future this is great to maintain backwards compat: Ie. you can introduce a new format of theRawManifest(e.g.V1) and determine into which one to deserialize by looking at the edition field first.This MR lost that capability as far as i can tell: You deserialize everything that has an edition into
Canaryand everything without intoUnknown(which is indeed the current behaviour, but rules out easy adoption of breaking manifest changes). If you now add the third variant to your code, you are not inspecting the edition format pre decoding.One option i could see to recover this (if you feel strongly about not having manual deserialization in place) is to add newtypes around
Editionwhich represents the subsets supported by oneRawManifestvariant.