Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A commandline utility for converting CDDL to various formats.
## Usage

1. Clone this repo and `cd` into it.
2. `cargo run -- path/to/file.cddl`
2. `cargo run -- [--format <type-script|zod>] path/to/file.cddl`

## Tips

Expand All @@ -26,4 +26,4 @@ cargo run -- path/to/file.cddl | prettier --stdin-filepath=$outfile > $outfile

## Limitations

1. Only [`TypeScript`](https://www.typescriptlang.org/) and [`Zod v4`](https://zod.dev/) is supported at the moment.
1. Only [`TypeScript`](https://www.typescriptlang.org/) (`--format type-script`) and [`Zod v4`](https://zod.dev/) (`--format zod`) are supported at the moment.
21 changes: 9 additions & 12 deletions src/engines/zod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ 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<[");
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, "\"{}\":\"{}\",", value, value);
}
}
write!(self.stdout, "]>");
write!(self.stdout, "}}>");
} else {
write!(self.stdout, "z.enum([");
for type2 in t.type_choices.iter().map(|choice| &choice.type1.type2) {
Expand All @@ -137,7 +137,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<[");
write!(self.stdout, "z.ZodUnion<readonly [");
} else {
write!(self.stdout, "z.union([");
}
Expand Down Expand Up @@ -369,7 +369,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<[");
write!(self.stdout, "z.ZodUnion<readonly [");
for bound in lower..upper + 1 {
if bound != 0 {
write!(self.stdout, ",");
Expand Down Expand Up @@ -404,9 +404,6 @@ 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 {
write!(self.stdout, ", \"atleastone\"");
}
write!(self.stdout, ">");
} else {
write!(self.stdout, "z.array(");
Expand Down Expand Up @@ -597,7 +594,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<[");
write!(self.stdout, "z.ZodUnion<readonly [");
} else {
write!(self.stdout, "z.union([");
}
Expand Down Expand Up @@ -688,7 +685,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<[");
write!(self.stdout, "z.ZodUnion<readonly [");
self.visit_type_groupname_entry(ge)?;
write!(self.stdout, ",z.ZodObject<{{}}>]>");
} else {
Expand All @@ -704,7 +701,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<[");
write!(self.stdout, "z.ZodUnion<readonly [");
self.visit_group(group)?;
write!(self.stdout, ",z.ZodObject<{{}}>]>");
} else {
Expand Down Expand Up @@ -755,7 +752,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<[");
write!(self.stdout, "z.ZodUnion<readonly [");
} else {
write!(self.stdout, "z.union([");
}
Expand Down
Loading
Loading