MCPcopy Index your code
hub / github.com/Nashenas88/coi

github.com/Nashenas88/coi @release-0.10.3

Chat with this repo
repository ↗ · DeepWiki ↗ · release release-0.10.3 ↗ · + Follow
170 symbols 264 edges 18 files 7 documented · 4%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

coi

Build Status docs.rs crates.io

Dependency Injection in Rust

The goal of this crate is to provide a simple dependency injection framework that is easy to use. Performance is not an initial concern, but might be later on as the crate matures.

Example

use coi::{container, Inject};
use std::sync::Arc;

// Inherit `Inject` on all traits you'd like to inject
pub trait Trait1: Inject {
    fn describe(&self) -> &'static str;
}

// derive `Inject` on all structs that will provide the implementation
#[derive(Inject)]
#[coi(provides dyn Trait1 with Impl1)]
struct Impl1;

// actually impl the trait
impl Trait1 for Impl1 {
    fn describe(&self) -> &'static str {
        "I'm impl1!"
    }
}

pub trait Trait2: Inject {
    fn deep_describe(&self) -> String;
}

#[derive(Inject)]
#[coi(provides dyn Trait2 with Impl2::new(trait1))]
struct Impl2 {
    // inject dependencies by Arc<dyn SomeTrait>
    #[coi(inject)]
    trait1: Arc<dyn Trait1>,
}

impl Impl2 {
    fn new(trait1: Arc<dyn Trait1>) -> Self {
        Self { trait1 }
    }
}

impl Trait2 for Impl2 {
    fn deep_describe(&self) -> String {
        format!("I'm impl2! and I have {}", self.trait1.describe())
    }
}

// It even works on structs
#[derive(Debug, Inject)]
#[coi(provides JustAStruct with JustAStruct)]
pub struct JustAStruct;

fn main() {
    // Then construct your container with the helper `container!` macro
    let container = container!{
        trait1 => Impl1Provider,
        trait2 => Impl2Provider; scoped,
        struct => JustAStructProvider; singleton
    };

    // And resolve away!
    let trait2 = container
        .resolve::<dyn Trait2>("trait2")
        .expect("Should exist");
    println!("Deep description: {}", trait2.as_ref().deep_describe());
    let a_struct = container
        .resolve::<JustAStruct>("struct")
        .expect("Should exist");
    println!("Got struct! {:?}", a_struct);
}

Name

The name coi comes from an inversion of the initialism IoC (Inversion of Control).

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

SPDX-License-Identifier: MIT OR Apache-2.0

Extension points exported contracts — how you extend this code

Provide (Interface)
A trait to manage the construction of an injectable trait or struct. [8 implementers]
src/lib.rs
I (Interface)
(no doc) [1 implementers]
benches/bench.rs
Trait1 (Interface)
(no doc) [1 implementers]
coi-test-debug/tests/missing_deps.rs
Dep1 (Interface)
(no doc) [1 implementers]
coi-test/tests/scoped.rs
Trait1 (Interface)
(no doc) [1 implementers]
coi-test-no-derive/tests/main.rs
Inject (Interface)
A marker trait for injectable traits and structs. [5 implementers]
src/lib.rs
Trait2 (Interface)
(no doc) [1 implementers]
coi-test-debug/tests/missing_deps.rs
Dep2 (Interface)
(no doc) [1 implementers]
coi-test/tests/scoped.rs

Core symbols most depended-on inside this repo

scoped
called by 21
src/lib.rs
push
called by 14
coi-derive/src/ctxt.rs
get
called by 4
coi-derive/src/attr.rs
eq
called by 3
coi-derive/src/symbol.rs
as_ident
called by 3
coi-derive/src/symbol.rs
dependency_graph
called by 2
src/lib.rs
analyze
called by 2
src/lib.rs
build
called by 2
src/lib.rs

Shape

Class 48
Method 47
Function 44
Interface 27
Enum 4

Languages

Rust100%

Modules by API surface

src/lib.rs28 symbols
coi-test/tests/scoped.rs21 symbols
benches/bench.rs21 symbols
coi-derive/src/attr.rs16 symbols
coi-test-debug/tests/debug.rs14 symbols
coi-test-no-derive/tests/main.rs13 symbols
coi-test/tests/main.rs10 symbols
coi-test-debug/tests/cycle.rs8 symbols
coi-test-debug/tests/missing_deps.rs7 symbols
coi-test/tests/register_closure.rs6 symbols
coi-test/tests/multiple_providers.rs6 symbols
coi-derive/src/symbol.rs4 symbols

For agents

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

⬇ download graph artifact

Ask about this repo answers extend the page