ValidationBuilder

Struct ValidationBuilder 

Source
pub struct ValidationBuilder<State = Initial> { /* private fields */ }
Expand description

Compile-time validation context for DI registrations.

This struct tracks service registrations during the build process and performs validation to catch common configuration errors at compile time.

§Validation Rules

  • Singleton → Scoped: Error - Singleton services cannot depend on scoped services
  • Singleton → Transient: Warning - Singleton will hold the same transient instance forever
  • Scoped → Transient: OK - Scoped service gets new transient on each scope resolution
  • Missing Dependencies: Error - Required services not registered
  • Circular Dependencies: Error - Services that depend on each other in a cycle
  • Trait Mismatches: Error - Trait registrations without matching implementations

§Examples

use ferrous_di::{ServiceCollection, ValidationBuilder, Lifetime};

struct DatabaseConnection;
struct UserService;  
struct RequestContext;

// This will catch the error at compile time
let validation = ValidationBuilder::new()
    .register::<DatabaseConnection>(Lifetime::Singleton)
    .register::<UserService>(Lifetime::Singleton)
    .depends_on::<UserService, RequestContext>() // RequestContext is scoped!
    .register::<RequestContext>(Lifetime::Scoped)
    .validate();

// Compilation error: Singleton UserService cannot depend on Scoped RequestContext

Implementations§

Source§

impl ValidationBuilder<Initial>

Source

pub fn new() -> Self

Creates a new validation builder.

Source

pub fn register<T: 'static>(self, lifetime: Lifetime) -> Self

Registers a service with the specified lifetime.

Source

pub fn register_factory<T: 'static, F>(self, lifetime: Lifetime) -> Self
where F: Fn(&ResolverContext<'_>) -> T,

Registers a service factory with the specified lifetime.

Source

pub fn register_trait<T: ?Sized + 'static, I: 'static>( self, lifetime: Lifetime, ) -> Self

Registers a trait implementation.

Source

pub fn depends_on<T: 'static, D: 'static>(self) -> Self

Declares that service T depends on service D.

Source

pub fn validate(self) -> ValidationBuilder<Validated>

Performs validation and transitions to validated state.

Source

pub fn validate_runtime(self) -> ValidationResult

Performs runtime validation (for development/debugging).

Source§

impl ValidationBuilder<Validated>

Source

pub fn build(self) -> ServiceCollection

Builds the ServiceCollection with validated configuration.

Source

pub fn get_result(&self) -> ValidationResult

Gets the validation results.

Trait Implementations§

Source§

impl Default for ValidationBuilder<Initial>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<State> Freeze for ValidationBuilder<State>

§

impl<State> RefUnwindSafe for ValidationBuilder<State>
where State: RefUnwindSafe,

§

impl<State> Send for ValidationBuilder<State>
where State: Send,

§

impl<State> Sync for ValidationBuilder<State>
where State: Sync,

§

impl<State> Unpin for ValidationBuilder<State>
where State: Unpin,

§

impl<State> UnwindSafe for ValidationBuilder<State>
where State: UnwindSafe,

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.