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    pub is_async: bool,
47}
48
49/// Represents an externally imported module
50pub struct ExternalModule {
51    /// The alias name (e.g. "sub2")
52    pub alias: Ident,
53    
54    /// The original pub use statement that created this external module
55    pub item_use: syn::ItemUse,
56}
57
58impl std::fmt::Debug for TusksParameters {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        f.debug_struct("TusksParameters")
61            .field("name", &self.pstruct.ident.to_string())
62            .finish()
63    }
64}
65
66impl std::fmt::Debug for Tusk {
67    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68        f.debug_struct("Tusk")
69            .field("name", &self.func.sig.ident.to_string())
70            .finish()
71    }
72}
73
74impl std::fmt::Debug for ExternalModule {
75    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76        f.debug_struct("ExternalModule")
77            .field("alias", &self.alias.to_string())
78            .field("item_use", &format!("ItemUse at span: {:?}", self.item_use.span()))
79            .finish()
80    }
81}
82
83impl std::fmt::Debug for Attributes {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        f.debug_struct("Attributes")
86            .field("count", &self.0.len())
87            .field(
88                "attributes",
89                &self.0.iter().map(|attr| {
90                    let path_str = attr
91                        .path()
92                        .segments
93                        .iter()
94                        .map(|seg: &PathSegment| seg.ident.to_string())
95                        .collect::<Vec<_>>()
96                        .join("::");
97
98                    format!("{} at span: {:?}", path_str, attr.span())
99                }).collect::<Vec<String>>(),
100            )
101            .finish()
102    }
103}