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 include_tag_node;
12pub mod insert_tag_node;
13pub mod mapper_tag_node;
14pub mod otherwise_tag_node;
15pub mod select_tag_node;
16pub mod set_tag_node;
17pub mod sql_tag_node;
18pub mod trim_tag_node;
19pub mod update_tag_node;
20pub mod when_tag_node;
21pub mod where_tag_node;
22
23// Re-export all node structs for easier access
24pub use bind_tag_node::BindTagNode;
25pub use break_tag_node::BreakTagNode;
26pub use choose_tag_node::ChooseTagNode;
27pub use continue_tag_node::ContinueTagNode;
28pub use delete_tag_node::DeleteTagNode;
29pub use foreach_tag_node::ForeachTagNode;
30pub use if_tag_node::IfTagNode;
31pub use include_tag_node::IncludeTagNode;
32pub use insert_tag_node::InsertTagNode;
33pub use mapper_tag_node::MapperTagNode;
34pub use otherwise_tag_node::OtherwiseTagNode;
35pub use select_tag_node::SelectTagNode;
36pub use set_tag_node::SetTagNode;
37pub use sql_tag_node::SqlTagNode;
38pub use trim_tag_node::TrimTagNode;
39pub use update_tag_node::UpdateTagNode;
40pub use when_tag_node::WhenTagNode;
41pub use where_tag_node::WhereTagNode;
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(
61        &mut self,
62        children: &[Element],
63        ignore: &mut Vec<String>,
64    ) -> TokenStream {
65        (self.child_parser)(children, self.methods, ignore, self.fn_name)
66    }
67}
68
69/// Trait for all HTML abstract syntax tree (AST) nodes.
70/// Defines how a node is created from an `Element` and how it generates Rust TokenStream.
71pub trait HtmlAstNode {
72    /// Returns the XML tag name for this node type (e.g., "if", "select").
73    fn node_tag_name() -> &'static str;
74
75    /// Creates an instance of the node from a generic `Element`.
76    /// This method will extract necessary attributes and validate them.
77    /// Can panic if attributes are missing, similar to original code's expect().
78    fn from_element(element: &Element) -> Self
79    where
80        Self: Sized;
81
82    /// Generates the Rust `TokenStream` for this specific AST node.
83    /// The `ignore` vector is passed directly to allow modification by calling nodes (e.g. for <foreach> scope).
84    fn generate_tokens<FChildParser>(
85        &self,
86        context: &mut NodeContext<FChildParser>,
87        ignore: &mut Vec<String>,
88    ) -> TokenStream
89    where
90        FChildParser: FnMut(&[Element], &mut TokenStream, &mut Vec<String>, &str) -> TokenStream;
91}