diff --git a/Cargo.lock b/Cargo.lock index c99bcc8..e558e0e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,3 +5,69 @@ version = 3 [[package]] name = "irsc" version = "0.1.0" +dependencies = [ + "serde", + "serde_derive", + "toml", +] + +[[package]] +name = "proc-macro2" +version = "1.0.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba508cc11742c0dc5c1659771673afbab7a0efab23aa17e854cbab0837ed0b43" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" + +[[package]] +name = "serde_derive" +version = "1.0.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "1.0.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2afee18b8beb5a596ecb4a2dce128c719b4ba399d34126b9e4396e3f9860966" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "toml" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +dependencies = [ + "serde", +] + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" diff --git a/Cargo.toml b/Cargo.toml index aad6b7b..cbc6307 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] - +toml = "0.5.8" +serde_derive = "1.0.130" +serde = "1.0.130" \ No newline at end of file diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..2ee957f --- /dev/null +++ b/config.toml @@ -0,0 +1,3 @@ +username = "test" +mode = "+B" +channels = ["#main", "#fsoc"] \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 01ee539..eae7b09 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,18 +1,22 @@ use std::borrow::Cow; +use std::fs::File; use std::io::{Error, Read, Write}; use std::net::TcpStream; +use std::path::Path; + +use serde_derive::Deserialize; pub struct Client { stream: TcpStream, config: Config, } -#[derive(Clone)] +#[derive(Clone, Deserialize, Default)] pub struct Config { username: String, - nickname: String, - mode: String, - channels: Box<[&'static str]>, + nickname: Option, + mode: Option, + channels: Box<[String]>, } #[derive(Debug)] @@ -61,7 +65,11 @@ impl Client { "*".into(), self.config.username.clone(), ))?; - self.write_command(Command::NICK(self.config.nickname.clone()))?; + if let Some(nick) = self.config.nickname.clone() { + self.write_command(Command::NICK(nick))?; + } else { + self.write_command(Command::NICK(self.config.username.clone()))?; + } loop { if let Ok(ref command) = self.read() { @@ -78,9 +86,7 @@ impl Client { } let config = self.config.clone(); - if config.mode != "" { - self.write_command(Command::MODE(config.username, Some(config.mode)))?; - } + self.write_command(Command::MODE(config.username, config.mode))?; for channel in config.channels.iter() { self.write_command(Command::JOIN(channel.to_string()))?; } @@ -161,7 +167,7 @@ impl Client { Cow::Owned(formatted) as Cow } OTHER(_) => { - return Err(std::io::Error::new( + return Err(Error::new( std::io::ErrorKind::Other, "Cannot write commands of type OTHER", )); @@ -177,15 +183,27 @@ impl Client { impl Config { pub fn new( username: &str, - nickname: Option<&str>, - mode: Option<&str>, - channels: Box<[&'static str]>, + nickname: Option, + mode: Option, + channels: Box<[String]>, ) -> Self { Self { username: username.into(), - nickname: nickname.unwrap_or(username).into(), - mode: mode.unwrap_or("").into(), - channels: channels.into(), + nickname, + mode, + channels, } } + + pub fn from_toml>(path: P) -> Result { + let mut file = File::open(&path)?; + let mut data = String::new(); + file.read_to_string(&mut data)?; + + toml::from_str(&data).map_err(|e| { + use std::io::ErrorKind; + + Error::new(ErrorKind::Other, format!("Invalid TOML: {}", e)) + }) + } } diff --git a/src/main.rs b/src/main.rs index 3aa1d1c..35f7b66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,14 +10,8 @@ macro_rules! loop_n { } fn main() -> Result<(), std::io::Error> { - let config = Config::new( - "test", - Some("test"), - Some("+B"), - Box::new(["#main", "#main2"]), - ); - let mut client = - Client::new("192.168.1.28", 6667, config).expect("Unable to connect to IRC server"); + let config = Config::from_toml("config.toml")?; + let mut client = Client::new("192.168.1.28", 6667, config)?; client.identify()?; loop {