From ab4c1dd01078efdc42e2359e1a61fce34b279511 Mon Sep 17 00:00:00 2001 From: Yash Karandikar Date: Thu, 4 Nov 2021 13:26:54 -0500 Subject: [PATCH] Add helper functions for PRIVMSG, MODE, and JOIN --- src/lib.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 2937ee9..b8b637d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 {