RouterBuilder

Struct RouterBuilder 

Source
pub struct RouterBuilder { /* private fields */ }
Expand description

Builder for constructing routers

Provides a fluent API for adding routes before creating the router.

§Example

use aimdb_core::router::RouterBuilder;

let router = RouterBuilder::new()
    .add_route(
        "sensors/temperature",
        producer_temp.clone(),
        Arc::new(|bytes| {
            serde_json::from_slice::<Temperature>(bytes)
                .map(|t| Box::new(t) as Box<dyn Any + Send>)
                .map_err(|e| e.to_string())
        })
    )
    .add_route(
        "sensors/humidity",
        producer_humidity.clone(),
        Arc::new(|bytes| {
            serde_json::from_slice::<Humidity>(bytes)
                .map(|h| Box::new(h) as Box<dyn Any + Send>)
                .map_err(|e| e.to_string())
        })
    )
    .build();

Implementations§

Source§

impl RouterBuilder

Source

pub fn new() -> Self

Create a new router builder

Source

pub fn from_routes( routes: Vec<(String, Box<dyn ProducerTrait>, DeserializerFn)>, ) -> Self

Create a router builder from a collection of routes

This is a convenience method for automatic router construction from AimDb::collect_inbound_routes(). The resource_ids are converted to Arc for proper memory management.

§Arguments
  • routes - Vector of (resource_id, producer, deserializer) tuples
§Example
let routes = db.collect_inbound_routes("mqtt");
let router = RouterBuilder::from_routes(routes).build();
connector.set_router(router).await?;
Source

pub fn add_route( self, resource_id: Arc<str>, producer: Box<dyn ProducerTrait>, deserializer: DeserializerFn, ) -> Self

Add a route to the router

§Arguments
  • resource_id - Resource identifier to match (as Arc)
  • producer - Producer that implements ProducerTrait
  • deserializer - Function to deserialize bytes to the target type
§Resource ID Memory Management

The resource_id is stored as Arc for proper reference counting and cleanup. You can create an Arc from:

  • String literal: Arc::from("sensors/temperature")
  • Owned String: Arc::from(string.as_str())
Source

pub fn build(self) -> Router

Build the router

Consumes the builder and returns a configured Router.

Source

pub fn route_count(&self) -> usize

Get the number of routes that will be created

Trait Implementations§

Source§

impl Default for RouterBuilder

Source§

fn default() -> Self

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

Auto Trait Implementations§

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.