lxy 0.1.1

A convenient async http and RPC framework in Rust
Documentation
use std::sync::Arc;

use crate::{ErrorCode, app::get_app, define_error, error::Result};

define_error!(ErrorCode::Internal, ResolveError);

/// Helper function to inject a dependency from the DI container within route handlers
///
/// This function retrieves a service of type `T` from the dependency injection container.
/// It uses the task-local `App` instance set up by the `AppLayer`.
///
/// # Example
///
/// ```rust
/// use lxy::utils::inject;
/// use lxy::Result;
///
/// struct MyService;
///
/// async fn my_handler() -> Result<()>  {
///     // Inject a service from the DI container
///     let my_service = inject::<MyService>()?;
///     
///     // do with my_service
///     // ...
///     Ok(())
/// }
/// ```
///
/// # Errors
///
/// Returns an error if:
/// - The factory of type `T` is not registered in the container
/// - The dependencies of type `T` is not registered in the container
/// - This function is called outside of a route handler context (where App is not available)
pub fn inject<T>() -> Result<Arc<T>>
where
  T: 'static,
{
  get_app()?.get::<T>()
}

/// Helper macro for injecting dependencies with less boilerplate
///
/// # Example
///
/// ```rust
/// use lxy::inject;
/// use lxy::Result;
///
/// struct MyService;
///
/// async fn my_handler() -> Result<()> {
///     let my_service = inject!(MyService)?;
///
///     // use my_service
///     // ...
///     Ok(())
/// }
/// ```
#[macro_export]
macro_rules! inject {
  ($t:ty) => {
    $crate::utils::inject::inject::<$t>()
  };
}