-
Notifications
You must be signed in to change notification settings - Fork 119
Support serde flatten for Typescript #115
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
1171bc5
037a0bd
a75852b
ee5923f
0b97bad
6608b74
9aa71fb
c70b536
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 |
|---|---|---|
|
|
@@ -78,7 +78,7 @@ pub enum ParseError { | |
| } | ||
|
|
||
| /// Parse the given Rust source string into `ParsedData`. | ||
| pub fn parse(input: &str) -> Result<ParsedData, ParseError> { | ||
|
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.
Author
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. Sounds fair, but concretely just wondering how you would see it implemented. |
||
| pub fn parse(input: &str, supports_flatten: bool) -> Result<ParsedData, ParseError> { | ||
| let mut parsed_data = ParsedData::default(); | ||
|
|
||
| // We will only produce output for files that contain the `#[typeshare]` | ||
|
|
@@ -94,7 +94,7 @@ pub fn parse(input: &str) -> Result<ParsedData, ParseError> { | |
| for item in flatten_items(source.items.iter()) { | ||
| match item { | ||
| syn::Item::Struct(s) if has_typeshare_annotation(&s.attrs) => { | ||
| parsed_data.push_rust_thing(parse_struct(s)?); | ||
| parsed_data.push_rust_thing(parse_struct(s, supports_flatten)?); | ||
| } | ||
| syn::Item::Enum(e) if has_typeshare_annotation(&e.attrs) => { | ||
| parsed_data.push_rust_thing(parse_enum(e)?); | ||
|
|
@@ -131,7 +131,7 @@ fn flatten_items<'a>( | |
| /// | ||
| /// This function can currently return something other than a struct, which is a | ||
| /// hack. | ||
| fn parse_struct(s: &ItemStruct) -> Result<RustItem, ParseError> { | ||
| fn parse_struct(s: &ItemStruct, supports_flatten: bool) -> Result<RustItem, ParseError> { | ||
| let serde_rename_all = serde_rename_all(&s.attrs); | ||
|
|
||
| let generic_types = s | ||
|
|
@@ -156,6 +156,7 @@ fn parse_struct(s: &ItemStruct) -> Result<RustItem, ParseError> { | |
| })); | ||
| } | ||
|
|
||
| let mut flattened: bool = false; | ||
| Ok(match &s.fields { | ||
| // Structs | ||
| Fields::Named(f) => { | ||
|
|
@@ -170,9 +171,14 @@ fn parse_struct(s: &ItemStruct) -> Result<RustItem, ParseError> { | |
| RustType::try_from(&f.ty)? | ||
| }; | ||
|
|
||
| if serde_flatten(&f.attrs) { | ||
| return Err(ParseError::SerdeFlattenNotAllowed); | ||
| } | ||
| flattened = if serde_flatten(&f.attrs) { | ||
| if !supports_flatten { | ||
| return Err(ParseError::SerdeFlattenNotAllowed); | ||
| } | ||
| true | ||
| } else { | ||
| false | ||
| }; | ||
|
|
||
| let has_default = serde_default(&f.attrs); | ||
| let decorators = get_field_decorators(&f.attrs); | ||
|
|
@@ -183,6 +189,7 @@ fn parse_struct(s: &ItemStruct) -> Result<RustItem, ParseError> { | |
| comments: parse_comment_attrs(&f.attrs), | ||
| has_default, | ||
| decorators, | ||
| flattened, | ||
| }) | ||
| }) | ||
| .collect::<Result<_, ParseError>>()?; | ||
|
|
@@ -380,6 +387,7 @@ fn parse_enum_variant( | |
| comments: parse_comment_attrs(&f.attrs), | ||
| has_default, | ||
| decorators, | ||
| flattened: false, | ||
| }) | ||
| }) | ||
| .collect::<Result<Vec<_>, ParseError>>()?, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.