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

Wrapper JSON Overhaul #224

Merged
merged 7 commits into from
Jan 30, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ book/
# Test files
tests/*/export/**
tests/*/export_wasm/**
tests/*/export_preserve/**
8 changes: 8 additions & 0 deletions docs/docs/comment_dsl.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ Putting this comment on a type forces that type to derive those traits even if i
This is useful for when you are writing utility code that would put them in a map and want the generated code to have it already,
which is particularly useful for re-generating as it lets your `mod.rs` files remain untouched.

## @custom_json

```cddl
foo = uint ; @newtype @custom_json
```

Avoids generating and/or deriving json-related traits under the assumption that the user will supply their own implementation to be used in the generated library.

## _CDDL_CODEGEN_EXTERN_TYPE_

While not as a comment, this allows you to compose in hand-written structs into a cddl spec.
Expand Down
44 changes: 43 additions & 1 deletion src/comment_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct RuleMetadata {
pub is_newtype: bool,
pub no_alias: bool,
pub used_as_key: bool,
pub custom_json: bool,
}

pub fn merge_metadata(r1: &RuleMetadata, r2: &RuleMetadata) -> RuleMetadata {
Expand All @@ -26,6 +27,7 @@ pub fn merge_metadata(r1: &RuleMetadata, r2: &RuleMetadata) -> RuleMetadata {
is_newtype: r1.is_newtype || r2.is_newtype,
no_alias: r1.no_alias || r2.no_alias,
used_as_key: r1.used_as_key || r2.used_as_key,
custom_json: r1.custom_json || r2.custom_json,
};
merged.verify();
merged
Expand All @@ -36,6 +38,7 @@ enum ParseResult {
Name(String),
DontGenAlias,
UsedAsKey,
CustomJson,
}

impl RuleMetadata {
Expand All @@ -61,6 +64,9 @@ impl RuleMetadata {
ParseResult::UsedAsKey => {
base.used_as_key = true;
}
ParseResult::CustomJson => {
base.custom_json = true;
}
}
}
base.verify();
Expand Down Expand Up @@ -101,9 +107,21 @@ fn tag_used_as_key(input: &str) -> IResult<&str, ParseResult> {
Ok((input, ParseResult::UsedAsKey))
}

fn tag_custom_json(input: &str) -> IResult<&str, ParseResult> {
let (input, _) = tag("@custom_json")(input)?;

Ok((input, ParseResult::CustomJson))
}

fn whitespace_then_tag(input: &str) -> IResult<&str, ParseResult> {
let (input, _) = take_while(char::is_whitespace)(input)?;
let (input, result) = alt((tag_name, tag_newtype, tag_no_alias, tag_used_as_key))(input)?;
let (input, result) = alt((
tag_name,
tag_newtype,
tag_no_alias,
tag_used_as_key,
tag_custom_json,
))(input)?;

Ok((input, result))
}
Expand Down Expand Up @@ -144,6 +162,7 @@ fn parse_comment_name() {
is_newtype: false,
no_alias: false,
used_as_key: false,
custom_json: false,
}
))
);
Expand All @@ -160,6 +179,7 @@ fn parse_comment_newtype() {
is_newtype: true,
no_alias: false,
used_as_key: false,
custom_json: false,
}
))
);
Expand All @@ -176,6 +196,7 @@ fn parse_comment_newtype_and_name() {
is_newtype: true,
no_alias: false,
used_as_key: false,
custom_json: false,
}
))
);
Expand All @@ -192,6 +213,7 @@ fn parse_comment_newtype_and_name_and_used_as_key() {
is_newtype: true,
no_alias: false,
used_as_key: true,
custom_json: false,
}
))
);
Expand All @@ -208,6 +230,7 @@ fn parse_comment_used_as_key() {
is_newtype: false,
no_alias: false,
used_as_key: true,
custom_json: false,
}
))
);
Expand All @@ -224,6 +247,7 @@ fn parse_comment_newtype_and_name_inverse() {
is_newtype: true,
no_alias: false,
used_as_key: false,
custom_json: false,
}
))
);
Expand All @@ -240,6 +264,24 @@ fn parse_comment_name_noalias() {
is_newtype: false,
no_alias: true,
used_as_key: false,
custom_json: false,
}
))
);
}

#[test]
fn parse_comment_newtype_and_custom_json() {
assert_eq!(
rule_metadata("@custom_json @newtype"),
Ok((
"",
RuleMetadata {
name: None,
is_newtype: true,
no_alias: false,
used_as_key: false,
custom_json: true,
}
))
);
Expand Down
Loading
Loading