Skip to main content

TusksModule

Struct TusksModule 

Source
pub struct TusksModule {
    pub name: Ident,
    pub attrs: Attributes,
    pub external_parent: Option<ExternalModule>,
    pub parameters: Option<TusksParameters>,
    pub tusks: Vec<Tusk>,
    pub submodules: Vec<TusksModule>,
    pub external_modules: Vec<ExternalModule>,
    pub allow_external_subcommands: bool,
    pub value_enums: Vec<ItemEnum>,
}
Expand description

Represents a tusks module with all its elements

Fields§

§name: Ident

Name of the module (e.g. “tasks”, “sub1”)

§attrs: Attributes§external_parent: Option<ExternalModule>

The parent annotated as pub use … as parent_

§parameters: Option<TusksParameters>

The Parameters struct (if not existing during parse it will be generated)

§tusks: Vec<Tusk>

List of all public functions

§submodules: Vec<TusksModule>

List of all pub sub-modules (recursive)

§external_modules: Vec<ExternalModule>

List of all external modules (pub use … as …)

§allow_external_subcommands: bool

if #[command(allow_external_subcommands=true)] is set

§value_enums: Vec<ItemEnum>

Enum definitions (e.g. #[derive(ValueEnum)]) to re-export in generated cli module

Implementations§

Source§

impl TusksModule

Source

pub fn from_module( module: ItemMod, is_tusks_root: bool, is_root: bool, ) -> Result<Option<Self>>

Parses a syn::ItemMod into a TusksModule

Source§

impl TusksModule

Internal helpers for CLI code generation.

Source

pub fn is_parameters_type(ty: &Type, params_ident: &Ident) -> bool

Check if a type is a reference to a parameters struct

Source§

impl TusksModule

Internal helpers for parameter supplementation.

Source

pub fn extract_lifetime(item_struct: &ItemStruct) -> Result<Lifetime>

Extract the first lifetime parameter from a struct

Source§

impl TusksModule

Internal helpers for handle_matches code generation.

Source

pub fn build_match_arms_recursive(&self, path: &ModulePath) -> Vec<TokenStream>

Build match arms recursively with path tracking

Source

pub fn build_no_command_error(path: &ModulePath) -> TokenStream

Source

pub fn tusk_has_parameters_arg(&self, tusk: &Tusk) -> bool

Source§

impl TusksModule

Source

pub fn build_function_match_arm( &self, tusk: &Tusk, cli_path: &TokenStream, path: &ModulePath, ) -> TokenStream

Generates a match arm for a command function.

For fn my_func(arg1: String, arg2: i32) this produces:

Some(cli::Commands::my_func { arg1: p1, arg2: p2 }) => {
    super::my_func(p1.clone(), p2.clone());
}
Source

pub fn build_default_function_match_arm( &self, tusk: &Tusk, path: &ModulePath, is_external_subcommand_case: bool, ) -> TokenStream

Source

pub fn build_external_subcommand_match_arm( &self, tusk: &Tusk, path: &ModulePath, ) -> TokenStream

Source

pub fn build_pattern_fields( &self, pattern_bindings: &[(Ident, Ident)], ) -> Vec<TokenStream>

Converts bindings to pattern fields [field: p1, field: p2, ...], filtering out generated fields.

Source§

impl TusksModule

Source

pub fn build_submodule_match_arm( &self, cli_path: &TokenStream, path: &ModulePath, ) -> TokenStream

Generates a match arm for a submodule. The complete generated pattern:

Some(cli::Commands::admin { user: p1, sub }) => {
    let super_parameters = &parameters;
    let parameters = super::admin::Parameters { user: p1, super_: super_parameters, .. };
    match sub {
        // ... nested arms ...
    }
}
Source§

impl TusksModule

Source

pub fn extract_attributes<'a>(&'a self, names: &[&str]) -> Vec<&'a Attribute>

Source§

impl TusksModule

Source

pub fn generate_command_attribute(&self) -> TokenStream

Example 1 - Root module of tusks

#[command(name = "tasks")] /// <===== here =====
    pub struct Cli {
        #[arg(long)]
        pub root_param: String,
        #[arg(short, long)]
        pub verbose: bool,
        #[command(subcommand)] /// see generate_command_attribute_for_subcommands
        pub sub: Option<Commands>,
    }

Example 2 - This is a submodule-subcommand:

pub enum Commands {
    /// ... other subcommands and submodule-subcommands
    #[command(name = "level1")] /// <===== here =====
    #[allow(non_camel_case_types)]
    level1 {
        #[arg(long)]
        level1_field: Option<String>,
        #[arg(long, default_value = "42")]
        level1_number: i32,
        #[command(subcommand)] /// see generate_command_attribute_for_subcommands
        sub: Option<level1::Commands>,
    },
}
Source

pub fn generate_command_attribute_for_subcommands(&self) -> TokenStream

Example 1 - Root module of tusks

#[command(name = "tasks")] /// see generate_command_attribute
    pub struct Cli {
        #[arg(long)]
        pub root_param: String,
        #[arg(short, long)]
        pub verbose: bool,
        #[command(subcommand)] /// <===== here =====
        pub sub: Option<Commands>,
    }

Example 2 - This is a submodule-subcommand:

pub enum Commands {
    /// ... other subcommands and submodule-subcommands
    #[command(name = "level1")] /// see generate_command_attribute
    #[allow(non_camel_case_types)]
    level1 {
        #[arg(long)]
        level1_field: Option<String>,
        #[arg(long, default_value = "42")]
        level1_number: i32,
        #[command(subcommand)] /// <===== here =====
        sub: Option<level1::Commands>,
    },
}
Source

pub fn generate_command_attribute_for_external_subcommands(&self) -> TokenStream

Example:

pub enum Commands {
    // ... other non-external subcommands ...
    #[command(flatten)]
    TuskExternalCommands(ExternalCommands),
}

Trait Implementations§

Source§

impl CliCodegen for TusksModule

Source§

fn build_cli(&self, path: Vec<&Ident>, debug: bool) -> TokenStream

Source§

impl Debug for TusksModule

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl HandleMatchesCodegen for TusksModule

Source§

fn build_handle_matches(&self, is_tusks_root: bool) -> TokenStream

Source§

impl ParametersCodegen for TusksModule

Source§

fn supplement_parameters( &mut self, module: &mut ItemMod, is_tusks_root: bool, derive_debug: bool, ) -> Result<()>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.