Skip to main content

tusks_lib/
models.rs

1use syn::{Attribute, Ident, ItemFn, ItemStruct, PathSegment, spanned::Spanned};
2
3#[derive(Default)]
4pub struct Attributes(pub Vec<Attribute>);
5
6/// Represents a tusks module with all its elements
7pub struct TusksModule {
8    /// Name of the module (e.g. "tasks", "sub1")
9    pub name: Ident,
10
11    pub attrs: Attributes,
12
13    /// The parent annotated as pub use ... as parent_
14    pub external_parent: Option<ExternalModule>,
15    
16    /// The Parameters struct (if not existing during parse it will be generated)
17    pub parameters: Option<TusksParameters>,
18    
19    /// List of all public functions
20    pub tusks: Vec<Tusk>,
21    
22    /// List of all pub sub-modules (recursive)
23    pub submodules: Vec<TusksModule>,
24    
25    /// List of all external modules (pub use ... as ...)
26    pub external_modules: Vec<ExternalModule>,
27
28    /// if #[command(allow_external_subcommands=true)] is set
29    pub allow_external_subcommands: bool,
30
31    /// Enum definitions (e.g. #[derive(ValueEnum)]) to re-export in generated cli module
32    pub value_enums: Vec<syn::ItemEnum>,
33}
34
35impl std::fmt::Debug for TusksModule {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        f.debug_struct("TusksModule")
38            .field("name", &self.name.to_string())
39            .field("parameters", &self.parameters)
40            .field("tusks", &self.tusks)
41            .field("submodules", &self.submodules)
42            .field("external_modules", &self.external_modules)
43            .field("allow_external_subcommands", &self.allow_external_subcommands)
44            .field("value_enums_count", &self.value_enums.len())
45            .finish()
46    }
47}
48
49/// Represents a parameters struct
50pub struct TusksParameters {
51    /// The underlying struct
52    pub pstruct: ItemStruct,
53}
54
55/// Represents a command function (tusk)
56pub struct Tusk {
57    /// The underlying function
58    pub func: ItemFn,
59
60    pub is_default: bool,
61
62    pub is_async: bool,
63}
64
65/// Represents an externally imported module
66pub struct ExternalModule {
67    /// The alias name (e.g. "sub2")
68    pub alias: Ident,
69    
70    /// The original pub use statement that created this external module
71    pub item_use: syn::ItemUse,
72}
73
74impl std::fmt::Debug for TusksParameters {
75    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76        f.debug_struct("TusksParameters")
77            .field("name", &self.pstruct.ident.to_string())
78            .finish()
79    }
80}
81
82impl std::fmt::Debug for Tusk {
83    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84        f.debug_struct("Tusk")
85            .field("name", &self.func.sig.ident.to_string())
86            .finish()
87    }
88}
89
90impl std::fmt::Debug for ExternalModule {
91    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92        f.debug_struct("ExternalModule")
93            .field("alias", &self.alias.to_string())
94            .field("item_use", &format!("ItemUse at span: {:?}", self.item_use.span()))
95            .finish()
96    }
97}
98
99impl std::fmt::Debug for Attributes {
100    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
101        f.debug_struct("Attributes")
102            .field("count", &self.0.len())
103            .field(
104                "attributes",
105                &self.0.iter().map(|attr| {
106                    let path_str = attr
107                        .path()
108                        .segments
109                        .iter()
110                        .map(|seg: &PathSegment| seg.ident.to_string())
111                        .collect::<Vec<_>>()
112                        .join("::");
113
114                    format!("{} at span: {:?}", path_str, attr.span())
115                }).collect::<Vec<String>>(),
116            )
117            .finish()
118    }
119}