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