pub trait ListenerConfig:
Send
+ Sync
+ Serialize
+ Deserialize {
// Required methods
fn protocol(&self) -> &str;
fn address(&self) -> &str;
fn build(&self) -> Box<dyn ChatListener>;
fn clone_box(&self) -> Box<dyn ListenerConfig>;
fn debug_fmt(&self, f: &mut Formatter<'_>) -> Result;
}Expand description
Per-listener configuration trait.
Each protocol provides its own config struct that implements this trait.
The trait is serde-compatible via typetag, so Vec<Box<dyn ListenerConfig>> can be serialized and deserialized from JSON, TOML, etc.
§Implementing a custom listener config
ⓘ
use chat_system::config::ListenerConfig;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct MyListenerConfig { pub address: String }
#[typetag::serde(name = "my-protocol")]
impl ListenerConfig for MyListenerConfig {
fn protocol(&self) -> &str { "my-protocol" }
fn address(&self) -> &str { &self.address }
fn build(&self) -> Box<dyn chat_system::server::ChatListener> { todo!() }
fn clone_box(&self) -> Box<dyn ListenerConfig> { Box::new(self.clone()) }
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self, f)
}
}Required Methods§
Sourcefn build(&self) -> Box<dyn ChatListener>
fn build(&self) -> Box<dyn ChatListener>
Construct a concrete ChatListener from
this config.
Sourcefn clone_box(&self) -> Box<dyn ListenerConfig>
fn clone_box(&self) -> Box<dyn ListenerConfig>
Clone this config into a new boxed trait object.