diff --git a/ast-generator/src/deserialize.rs b/ast-generator/src/deserialize.rs index 869a2e7..4c199cd 100644 --- a/ast-generator/src/deserialize.rs +++ b/ast-generator/src/deserialize.rs @@ -15,7 +15,7 @@ fn get_vec(vec: &Yaml) -> bool { vec.as_bool().unwrap_or_else(|| false) } -fn get_vec_child_to_build(build: &Yaml, rule: &str) -> VecChildToBuild { +fn get_vec_child_to_build(build: &Yaml, name: &str, rule: &str) -> VecChildToBuild { if build.is_hash() { let build_type = build["build"].as_str().unwrap(); let var_name = build["var"] @@ -32,7 +32,7 @@ fn get_vec_child_to_build(build: &Yaml, rule: &str) -> VecChildToBuild { let build_as_str = build.as_str().unwrap_or(rule); VecChildToBuild::Type(VecTypeChildToBuild::new( build_as_str, - build_as_str.to_case(Case::Snake).as_str(), + name, make_build_fn_name(build_as_str).as_str(), )) } @@ -42,7 +42,7 @@ fn get_vec_child(name: &str, rule: &str, build: &Yaml) -> ChildSpec { ChildSpec::VecChild(VecChild::new( name, rule, - get_vec_child_to_build(build, rule), + get_vec_child_to_build(build, name, rule), )) } @@ -86,10 +86,11 @@ fn get_child_specs(children: &Yaml) -> Vec { .map(|child_spec| { if child_spec.is_hash() { let as_hash = child_spec.as_hash().unwrap(); - let only_pair = as_hash.iter().next().unwrap(); - - let name = only_pair.0.as_str().unwrap(); - let props = only_pair.1; + let (name, props) = as_hash + .iter() + .next() + .map(|(name, props)| (name.as_str().unwrap(), props)) + .unwrap(); let rule = props["rule"].as_str().unwrap(); diff --git a/ast-generator/src/type_gen.rs b/ast-generator/src/type_gen.rs index 4c60280..62f2abb 100644 --- a/ast-generator/src/type_gen.rs +++ b/ast-generator/src/type_gen.rs @@ -31,20 +31,25 @@ fn handle_vec_child( annotated_members: &mut Vec, accessors: &mut Vec, ) { - let (child_ident, child_type_ident) = match vec_child.build() { + let (child_ident, child_ident_mut, child_type_ident) = match vec_child.build() { VecChildToBuild::Type(vec_type_child) => ( format_ident!("{}", vec_type_child.var_name()), + format_ident!("{}_mut", vec_type_child.var_name()), format_ident!("{}", vec_type_child.build()), ), }; member_names.push(child_ident.clone()); annotated_members.push(quote! { - #child_ident: Vec<#child_type_ident> + #child_ident: Vec> }); accessors.push(quote! { - pub fn #child_ident(&self) -> &[#child_type_ident] { - &self.#child_ident + pub fn #child_ident(&self) -> impl Iterator { + self.#child_ident.iter().map(Box::as_ref) + } + + pub fn #child_ident_mut(&mut self) -> impl Iterator { + self.#child_ident.iter_mut().map(Box::as_mut) } }); } @@ -56,14 +61,19 @@ fn handle_single_type_child( accessors: &mut Vec, ) { let child_ident = format_ident!("{}", single_type_child.var_name()); + let child_ident_mut = format_ident!("{}_mut", single_type_child.var_name()); let child_type_ident = format_ident!("{}", single_type_child.build()); member_names.push(child_ident.clone()); annotated_members.push(quote! { - #child_ident: #child_type_ident + #child_ident: Box<#child_type_ident> }); accessors.push(quote! { pub fn #child_ident(&self) -> &#child_type_ident { - &self.#child_ident + self.#child_ident.as_ref() + } + + pub fn #child_ident_mut(&mut self) -> &mut #child_type_ident { + self.#child_ident.as_mut() } }); }