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
impl RouterBuilder
Sourcepub fn from_routes(
routes: Vec<(String, Box<dyn ProducerTrait>, DeserializerFn)>,
) -> Self
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
§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?;Sourcepub fn add_route(
self,
resource_id: Arc<str>,
producer: Box<dyn ProducerTrait>,
deserializer: DeserializerFn,
) -> Self
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 ProducerTraitdeserializer- Function to deserialize bytes to the target type
§Resource ID Memory Management
The resource_id is stored as Arc
- String literal:
Arc::from("sensors/temperature") - Owned String:
Arc::from(string.as_str())
Sourcepub fn build(self) -> Router
pub fn build(self) -> Router
Build the router
Consumes the builder and returns a configured Router.
Sourcepub fn route_count(&self) -> usize
pub fn route_count(&self) -> usize
Get the number of routes that will be created
Trait Implementations§
Auto Trait Implementations§
impl Freeze for RouterBuilder
impl !RefUnwindSafe for RouterBuilder
impl Send for RouterBuilder
impl Sync for RouterBuilder
impl Unpin for RouterBuilder
impl !UnwindSafe for RouterBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more