From a312c3097f4e04f6faf0c093429c80a7953e9084 Mon Sep 17 00:00:00 2001 From: Yash Karandikar Date: Thu, 4 Nov 2021 11:43:49 -0500 Subject: [PATCH] Improve documentation --- src/lib.rs | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1bbf915..9452ced 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,18 @@ //! A simple IRC crate written in rust +//! ```rust +//! fn main() -> Result<(), std::io::Error> { +//! let config = Config::from_toml("config.toml")?; +//! let mut client = Client::new(config)?; +//! client.identify()?; +//! +//! loop { +//! if let Ok(ref command) = client.read() { +//! if let Command::OTHER(line) = command { +//! print!("{}", line); +//! } +//! } +//! } +//! } #![warn(missing_docs)] use std::borrow::Cow; @@ -40,7 +54,7 @@ pub enum Command { CAP(CapMode), /// Joins a channel /// ```rust - /// client.write_command(Command::PRIVMSG("#main".to_string()))?; + /// client.write_command(Command::JOIN("#main".to_string()))?; /// ``` JOIN( /// Channel @@ -50,6 +64,7 @@ pub enum Command { /// ```rust /// client.write_command(Command::MODE("#main".to_string(), Some("+B")))?; /// ``` + /// If the MODE is not given (e.g. None), then the client will send "MODE target" MODE( /// Channel String, @@ -60,9 +75,9 @@ pub enum Command { NICK(String), /// Everything that is not a command OTHER(String), - /// Ping another user + /// Ping another user or the server PING( - /// User + /// target String, ), #[doc(hidden)] @@ -99,15 +114,19 @@ impl Client { /// ```rust /// let mut client = Client::new(config)?; /// ``` + /// + /// Errors if the client could not connect to the given host. pub fn new(config: Config) -> Result { let stream = TcpStream::connect(format!("{}:{}", config.host, config.port))?; Ok(Self { stream, config }) } - /// Identify user and join the IRC + /// Identify user and join the specified channels /// ```rust /// client.identify()?; /// ``` + /// + /// Errors if the client could not write to the stream. pub fn identify(&mut self) -> Result<(), Error> { self.write_command(Command::CAP(CapMode::END))?; self.write_command(Command::USER( @@ -157,7 +176,7 @@ impl Client { Some(String::from_utf8_lossy(&buffer).into()) } - /// Read data comming from the IRC + /// Read data coming from the IRC as a [`Command`] /// ```rust /// if let Ok(ref command) = client.read() { /// if let Command::OTHER(line) = command { @@ -165,6 +184,8 @@ impl Client { /// } /// } /// ``` + /// + /// Errors if there are no new messages. This should not be taken as an actual Error, because nothing went wrong. pub fn read(&mut self) -> Result { if let Some(string) = self.read_string() { return Ok(Command::from_str(&string)); @@ -184,10 +205,12 @@ impl Client { Ok(()) } - /// Send a command to the IRC + /// Send a [`Command`] to the IRC /// ```rust /// client.write_command(Command::PRIVMSG("#main".to_string(), "Hello".to_string()))?; /// ``` + /// + /// Errors if the stream could not write. pub fn write_command(&mut self, command: Command) -> Result<(), Error> { use Command::*; let computed = match command { @@ -251,8 +274,8 @@ impl Config { ///
/// channels: Channels to join on the IRC
/// host: IP or domain of the IRC server
- /// mode: Mode to join the IRC with
- /// nickname: Nickname to join the IRC with
+ /// mode: Mode to join the IRC with (optional)
+ /// nickname: Nickname to join the IRC with (optional, defaults to the given username)
/// port: Port of the IRC server
/// username: Username to join the IRC with
/// ```rust @@ -296,6 +319,8 @@ impl Config { /// port = 6667 /// username = "IRSC" /// ``` + /// + /// Returns an Error if the file cannot be opened or if the TOML is invalid pub fn from_toml>(path: P) -> Result { let mut file = File::open(&path)?; let mut data = String::new();