---
source: crates/bullion/language/syntax/macro/src/ast/tests.rs
expression: "use bullion_syntax_macro::ast::tests::definition"
snapshot_kind: text
---
Source: >
#[bullion_syntax(AST)]
pub struct Definition {
items: Vec<crate::declaration::Function, crate::declaration::Import>,
expr: NodeId<crate::expression::Expression>,
}
Output: >
#[derive(Clone, PartialEq)]
pub struct Definition {
/// The unique identifier for this node.
pub(crate) id: crate::NodeId,
/// The syntax node id that this node is based on.
pub(crate) syntax: bullion_syntax_concrete::CstNodeId,
/// Any meta information that is associated with this node.
pub(crate) meta: Option<crate::NodeId>,
pub(crate) items: Vec<crate::NodeId>,
pub(crate) expr: crate::NodeId,
}
impl Definition {
pub fn get_id(&self) -> crate::NodeId {
self.id
}
pub fn get_syntax(&self) -> bullion_syntax_concrete::CstNodeId {
self.syntax
}
pub fn get_meta(&self) -> Option<crate::NodeId> {
self.meta
}
}
pub mod definition_field {
#[derive(Debug)]
pub enum Items<'ast> {
DeclarationFunction(&'ast crate::declaration::Function),
DeclarationImport(&'ast crate::declaration::Import),
Error,
}
impl<'ast> Items<'ast> {
pub fn validate(&self, ast: &'ast crate::AST) -> bullion_error::Result<()> {
match self {
Self::Error => Err(bullion_error::BLC2204()),
_ => Ok(()),
}
}
pub fn from_node(node: &'ast crate::Node) -> Option<Items<'ast>> {
Some(Self::from(node))
}
}
impl<'ast> From<&'ast crate::Node> for Items<'ast> {
fn from(n: &'ast crate::Node) -> Self {
match n {
crate::Node::DeclarationFunction(val) => Items::DeclarationFunction(val),
crate::Node::DeclarationImport(val) => Items::DeclarationImport(val),
_ => Items::Error,
}
}
}
impl<'ast> From<&'ast crate::declaration::Function> for Items<'ast> {
fn from(v: &'ast crate::declaration::Function) -> Self {
Self::DeclarationFunction(v)
}
}
impl<'ast> From<&'ast crate::declaration::Import> for Items<'ast> {
fn from(v: &'ast crate::declaration::Import) -> Self {
Self::DeclarationImport(v)
}
}
}
#[allow(unused)]
impl Definition {
///Returns the `items` field
pub fn get_items<'ast>(
&self,
ast: &'ast crate::AST,
) -> Vec<definition_field::Items<'ast>> {
{
let items = &self.items;
items
.iter()
.filter_map(|n| {
ast
.node(*n)
.and_then(|node| definition_field::Items::from_node(node))
})
.collect::<Vec<_>>()
}
}
///Returns the `expr` field
pub fn get_expr<'ast>(
&self,
ast: &'ast crate::AST,
) -> Option<&'ast crate::expression::Expression> {
{
let expr = &self.expr;
ast.node(*expr)
.and_then(|node| crate::expression::Expression::from_node(node))
}
}
///Returns the NodeId for the `expr` field
pub fn get_expr_id<'ast>(&self, ast: &'ast crate::AST) -> crate::NodeId {
{
let expr = &self.expr;
*expr
}
}
}
#[allow(unused)]
impl<'ast> Definition {
pub fn validate(&self, ast: &'ast crate::AST) -> bullion_error::Result<()> {
self.get_items(ast).iter().map(|f| f.validate(ast).unwrap());
Ok(())
}
}
#[allow(unused)]
impl std::fmt::Display for Definition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct(
&format!(
"{}::{}", & module_path!() .replace("bullion_syntax_abstract::", ""),
"Definition"
),
)
.finish()
}
}
#[allow(unused)]
impl std::fmt::Debug for Definition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct(
&format!(
"{}::{}", & module_path!() .replace("bullion_syntax_abstract::", ""),
"Definition"
),
)
.finish()
}
}
#[allow(unused)]
impl bluegum::Bluegum for Definition {
fn node(&self, b: &mut bluegum::Builder) {
b.name(
&crate::style_title(
&module_path!().replace("bullion_syntax_abstract::", ""),
&"Definition",
),
);
}
}
#[allow(unused)]
impl<'tokens, 'src> bluegum::BluegumWithState<crate::Printer<'tokens>> for Definition {
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(
&module_path!().replace("bullion_syntax_abstract::", ""),
&"Definition",
),
);
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);
}
{
{
let items = &self.items;
b.add_nodes_of_builders(
stringify!(items),
items
.iter()
.map(|child| bluegum::Builder::render_with_state(child, state))
.collect::<Vec<bluegum::Builder>>(),
);
};
{
let expr = &self.expr;
b.add_node_with_state(state, stringify!(expr), expr);
};
};
}
}