MCPcopy Index your code
hub / github.com/AzureMarker/shaku

github.com/AzureMarker/shaku @v0.6.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.6.2 ↗ · + Follow
469 symbols 743 edges 98 files 52 documented · 11% updated 49d agov0.6.2 · 2024-08-31★ 5926 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Current version Current documentation Build status

Shaku

Shaku is a compile time dependency injection Rust library. See the docs for more details, including a getting started guide.

Guides

Example

use shaku::{module, Component, Interface, HasComponent};
use std::sync::Arc;

trait Logger: Interface {
    fn log(&self, content: &str);
}

trait DateLogger: Interface {
    fn log_date(&self);
}

#[derive(Component)]
#[shaku(interface = Logger)]
struct LoggerImpl;

impl Logger for LoggerImpl {
    fn log(&self, content: &str) {
        println!("{}", content);
    }
}

#[derive(Component)]
#[shaku(interface = DateLogger)]
struct DateLoggerImpl {
    #[shaku(inject)]
    logger: Arc<dyn Logger>,
    today: String,
    year: usize,
}

impl DateLogger for DateLoggerImpl {
    fn log_date(&self) {
        self.logger.log(&format!("Today is {}, {}", self.today, self.year));
    }
}

module! {
    MyModule {
        components = [LoggerImpl, DateLoggerImpl],
        providers = []
    }
}

fn main() {
    let module = MyModule::builder()
        .with_component_parameters::<DateLoggerImpl>(DateLoggerImplParameters {
            today: "Jan 26".to_string(),
            year: 2020
        })
        .build();

    let date_logger: &dyn DateLogger = module.resolve_ref();
    date_logger.log_date();
}

Component vs Provider

Component represents a single instance of a service, aka a singleton. Provider is more like a factory for instances. Each time a component is resolved you will get the same instance. Each time a provider is resolved you will get a new instance.

For more details on Component and Provider, see the getting started guide and the provider getting started guide.

Minimum Supported Rust Version

Shaku supports the latest stable release of Rust, plus the previous two versions at minimum (but possibly more). Changes to the minimum supported version will be noted in the changelog.

Minimum supported version: 1.38.0

Project Status

The foundation of shaku's API is in place, and now the focus is to mature the project based on user feedback. I (@AzureMarker) am active in the project, but I do not have many major changes of my own planned for the future. Most of the future changes will be based on user feedback.

Acknowledgements

This library started off as "he_di" (later renamed to "shaku") under the guidance of @bgbahoue and @U007D. Their work inspired the current maintainer (@AzureMarker) to continue the library from where they left off.

Extension points exported contracts — how you extend this code

Parser (Interface)
Generic parser for syn structures Note: Can't use `std::convert::From` here because we don't want to consume `T` [5 implementers]
shaku_derive/src/parser.rs
HasComponent (Interface)
Indicates that a module contains a component which implements the interface. [3 implementers]
shaku/src/component.rs
MyInterface (Interface)
(no doc) [3 implementers]
shaku/tests/override_component_fn.rs
MyComponent (Interface)
The MyComponent trait [1 implementers]
shaku_derive/tests/ui/doc_forwarding.rs
Animal (Interface)
(no doc) [2 implementers]
shaku/examples/simple_generic_component.rs
MyComponent (Interface)
(no doc) [1 implementers]
shaku_actix/tests/module_interface.rs
IOutput (Interface)
(no doc) [1 implementers]
shaku_actix/examples/autofac_actix/autofac.rs
MyComponent (Interface)
(no doc) [1 implementers]
shaku_rocket/tests/module_interface.rs

Core symbols most depended-on inside this repo

generate_name
called by 10
shaku_derive/src/macros/module.rs
build
called by 5
shaku/examples/simple_no_macros.rs
provide
called by 5
shaku/examples/simple_no_macros.rs
resolve_ref
called by 5
shaku/examples/simple_no_macros.rs
resolve
called by 4
shaku/examples/simple_no_macros.rs
submodule_names
called by 4
shaku_derive/src/macros/module.rs
build
called by 3
shaku/src/module/module_builder.rs
get_debug_level
called by 3
shaku_derive/src/debug.rs

Shape

Class 137
Interface 131
Function 114
Method 81
Enum 6

Languages

Rust100%

Modules by API surface

shaku_derive/src/macros/module.rs19 symbols
shaku/tests/extreme_submodules.rs16 symbols
shaku/tests/repository_service_provider.rs15 symbols
shaku/examples/simple_no_macros.rs12 symbols
shaku/tests/override_service.rs10 symbols
shaku/tests/lazy_components.rs10 symbols
shaku/tests/circular_dependency_runtime.rs10 symbols
shaku_derive/src/structures/module.rs9 symbols
shaku/tests/override_component_fn.rs9 symbols
shaku/tests/multiple_submodules.rs9 symbols
shaku_derive/tests/compile_two_derives.rs8 symbols
shaku/tests/shared_dependency.rs8 symbols

For agents

$ claude mcp add shaku \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact