rust-irc/examples/multiserver.rs
John-John Tedro 5189b69e7e Simplify Config structure
This simplifies some of the `Config` structure, in particular this
means:

Parameters which are meaningfully equivalent longer stored in
an `Option<T>`, an example of this is `channels`. If you don't
want to join any channels you simply leave it as empty instead.
In effect, `None` is behaviorally equivalent to `vec![]`.

We don't allocate when accessing certain configuration options.
For example, when accessing `channels` we used to allocate a
vector to handle the "empty case", we simply return the slice
corresponding to the list of channels instead.

We skip serializing empty or optional configuration fields.
From a deserialization perspective this is already something
that was mostly supported through use of `Option<T>` and
`#[serde(default)]`.
2019-12-27 17:12:46 +01:00

60 lines
1.6 KiB
Rust

extern crate irc;
use futures::prelude::*;
use irc::{client::prelude::*, error};
#[tokio::main]
async fn main() -> irc::error::Result<()> {
env_logger::init();
let cfg1 = Config {
nickname: Some("pickles".to_owned()),
server: Some("irc.mozilla.org".to_owned()),
channels: vec!["#rust-spam".to_owned()],
..Default::default()
};
let cfg2 = Config {
nickname: Some("bananas".to_owned()),
server: Some("irc.mozilla.org".to_owned()),
channels: vec!["#rust-spam".to_owned()],
..Default::default()
};
let configs = vec![cfg1, cfg2];
let mut senders = Vec::new();
let mut streams = Vec::new();
for config in configs {
// Immediate errors like failure to resolve the server's domain or to establish any connection will
// manifest here in the result of prepare_client_and_connect.
let mut client = Client::from_config(config).await?;
client.identify()?;
senders.push(client.sender());
streams.push(client.stream()?);
}
loop {
let (message, index, _) =
futures::future::select_all(streams.iter_mut().map(|s| s.select_next_some())).await;
let message = message?;
let sender = &senders[index];
process_msg(sender, message)?;
}
}
fn process_msg(sender: &Sender, message: Message) -> error::Result<()> {
// print!("{}", message);
match message.command {
Command::PRIVMSG(ref target, ref msg) => {
if msg.contains("pickles") {
sender.send_privmsg(target, "Hi!")?;
}
}
_ => (),
}
Ok(())
}