Repository navigation
feat: add Zod v4 output format #96
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Choice = text / int | ||
| Mode = "decline" / "dedicated-worker" / "-0" | ||
| Events = [+ text] | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -46,6 +46,12 @@ struct PostambleOptions { | |||||
| print_flatten: bool, | ||||||
| } | ||||||
|
|
||||||
| #[derive(Debug, Copy, Clone, PartialEq, Eq)] | ||||||
| enum Version { | ||||||
| V3, | ||||||
| V4, | ||||||
| } | ||||||
|
|
||||||
|
Comment on lines
+49
to
+54
Collaborator
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. Nit: Rather than keeping |
||||||
| pub struct Engine<Stdout, Stderr> | ||||||
| where | ||||||
| Stdout: Write, | ||||||
|
|
@@ -54,6 +60,7 @@ where | |||||
| nested_group_choices: Vec<GroupChoiceContext>, | ||||||
| nested_type1: Vec<Type1Context>, | ||||||
| is_type_mode: bool, | ||||||
| version: Version, | ||||||
| cyclic_rules: std::collections::HashSet<String>, | ||||||
| #[allow(dead_code)] | ||||||
| postamble_options: PostambleOptions, | ||||||
|
|
@@ -78,10 +85,23 @@ fn calculate_occurrence(occur: &Option<cddl::ast::Occurrence<'_>>) -> (usize, us | |||||
|
|
||||||
| impl<'a, 'b: 'a, 'c, Stdout: Write, Stderr: Write> Engine<Stdout, Stderr> { | ||||||
| pub fn with_writers(stdout: Stdout, stderr: Stderr) -> Engine<Stdout, Stderr> { | ||||||
| Self::with_writers_for_version(stdout, stderr, Version::V3) | ||||||
| } | ||||||
|
|
||||||
| pub fn with_writers_zod4(stdout: Stdout, stderr: Stderr) -> Engine<Stdout, Stderr> { | ||||||
| Self::with_writers_for_version(stdout, stderr, Version::V4) | ||||||
| } | ||||||
|
|
||||||
| fn with_writers_for_version( | ||||||
| stdout: Stdout, | ||||||
| stderr: Stderr, | ||||||
| version: Version, | ||||||
| ) -> Engine<Stdout, Stderr> { | ||||||
| Engine { | ||||||
| nested_group_choices: Vec::new(), | ||||||
| nested_type1: Vec::new(), | ||||||
| is_type_mode: false, | ||||||
| version, | ||||||
| cyclic_rules: std::collections::HashSet::new(), | ||||||
| postamble_options: PostambleOptions { | ||||||
| #[cfg(feature = "vector_groups")] | ||||||
|
|
@@ -104,6 +124,13 @@ impl<'a, 'b: 'a, 'c, Stdout: Write, Stderr: Write> Engine<Stdout, Stderr> { | |||||
| unimplemented!(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fn print_union_type_start(&mut self) { | ||||||
| match self.version { | ||||||
| Version::V3 => write!(self.stdout, "z.ZodUnion<["), | ||||||
| Version::V4 => write!(self.stdout, "z.ZodUnion<readonly ["), | ||||||
| }; | ||||||
| } | ||||||
| fn visit_maybe_enum_type(&mut self, t: &'b cddl::ast::Type<'a>) -> bool { | ||||||
| // Special case for string enums | ||||||
| if t.type_choices.len() > 1 | ||||||
|
|
@@ -113,13 +140,26 @@ impl<'a, 'b: 'a, 'c, Stdout: Write, Stderr: Write> Engine<Stdout, Stderr> { | |||||
| .all(|type2| matches!(type2, cddl::ast::Type2::TextValue { .. })) | ||||||
| { | ||||||
| if self.is_type_mode { | ||||||
| write!(self.stdout, "z.ZodEnum<["); | ||||||
| for type2 in t.type_choices.iter().map(|choice| &choice.type1.type2) { | ||||||
| if let cddl::ast::Type2::TextValue { value, .. } = type2 { | ||||||
| write!(self.stdout, "\"{}\",", value); | ||||||
| match self.version { | ||||||
| Version::V3 => { | ||||||
| write!(self.stdout, "z.ZodEnum<["); | ||||||
| for type2 in t.type_choices.iter().map(|choice| &choice.type1.type2) { | ||||||
| if let cddl::ast::Type2::TextValue { value, .. } = type2 { | ||||||
| write!(self.stdout, "\"{}\",", value); | ||||||
| } | ||||||
| } | ||||||
| write!(self.stdout, "]>"); | ||||||
| } | ||||||
| Version::V4 => { | ||||||
| write!(self.stdout, "z.ZodEnum<{{"); | ||||||
| for type2 in t.type_choices.iter().map(|choice| &choice.type1.type2) { | ||||||
| if let cddl::ast::Type2::TextValue { value, .. } = type2 { | ||||||
| write!(self.stdout, "\"{}\":\"{}\";", value, value); | ||||||
|
Collaborator
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.
Suggested change
Nit: Use |
||||||
| } | ||||||
| } | ||||||
| write!(self.stdout, "}}>"); | ||||||
| } | ||||||
| } | ||||||
| write!(self.stdout, "]>"); | ||||||
| } else { | ||||||
| write!(self.stdout, "z.enum(["); | ||||||
| for type2 in t.type_choices.iter().map(|choice| &choice.type1.type2) { | ||||||
|
|
@@ -137,7 +177,7 @@ impl<'a, 'b: 'a, 'c, Stdout: Write, Stderr: Write> Engine<Stdout, Stderr> { | |||||
| fn visit_array(&mut self, g: &'b cddl::ast::Group<'a>) -> cddl::visitor::Result<Error> { | ||||||
| if g.group_choices.len() != 1 { | ||||||
| if self.is_type_mode { | ||||||
| write!(self.stdout, "z.ZodUnion<["); | ||||||
| self.print_union_type_start(); | ||||||
| } else { | ||||||
| write!(self.stdout, "z.union(["); | ||||||
| } | ||||||
|
|
@@ -369,7 +409,7 @@ impl<'a, 'b: 'a, 'c, Stdout: Write, Stderr: Write> Engine<Stdout, Stderr> { | |||||
| } | ||||||
| if upper < MAX_ARRAYS { | ||||||
| if self.is_type_mode { | ||||||
| write!(self.stdout, "z.ZodUnion<["); | ||||||
| self.print_union_type_start(); | ||||||
| for bound in lower..upper + 1 { | ||||||
| if bound != 0 { | ||||||
| write!(self.stdout, ","); | ||||||
|
|
@@ -404,7 +444,7 @@ impl<'a, 'b: 'a, 'c, Stdout: Write, Stderr: Write> Engine<Stdout, Stderr> { | |||||
| } else if self.is_type_mode { | ||||||
| write!(self.stdout, "z.ZodArray<"); | ||||||
| self.visit_type(&entry.entry_type)?; | ||||||
| if lower == 1 { | ||||||
| if lower == 1 && self.version == Version::V3 { | ||||||
| write!(self.stdout, ", \"atleastone\""); | ||||||
| } | ||||||
| write!(self.stdout, ">"); | ||||||
|
|
@@ -597,7 +637,7 @@ impl<'a, 'b: 'a, Stdout: Write, Stderr: Write> Visitor<'a, 'b, Error> for Engine | |||||
| } | ||||||
| if t.type_choices.len() != 1 { | ||||||
| if self.is_type_mode { | ||||||
| write!(self.stdout, "z.ZodUnion<["); | ||||||
| self.print_union_type_start(); | ||||||
| } else { | ||||||
| write!(self.stdout, "z.union(["); | ||||||
| } | ||||||
|
|
@@ -688,7 +728,7 @@ impl<'a, 'b: 'a, Stdout: Write, Stderr: Write> Visitor<'a, 'b, Error> for Engine | |||||
| self.print_group_joiner(); | ||||||
| if matches!(calculate_occurrence(&ge.occur), (0, max) if max > 0) { | ||||||
| if self.is_type_mode { | ||||||
| write!(self.stdout, "z.ZodUnion<["); | ||||||
| self.print_union_type_start(); | ||||||
| self.visit_type_groupname_entry(ge)?; | ||||||
| write!(self.stdout, ",z.ZodObject<{{}}>]>"); | ||||||
| } else { | ||||||
|
|
@@ -704,7 +744,7 @@ impl<'a, 'b: 'a, Stdout: Write, Stderr: Write> Visitor<'a, 'b, Error> for Engine | |||||
| self.print_group_joiner(); | ||||||
| if matches!(calculate_occurrence(&occur), (0, max) if max > 0) { | ||||||
| if self.is_type_mode { | ||||||
| write!(self.stdout, "z.ZodUnion<["); | ||||||
| self.print_union_type_start(); | ||||||
| self.visit_group(group)?; | ||||||
| write!(self.stdout, ",z.ZodObject<{{}}>]>"); | ||||||
| } else { | ||||||
|
|
@@ -755,7 +795,7 @@ impl<'a, 'b: 'a, Stdout: Write, Stderr: Write> Visitor<'a, 'b, Error> for Engine | |||||
| fn visit_group(&mut self, g: &'b cddl::ast::Group<'a>) -> cddl::visitor::Result<Error> { | ||||||
| if g.group_choices.len() != 1 { | ||||||
| if self.is_type_mode { | ||||||
| write!(self.stdout, "z.ZodUnion<["); | ||||||
| self.print_union_type_start(); | ||||||
| } else { | ||||||
| write!(self.stdout, "z.union(["); | ||||||
| } | ||||||
|
|
||||||
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.
All tests in
tests/typescript.rsandtests/zod.rsuseinsta::assert_snapshot!against the shared CDDL fixtures inexamples/.Adding a separate
examples/zod4_compat.cddlwith manualassert!(stdout.contains(...))checks has two drawbacks:Choice = text / intonly exercises 1 of the 6z.ZodUnioncode paths (visit_type), leaving the other 5 (visit_array, bounded array occurrences, optionalTypeGroupname, optionalInlineGroup, andvisit_group) untested forzod4.examples/is snapshot-tested across bothtests/typescript.rsandtests/zod.rs.Could we instead add
tests/zod4.rs(mirroringtests/zod.rswithinsta::assert_snapshot!over the existingexamples/fixtures) and dropexamples/zod4_compat.cddl?