Add pretty print for polymorphic enum loop.
This commit is contained in:
parent
5a3403cc28
commit
3159f119bc
@ -7,6 +7,55 @@ use crate::spec::BuildSpec;
|
|||||||
use convert_case::{Case, Casing};
|
use convert_case::{Case, Casing};
|
||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::{format_ident, quote};
|
use quote::{format_ident, quote};
|
||||||
|
use crate::spec::polymorphic_enum_loop_spec::{PolymorphicEnumLoopBuildSpec, PolymorphicEnumLoopRule, PolymorphicEnumLoopRuleBuildChild};
|
||||||
|
|
||||||
|
fn make_polymorphic_enum_loop_p2_impl(spec: &PolymorphicEnumLoopBuildSpec) -> TokenStream {
|
||||||
|
let type_ident = format_ident!("{}", spec.name());
|
||||||
|
let type_string = spec.name();
|
||||||
|
|
||||||
|
let build = spec.rules()
|
||||||
|
.find(|rule| {
|
||||||
|
match rule {
|
||||||
|
PolymorphicEnumLoopRule::Build(_) => true,
|
||||||
|
_ => false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.map(|rule| {
|
||||||
|
match rule {
|
||||||
|
PolymorphicEnumLoopRule::Build(build) => build,
|
||||||
|
_ => unreachable!()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let child_print_statements = build.children()
|
||||||
|
.map(|child| {
|
||||||
|
let child_ident = match child {
|
||||||
|
PolymorphicEnumLoopRuleBuildChild::UseCurrent(use_current) => {
|
||||||
|
format_ident!("{}", use_current.name())
|
||||||
|
}
|
||||||
|
PolymorphicEnumLoopRuleBuildChild::OnEach(on_each) => {
|
||||||
|
format_ident!("{}", on_each.name())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
quote! {
|
||||||
|
self.#child_ident().pretty_print(writer)?;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
impl PrettyPrint for #type_ident {
|
||||||
|
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
||||||
|
writer.writeln_indented(#type_string)?;
|
||||||
|
writer.increase_indent();
|
||||||
|
#(#child_print_statements)*
|
||||||
|
writer.decrease_indent();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn make_polymorphic_type_p2_impl(spec: &PolymorphicTypeBuildSpec) -> TokenStream {
|
fn make_polymorphic_type_p2_impl(spec: &PolymorphicTypeBuildSpec) -> TokenStream {
|
||||||
let type_ident = format_ident!("{}", spec.name());
|
let type_ident = format_ident!("{}", spec.name());
|
||||||
@ -209,6 +258,8 @@ pub fn make_pretty_print_impl(build_spec: &BuildSpec) -> Option<TokenStream> {
|
|||||||
Some(make_polymorphic_type_p2_impl(polymorphic_type))
|
Some(make_polymorphic_type_p2_impl(polymorphic_type))
|
||||||
}
|
}
|
||||||
BuildSpec::PolymorphicPassThrough(_) => None,
|
BuildSpec::PolymorphicPassThrough(_) => None,
|
||||||
BuildSpec::PolymorphicEnumLoop(_) => None,
|
BuildSpec::PolymorphicEnumLoop(polymorphic_enum_loop) => {
|
||||||
|
Some(make_polymorphic_enum_loop_p2_impl(polymorphic_enum_loop))
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user