Skip to main content

service_factory

Attribute Macro service_factory 

Source
#[service_factory]
Expand description

Attribute macro for service factory registration.

Automatically registers a service factory function with the inventory system, following the same pattern as #[register_scopes] for authorization scopes and DriverFactory for storage drivers.

§Usage

ⓘ
use hyprstream_service::ServiceContext;
use hyprstream_rpc::service::Spawnable;
use hyprstream_rpc_derive::service_factory;

#[service_factory("policy")]
fn create_policy_service(ctx: &ServiceContext) -> anyhow::Result<Box<dyn Spawnable>> {
    // Services include infrastructure and are directly Spawnable
    let policy = PolicyService::new(
        ...,
        ctx.zmq_context(),
        ctx.transport("policy", SocketKind::Rep),
        ctx.verifying_key(),
    );
    Ok(Box::new(policy))
}

§Generated Code

The macro generates:

ⓘ
fn create_policy_service(ctx: &ServiceContext) -> anyhow::Result<Box<dyn Spawnable>> {
    // ... original function body ...
}

inventory::submit! {
    hyprstream_service::ServiceFactory::new(
        "policy",
        create_policy_service
    )
}

§Benefits

  • ✅ DRY: Service factory declared once, auto-registered
  • ✅ Decentralized: Services own their registration (not in main.rs)
  • ✅ Self-documenting: All services discoverable via list_factories()
  • ✅ Matches existing patterns: Same as #[register_scopes] and DriverFactory