Skip to main content

rbatis_codegen/codegen/syntax_tree_html/
mod.rs

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