rbatis_codegen/codegen/syntax_tree_html/
mod.rs

1use proc_macro2::TokenStream;
2use crate::codegen::loader_html::Element;
3
4pub mod sql_tag_node;
5pub mod include_tag_node;
6pub mod mapper_tag_node;
7pub mod if_tag_node;
8pub mod trim_tag_node;
9pub mod bind_tag_node;
10pub mod where_tag_node;
11pub mod choose_tag_node;
12pub mod when_tag_node;
13pub mod otherwise_tag_node;
14pub mod foreach_tag_node;
15pub mod set_tag_node;
16pub mod continue_tag_node;
17pub mod break_tag_node;
18pub mod select_tag_node;
19pub mod update_tag_node;
20pub mod insert_tag_node;
21pub mod delete_tag_node;
22
23// Re-export all node structs for easier access
24pub use sql_tag_node::SqlTagNode;
25pub use include_tag_node::IncludeTagNode;
26pub use mapper_tag_node::MapperTagNode;
27pub use if_tag_node::IfTagNode;
28pub use trim_tag_node::TrimTagNode;
29pub use bind_tag_node::BindTagNode;
30pub use where_tag_node::WhereTagNode;
31pub use choose_tag_node::ChooseTagNode;
32pub use when_tag_node::WhenTagNode;
33pub use otherwise_tag_node::OtherwiseTagNode;
34pub use foreach_tag_node::ForeachTagNode;
35pub use set_tag_node::SetTagNode;
36pub use continue_tag_node::ContinueTagNode;
37pub use break_tag_node::BreakTagNode;
38pub use select_tag_node::SelectTagNode;
39pub use update_tag_node::UpdateTagNode;
40pub use insert_tag_node::InsertTagNode;
41pub use delete_tag_node::DeleteTagNode;
42
43/// Context passed around during token generation.
44/// FChildParser is a type parameter for the function that parses child elements.
45pub struct NodeContext<'a, FChildParser>
46where
47    FChildParser: FnMut(&[Element], &mut TokenStream, &mut Vec<String>, &str) -> TokenStream,
48{
49    pub methods: &'a mut TokenStream, // For accumulating helper methods (e.g., for CRUD operations)
50    pub fn_name: &'a str,            // The name of the main function being generated
51    pub child_parser: FChildParser,   // The function to call to parse child Elements
52}
53
54impl<'a, FChildParser> NodeContext<'a, FChildParser>
55where
56    FChildParser: FnMut(&[Element], &mut TokenStream, &mut Vec<String>, &str) -> TokenStream,
57{
58    /// Helper method to parse child elements using the provided child_parser function.
59    /// The `ignore` vector is passed directly here for flexibility with constructs like <foreach>.
60    pub fn parse_children(&mut self, children: &[Element], ignore: &mut Vec<String>) -> TokenStream {
61        (self.child_parser)(children, self.methods, ignore, self.fn_name)
62    }
63}
64
65/// Trait for all HTML abstract syntax tree (AST) nodes.
66/// Defines how a node is created from an `Element` and how it generates Rust TokenStream.
67pub trait HtmlAstNode {
68    /// Returns the XML tag name for this node type (e.g., "if", "select").
69    fn node_tag_name() -> &'static str;
70
71    /// Creates an instance of the node from a generic `Element`.
72    /// This method will extract necessary attributes and validate them.
73    /// Can panic if attributes are missing, similar to original code's expect().
74    fn from_element(element: &Element) -> Self
75    where
76        Self: Sized;
77
78    /// Generates the Rust `TokenStream` for this specific AST node.
79    /// The `ignore` vector is passed directly to allow modification by calling nodes (e.g. for <foreach> scope).
80    fn generate_tokens<FChildParser>(&self, context: &mut NodeContext<FChildParser>, ignore: &mut Vec<String>) -> TokenStream
81    where
82        FChildParser: FnMut(&[Element], &mut TokenStream, &mut Vec<String>, &str) -> TokenStream;
83}