Add helper functions for PRIVMSG, MODE, and JOIN

This commit is contained in:
Yash Karandikar 2021-11-04 13:26:54 -05:00
parent af7a97644d
commit ab4c1dd010
Signed by: karx
GPG key ID: A794DA2529474BA5

View file

@ -295,6 +295,78 @@ impl Client {
self.write(&computed)?;
Ok(())
}
// Utility functions!
/// Helper function for sending PRIVMSGs.
/// This makes
/// ```no_run
/// # use circe::*;
/// # let mut client = Client::new(Config::from_toml("config.toml")?)?;
/// client.send_privmsg("#main", "Hello")?;
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// equivalent to
///
/// ```no_run
/// # use circe::*;
/// # let mut client = Client::new(Config::from_toml("config.toml")?)?;
/// client.write_command(Command::PRIVMSG("#main".to_string(), "Hello".to_string()))?;
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn send_privmsg(&mut self, channel: &str, message: &str) -> Result<(), Error> {
self.write_command(Command::PRIVMSG(channel.to_string(), message.to_string()))?;
Ok(())
}
/// Helper function for sending JOINs.
/// This makes
/// ```no_run
/// # use circe::*;
/// # let mut client = Client::new(Config::from_toml("config.toml")?)?;
/// client.send_join("#main")?;
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// equivalent to
///
/// ```no_run
/// # use circe::*;
/// # let mut client = Client::new(Config::from_toml("config.toml")?)?;
/// client.write_command(Command::JOIN("#main".to_string()))?;
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn send_join(&mut self, channel: &str) -> Result<(), Error> {
self.write_command(Command::JOIN(channel.to_string()))?;
Ok(())
}
/// Helper function for sending MODEs.
/// This makes
/// ```no_run
/// # use circe::*;
/// # let mut client = Client::new(Config::from_toml("config.toml")?)?;
/// client.send_mode("test", Some("+B"))?;
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// equivalent to
///
/// ```no_run
/// # use circe::*;
/// # let mut client = Client::new(Config::from_toml("config.toml")?)?;
/// client.write_command(Command::MODE("test".to_string(), Some("+B".to_string())))?;
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn send_mode(&mut self, target: &str, mode: Option<&str>) -> Result<(), Error> {
if let Some(mode) = mode {
self.write_command(Command::MODE(target.to_string(), Some(mode.to_string())))?;
} else {
self.write_command(Command::MODE(target.to_string(), None))?;
}
Ok(())
}
}
impl Config {