Add ability to create Config using TOML
This commit is contained in:
parent
97e1d562b8
commit
21a9b21f42
66
Cargo.lock
generated
66
Cargo.lock
generated
|
@ -5,3 +5,69 @@ version = 3
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "irsc"
|
name = "irsc"
|
||||||
version = "0.1.0"
|
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"
|
||||||
|
|
|
@ -6,4 +6,6 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
toml = "0.5.8"
|
||||||
|
serde_derive = "1.0.130"
|
||||||
|
serde = "1.0.130"
|
3
config.toml
Normal file
3
config.toml
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
username = "test"
|
||||||
|
mode = "+B"
|
||||||
|
channels = ["#main", "#fsoc"]
|
48
src/lib.rs
48
src/lib.rs
|
@ -1,18 +1,22 @@
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
use std::fs::File;
|
||||||
use std::io::{Error, Read, Write};
|
use std::io::{Error, Read, Write};
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use serde_derive::Deserialize;
|
||||||
|
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
stream: TcpStream,
|
stream: TcpStream,
|
||||||
config: Config,
|
config: Config,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Deserialize, Default)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
username: String,
|
username: String,
|
||||||
nickname: String,
|
nickname: Option<String>,
|
||||||
mode: String,
|
mode: Option<String>,
|
||||||
channels: Box<[&'static str]>,
|
channels: Box<[String]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -61,7 +65,11 @@ impl Client {
|
||||||
"*".into(),
|
"*".into(),
|
||||||
self.config.username.clone(),
|
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 {
|
loop {
|
||||||
if let Ok(ref command) = self.read() {
|
if let Ok(ref command) = self.read() {
|
||||||
|
@ -78,9 +86,7 @@ impl Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
let config = self.config.clone();
|
let config = self.config.clone();
|
||||||
if config.mode != "" {
|
self.write_command(Command::MODE(config.username, config.mode))?;
|
||||||
self.write_command(Command::MODE(config.username, Some(config.mode)))?;
|
|
||||||
}
|
|
||||||
for channel in config.channels.iter() {
|
for channel in config.channels.iter() {
|
||||||
self.write_command(Command::JOIN(channel.to_string()))?;
|
self.write_command(Command::JOIN(channel.to_string()))?;
|
||||||
}
|
}
|
||||||
|
@ -161,7 +167,7 @@ impl Client {
|
||||||
Cow::Owned(formatted) as Cow<str>
|
Cow::Owned(formatted) as Cow<str>
|
||||||
}
|
}
|
||||||
OTHER(_) => {
|
OTHER(_) => {
|
||||||
return Err(std::io::Error::new(
|
return Err(Error::new(
|
||||||
std::io::ErrorKind::Other,
|
std::io::ErrorKind::Other,
|
||||||
"Cannot write commands of type OTHER",
|
"Cannot write commands of type OTHER",
|
||||||
));
|
));
|
||||||
|
@ -177,15 +183,27 @@ impl Client {
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
username: &str,
|
username: &str,
|
||||||
nickname: Option<&str>,
|
nickname: Option<String>,
|
||||||
mode: Option<&str>,
|
mode: Option<String>,
|
||||||
channels: Box<[&'static str]>,
|
channels: Box<[String]>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
username: username.into(),
|
username: username.into(),
|
||||||
nickname: nickname.unwrap_or(username).into(),
|
nickname,
|
||||||
mode: mode.unwrap_or("").into(),
|
mode,
|
||||||
channels: channels.into(),
|
channels,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn from_toml<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
|
||||||
|
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))
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -10,14 +10,8 @@ macro_rules! loop_n {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), std::io::Error> {
|
fn main() -> Result<(), std::io::Error> {
|
||||||
let config = Config::new(
|
let config = Config::from_toml("config.toml")?;
|
||||||
"test",
|
let mut client = Client::new("192.168.1.28", 6667, config)?;
|
||||||
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");
|
|
||||||
client.identify()?;
|
client.identify()?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
|
Loading…
Reference in a new issue