-
Notifications
You must be signed in to change notification settings - Fork 30
Flattened struct helper functions #273
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
Exotik850
wants to merge
11
commits into
Bergmann89:master
Choose a base branch
from
Exotik850:flattened-functions
base: master
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
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c18220c
add flattened content helpers rendering step, add flags
Exotik850 67daf48
remove rendererflag, only publicly add as a renderstep
Exotik850 f1e7ba6
add test
Exotik850 44106f3
make sure emitted method names are not keywords
Exotik850 b39ed78
remove em dash
Exotik850 b34078f
rename render step and module
Exotik850 fd626c2
use complex data field ident instead of generating manually
Exotik850 c75b20c
only emit helpers for types that appear once
Exotik850 9402240
add content helpers render step to RenderStep enum
Exotik850 a38502e
remove keywords export
Exotik850 4aacf94
dedupe underscore for mut method
Exotik850 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
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
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
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
115 changes: 115 additions & 0 deletions
115
xsd-parser/src/pipeline/renderer/steps/content_helper.rs
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 |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| use proc_macro2::TokenStream; | ||
| use quote::{format_ident, quote}; | ||
|
|
||
| use crate::models::data::{ComplexData, ComplexDataEnum, DataTypeVariant, Occurs}; | ||
| use crate::pipeline::renderer::{Context, RenderStep, RenderStepType}; | ||
|
|
||
| /// RenderStep that generates ergonomic helper accessors for flattened struct content. | ||
| /// | ||
| /// This targets the pattern: | ||
| /// pub struct Foo { pub content: Vec<FooContent>, ... } | ||
| /// pub enum FooContent { PrivateNote(String), ... } | ||
| /// | ||
| /// and generates: | ||
| /// impl Foo { | ||
| /// pub fn private_note(&self) -> Option<&String> { ... } | ||
| /// } | ||
| #[derive(Debug, Clone, Copy)] | ||
| pub struct ContentHelpersRenderStep; | ||
|
|
||
| impl RenderStep for ContentHelpersRenderStep { | ||
| fn render_step_type(&self) -> RenderStepType { | ||
| RenderStepType::ExtraImpls | ||
| } | ||
|
|
||
| fn render_type(&mut self, ctx: &mut Context<'_, '_>) { | ||
| let DataTypeVariant::Complex(complex) = &ctx.data.variant else { | ||
| return; | ||
| }; | ||
| let ComplexData::Struct { | ||
| type_, | ||
| content_type, | ||
| } = complex | ||
| else { | ||
| return; | ||
| }; | ||
| let Some(content) = type_.content() else { | ||
| return; | ||
| }; | ||
| if content.occurs != Occurs::DynamicList { | ||
| return; | ||
| }; | ||
|
|
||
| // The content enum is stored inline as content_type of ComplexData::Struct | ||
| let Some(content_type) = content_type else { | ||
| return; | ||
| }; | ||
|
|
||
| let impl_block = match content_type.as_ref() { | ||
| ComplexData::Enum { | ||
| type_: enum_type, .. | ||
| } => render_helpers_for_complex_enum( | ||
| ctx, | ||
| &type_.base.type_ident, | ||
| &content.field_ident, | ||
| enum_type, | ||
| ), | ||
| ComplexData::Struct { .. } => { | ||
| // Struct content (e.g. a sequence) - no enum variants to flatten | ||
| return; | ||
| } | ||
| }; | ||
|
|
||
| ctx.current_module().append(impl_block); | ||
| } | ||
| } | ||
|
|
||
| /// Generate helper accessor methods for a [`ComplexDataEnum`] content type. | ||
| fn render_helpers_for_complex_enum( | ||
| ctx: &Context<'_, '_>, | ||
| struct_ident: &proc_macro2::Ident, | ||
| content_field_ident: &proc_macro2::Ident, | ||
| enum_type: &ComplexDataEnum<'_>, | ||
| ) -> TokenStream { | ||
| let enum_ident = &enum_type.base.type_ident; | ||
|
|
||
| let methods = enum_type.elements.iter().filter_map(|e| { | ||
| if e.occurs != Occurs::Single { | ||
| return None; | ||
| } | ||
| let variant_ident = &e.variant_ident; | ||
| let method_ident = &e.field_ident; | ||
| let mut_method = format!("{}_mut", method_ident).replace("__", "_"); | ||
| let mut_method_ident = format_ident!("{mut_method}"); | ||
| let target_ty = ctx.resolve_type_for_module(&e.target_type); | ||
| let option = ctx.resolve_build_in("::core::option::Option"); | ||
|
|
||
| let out = quote! { | ||
| #[inline] | ||
| pub fn #method_ident(&self) -> #option<&#target_ty> { | ||
| self.#content_field_ident.iter().find_map(|x| { | ||
| match x { | ||
| #enum_ident::#variant_ident(v) => #option::Some(v), | ||
| _ => #option::None, | ||
| } | ||
| }) | ||
| } | ||
| #[inline] | ||
| pub fn #mut_method_ident(&mut self) -> #option<&mut #target_ty> { | ||
| self.#content_field_ident.iter_mut().find_map(|x| { | ||
| match x { | ||
| #enum_ident::#variant_ident(v) => #option::Some(v), | ||
| _ => #option::None, | ||
| } | ||
| }) | ||
| } | ||
| }; | ||
| Some(out) | ||
| }); | ||
|
|
||
| quote! { | ||
| impl #struct_ident { | ||
| #( #methods )* | ||
| } | ||
| } | ||
| } | ||
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
6 changes: 6 additions & 0 deletions
6
xsd-parser/tests/feature/flattened_content_helpers/example/default.xml
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 |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <Foo xmlns="http://example.com"> | ||
| <Bar>hello</Bar> | ||
| <Baz>42</Baz> | ||
| <Bar>world</Bar> | ||
| </Foo> |
40 changes: 40 additions & 0 deletions
40
xsd-parser/tests/feature/flattened_content_helpers/expected/default.rs
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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| pub type Foo = FooType; | ||
| #[derive(Debug)] | ||
| pub struct FooType { | ||
| pub content: Vec<FooTypeContent>, | ||
| } | ||
| #[derive(Debug)] | ||
| pub enum FooTypeContent { | ||
| Bar(String), | ||
| Baz(i32), | ||
| } | ||
| impl FooType { | ||
| #[inline] | ||
| pub fn bar(&self) -> Option<&String> { | ||
| self.content.iter().find_map(|x| match x { | ||
| FooTypeContent::Bar(v) => Option::Some(v), | ||
| _ => Option::None, | ||
| }) | ||
| } | ||
| #[inline] | ||
| pub fn bar_mut(&mut self) -> Option<&mut String> { | ||
| self.content.iter_mut().find_map(|x| match x { | ||
| FooTypeContent::Bar(v) => Option::Some(v), | ||
| _ => Option::None, | ||
| }) | ||
| } | ||
| #[inline] | ||
| pub fn baz(&self) -> Option<&i32> { | ||
| self.content.iter().find_map(|x| match x { | ||
| FooTypeContent::Baz(v) => Option::Some(v), | ||
| _ => Option::None, | ||
| }) | ||
| } | ||
| #[inline] | ||
| pub fn baz_mut(&mut self) -> Option<&mut i32> { | ||
| self.content.iter_mut().find_map(|x| match x { | ||
| FooTypeContent::Baz(v) => Option::Some(v), | ||
| _ => Option::None, | ||
| }) | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| use xsd_parser::{pipeline::renderer::ContentHelpersRenderStep, Config, IdentType}; | ||
|
|
||
| use crate::utils::{generate_test, ConfigEx}; | ||
|
|
||
| fn config() -> Config { | ||
| Config::test_default() | ||
| .with_render_step(ContentHelpersRenderStep) | ||
| .with_generate([(IdentType::Element, "tns:Foo")]) | ||
| } | ||
|
|
||
| #[test] | ||
| fn generate_default() { | ||
| generate_test( | ||
| "tests/feature/flattened_content_helpers/schema.xsd", | ||
| "tests/feature/flattened_content_helpers/expected/default.rs", | ||
| config(), | ||
| ); | ||
| } |
15 changes: 15 additions & 0 deletions
15
xsd-parser/tests/feature/flattened_content_helpers/schema.xsd
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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" | ||
| xmlns:tns="http://example.com" | ||
| targetNamespace="http://example.com" | ||
| elementFormDefault="qualified"> | ||
|
|
||
| <xs:complexType name="FooType"> | ||
| <xs:choice maxOccurs="unbounded"> | ||
| <xs:element name="Bar" type="xs:string" /> | ||
| <xs:element name="Baz" type="xs:int" /> | ||
| </xs:choice> | ||
| </xs:complexType> | ||
|
|
||
| <xs:element name="Foo" type="tns:FooType" /> | ||
| </xs:schema> |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.