Skip to main content

ModuleTree

Struct ModuleTree 

Source
pub struct ModuleTree {
    pub name: String,
    pub vis: PureVis,
    pub uses: Vec<PureUse>,
    pub inner_attrs: Vec<PureAttribute>,
    pub items: Vec<PureItem>,
    pub children: Vec<ModuleTree>,
}
Expand description

A hierarchical module tree for source generation.

This structure represents a complete module hierarchy that can be transformed into source code by a SourceGenerator.

§Example

// Build a tree representing:
// mod utils {
//     use std::io;
//     pub fn helper() {}
// }

let tree = ModuleTree::new("utils")
    .with_vis(PureVis::Public)
    .with_use(PureUse { ... })
    .with_item(PureItem::Fn(helper_fn));

Fields§

§name: String

Module name (empty string for crate root).

§vis: PureVis

Module visibility.

§uses: Vec<PureUse>

Use statements for this module.

§inner_attrs: Vec<PureAttribute>

Inner attributes (e.g., //! doc, #![allow(...)]).

§items: Vec<PureItem>

Items in this module (structs, functions, etc.).

§children: Vec<ModuleTree>

Child modules.

Implementations§

Source§

impl ModuleTree

Source

pub fn new(name: impl Into<String>) -> Self

Create a new module tree with the given name.

For crate root, use an empty string or “crate”.

Source

pub fn crate_root() -> Self

Create a crate root module tree.

Source

pub fn with_vis(self, vis: PureVis) -> Self

Set visibility.

Source

pub fn with_use(self, use_stmt: PureUse) -> Self

Add a use statement.

Source

pub fn with_uses(self, uses: impl IntoIterator<Item = PureUse>) -> Self

Add multiple use statements.

Source

pub fn with_inner_attr(self, attr: PureAttribute) -> Self

Add an inner attribute.

Source

pub fn with_item(self, item: PureItem) -> Self

Add an item.

Source

pub fn with_items(self, items: impl IntoIterator<Item = PureItem>) -> Self

Add multiple items.

Source

pub fn with_child(self, child: ModuleTree) -> Self

Add a child module.

Source

pub fn with_children( self, children: impl IntoIterator<Item = ModuleTree>, ) -> Self

Add multiple child modules.

Source

pub fn is_crate_root(&self) -> bool

Check if this is the crate root.

Source

pub fn total_items(&self) -> usize

Get total item count (including children).

Source

pub fn total_modules(&self) -> usize

Get total module count (including self and children).

Source

pub fn find_child(&self, name: &str) -> Option<&ModuleTree>

Find a child module by name.

Source

pub fn find_child_mut(&mut self, name: &str) -> Option<&mut ModuleTree>

Find a child module by name (mutable).

Source

pub fn get_or_create_child(&mut self, name: &str) -> &mut ModuleTree

Get or create a child module by name.

If a child with the given name exists, returns a mutable reference to it. Otherwise, creates a new child module and returns a mutable reference.

Source

pub fn navigate(&self, path: &[&str]) -> Option<&ModuleTree>

Navigate to a nested module by path.

Path is a slice of module names, e.g., ["foo", "bar"] for foo::bar.

Source

pub fn navigate_mut(&mut self, path: &[&str]) -> Option<&mut ModuleTree>

Navigate to a nested module by path (mutable).

Source

pub fn get_or_create_path(&mut self, path: &[&str]) -> &mut ModuleTree

Get or create nested modules by path.

Creates any missing intermediate modules.

Trait Implementations§

Source§

impl Clone for ModuleTree

Source§

fn clone(&self) -> ModuleTree

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ModuleTree

Source§

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

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

impl Default for ModuleTree

Source§

fn default() -> ModuleTree

Returns the “default value” for a type. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.