---
source: crates/laburnum-syntax-macro/src/tests/mod.rs
expression: "use laburnum_syntax_macro::tests::test_deeply_nested_types"
snapshot_kind: text
---
Input: >
  #[laburnum_syntax(AST)]
  pub struct DeeplyNested {
      nested1: Option<Vec<NodeId<crate::Type>>>,
      nested2: Vec<Option<NodeId<crate::Type>>>,
      nested3: Option<Vec<crate::declaration::Function, crate::declaration::Import>>,
  }

Output: >
  #[derive(Clone, PartialEq)]
  pub struct DeeplyNested {
      /// The unique identifier for this node.
      pub(crate) id: crate::NodeId,
      /// The syntax node id that this node is based on.
      pub(crate) syntax: crate::SyntaxId,
      /// Any meta information that is associated with this node.
      pub(crate) meta: Option<crate::NodeId>,
      pub(crate) nested1: Vec<Option<crate::NodeId>>,
      pub(crate) nested2: Vec<Option<crate::NodeId>>,
      pub(crate) nested3: Vec<Option<crate::NodeId>>,
  }
  impl DeeplyNested {
      pub fn get_id(&self) -> crate::NodeId {
          self.id
      }
      pub fn get_syntax(&self) -> crate::SyntaxId {
          self.syntax
      }
      pub fn get_meta(&self) -> Option<crate::NodeId> {
          self.meta
      }
  }
  pub mod deeply_nested_field {
      #[derive(Debug)]
      pub enum Nested3<'ast> {
          DeclarationFunction(&'ast crate::declaration::Function),
          DeclarationImport(&'ast crate::declaration::Import),
          Error,
      }
      impl<'ast> Nested3<'ast> {
          pub fn validate(&self, ast: &'ast crate::AST) -> crate::Result<()> {
              match self {
                  Self::Error => Err(crate::invalid_node_error()),
                  _ => Ok(()),
              }
          }
          pub fn from_node(node: &'ast crate::Node) -> Option<Nested3<'ast>> {
              Some(Self::from(node))
          }
      }
      impl<'ast> From<&'ast crate::Node> for Nested3<'ast> {
          fn from(n: &'ast crate::Node) -> Self {
              match n {
                  crate::Node::DeclarationFunction(val) => {
                      Nested3::DeclarationFunction(val)
                  }
                  crate::Node::DeclarationImport(val) => Nested3::DeclarationImport(val),
                  _ => Nested3::Error,
              }
          }
      }
      impl<'ast> From<&'ast crate::declaration::Function> for Nested3<'ast> {
          fn from(v: &'ast crate::declaration::Function) -> Self {
              Self::DeclarationFunction(v)
          }
      }
      impl<'ast> From<&'ast crate::declaration::Import> for Nested3<'ast> {
          fn from(v: &'ast crate::declaration::Import) -> Self {
              Self::DeclarationImport(v)
          }
      }
  }
  #[allow(unused)]
  impl DeeplyNested {
      ///Returns the `nested1` field
      pub fn get_nested1<'ast>(&self, ast: &'ast crate::AST) -> Vec<&'ast crate::Type> {
          {
              if let Some(ref nested1) = self.nested1 {
                  nested1
                      .iter()
                      .filter_map(|n: &crate::NodeId| {
                          ast.node(*n)
                              .and_then(|node: &crate::Node| {
                                  crate::Type::from_node(node)
                              })
                      })
                      .collect::<Vec<_>>()
              } else {
                  None
              }
          }
      }
      ///Returns the NodeIds for the `nested1` field
      pub fn get_nested1_ids<'ast>(&self, ast: &'ast crate::AST) -> Vec<crate::NodeId> {
          {
              if let Some(ref nested1) = self.nested1 {
                  nested1.clone()
              } else {
                  Vec::new()
              }
          }
      }
      ///Returns the `nested2` field
      pub fn get_nested2<'ast>(&self, ast: &'ast crate::AST) -> Vec<&'ast crate::Type> {
          {
              if let Some(ref nested2) = self.nested2 {
                  nested2
                      .iter()
                      .filter_map(|n: &crate::NodeId| {
                          ast.node(*n)
                              .and_then(|node: &crate::Node| {
                                  crate::Type::from_node(node)
                              })
                      })
                      .collect::<Vec<_>>()
              } else {
                  None
              }
          }
      }
      ///Returns the NodeIds for the `nested2` field
      pub fn get_nested2_ids<'ast>(&self, ast: &'ast crate::AST) -> Vec<crate::NodeId> {
          {
              if let Some(ref nested2) = self.nested2 {
                  nested2.clone()
              } else {
                  Vec::new()
              }
          }
      }
      ///Returns the `nested3` field
      pub fn get_nested3<'ast>(
          &self,
          ast: &'ast crate::AST,
      ) -> Vec<deeply_nested_field::Nested3<'ast>> {
          {
              if let Some(ref nested3) = self.nested3 {
                  nested3
                      .iter()
                      .filter_map(|n| {
                          ast
                              .node(*n)
                              .and_then(|node| deeply_nested_field::Nested3::from_node(
                                  node,
                              ))
                      })
                      .collect::<Vec<_>>()
              } else {
                  None
              }
          }
      }
  }
  #[allow(unused)]
  impl<'ast> DeeplyNested {
      pub fn validate(&self, ast: &'ast crate::AST) -> crate::Result<()> {
          self.get_nested3(ast).iter().map(|f| f.validate(ast).unwrap());
          Ok(())
      }
  }
  #[allow(unused)]
  impl std::fmt::Display for DeeplyNested {
      fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
          f.debug_struct(
                  &format!(
                      "{}::{}", { let __mp = module_path!(); __mp.find("::").map(| i | &
                      __mp[i + 2..]).unwrap_or(__mp) }, "DeeplyNested"
                  ),
              )
              .finish()
      }
  }
  #[allow(unused)]
  impl std::fmt::Debug for DeeplyNested {
      fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
          f.debug_struct(
                  &format!(
                      "{}::{}", { let __mp = module_path!(); __mp.find("::").map(| i | &
                      __mp[i + 2..]).unwrap_or(__mp) }, "DeeplyNested"
                  ),
              )
              .finish()
      }
  }
  #[allow(unused)]
  impl bluegum::Bluegum for DeeplyNested {
      fn node(&self, b: &mut bluegum::Builder) {
          b.name(
              &crate::style_title(
                  {
                      let __mp = module_path!();
                      __mp.find("::").map(|i| &__mp[i + 2..]).unwrap_or(__mp)
                  },
                  &"DeeplyNested",
              ),
          );
      }
  }
  #[allow(unused)]
  impl<'tokens, 'src> bluegum::BluegumWithState<crate::Printer<'tokens>> for DeeplyNested {
      fn node_with_state(
          &self,
          b: &mut bluegum::Builder,
          state: &crate::Printer<'tokens>,
      ) {
          use owo_colors::OwoColorize;
          use unicode_width::UnicodeWidthStr;
          b.name(
              &crate::style_title(
                  {
                      let __mp = module_path!();
                      __mp.find("::").map(|i| &__mp[i + 2..]).unwrap_or(__mp)
                  },
                  &"DeeplyNested",
              ),
          );
          if let Some(cst_node) = state.get_syntax(self.syntax) {
              let span = cst_node.span();
              if let Some(span_data) = span.data(state.get_span_cache()) {
                  b.debug(
                      "span",
                      format!("{}..{}", span_data.start, span_data.start + span_data.len),
                  );
              }
          }
          if let Some(meta) = self.meta {
              b.add_node_with_state(state, &"meta".blue().italic().to_string(), &meta);
          }
          {
              {
                  if let Some(ref nested1) = self.nested1 {
                      b.add_nodes_of_builders(
                          stringify!(nested1),
                          nested1
                              .iter()
                              .map(|child| bluegum::Builder::render_with_state(
                                  child,
                                  state,
                              ))
                              .collect::<Vec<bluegum::Builder>>(),
                      );
                  }
              };
              {
                  if let Some(ref nested2) = self.nested2 {
                      b.add_nodes_of_builders(
                          stringify!(nested2),
                          nested2
                              .iter()
                              .map(|child| bluegum::Builder::render_with_state(
                                  child,
                                  state,
                              ))
                              .collect::<Vec<bluegum::Builder>>(),
                      );
                  }
              };
              {
                  if let Some(ref nested3) = self.nested3 {
                      b.add_nodes_of_builders(
                          stringify!(nested3),
                          nested3
                              .iter()
                              .map(|child| bluegum::Builder::render_with_state(
                                  child,
                                  state,
                              ))
                              .collect::<Vec<bluegum::Builder>>(),
                      );
                  }
              };
          };
      }
  }
