Skip to main content

SmsRouter

Struct SmsRouter 

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

Routes SMS sends to a named provider without requiring the caller to know about individual provider crate types.

This is the unified dispatch client that eliminates boilerplate in consumer code. Instead of matching on a provider enum and constructing the right client, register each provider once and then call send_via with a name.

SmsRouter also implements SmsClient itself, forwarding to a configured default provider.

§Example

use sms_core::{SmsRouter, SendRequest};

let router = SmsRouter::new()
    .with("plivo", plivo_client)
    .with("aws-sns", sns_client)
    .default_provider("plivo");

// Explicit dispatch:
router.send_via("aws-sns", SendRequest { .. }).await?;

// Or use the SmsClient impl (goes to the default):
router.send(SendRequest { .. }).await?;

Implementations§

Source§

impl SmsRouter

Source

pub fn new() -> Self

Create an empty router with no providers registered.

Source

pub fn with( self, name: impl Into<String>, client: impl SmsClient + 'static, ) -> Self

Register a provider under the given name.

If this is the first provider added it automatically becomes the default (override with default_provider).

Source

pub fn with_arc( self, name: impl Into<String>, client: Arc<dyn SmsClient>, ) -> Self

Register a provider that is already behind an Arc.

Source

pub fn default_provider(self, name: impl Into<String>) -> Self

Set which provider name is used when calling the SmsClient trait impl directly (i.e. router.send(..)).

Source

pub async fn send_via( &self, provider: &str, req: SendRequest<'_>, ) -> Result<SendResponse, SmsError>

Send a message through a specific named provider.

Source

pub fn has_provider(&self, name: &str) -> bool

Returns true if a provider with the given name is registered.

Source

pub fn default_provider_name(&self) -> Option<&str>

Returns the name of the current default provider, if any.

Trait Implementations§

Source§

impl Clone for SmsRouter

Source§

fn clone(&self) -> SmsRouter

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for SmsRouter

Source§

fn default() -> Self

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

impl SmsClient for SmsRouter

Source§

fn send<'life0, 'life1, 'async_trait>( &'life0 self, req: SendRequest<'life1>, ) -> Pin<Box<dyn Future<Output = Result<SendResponse, SmsError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Send through the default provider.

Returns SmsError::Invalid if no default has been set.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.