Made user configuration more convenient, changes to examples in the docs

This commit is contained in:
famfo 2021-11-07 17:08:21 +01:00 committed by Yash Karandikar
parent ce37cac4fe
commit 3058f92f8b
Signed by: karx
GPG key ID: A794DA2529474BA5
2 changed files with 57 additions and 45 deletions

View file

@ -14,10 +14,10 @@
//! if let Command::PRIVMSG(channel, message) = command {
//! println!("PRIVMSG received: {} {}", channel, message);
//! }
//! }
//! }
//! # break;
//! }
//!
//!
//! # Ok(())
//! }
@ -39,7 +39,7 @@ pub struct Client {
/// Config for the IRC client
#[derive(Clone, Deserialize, Default)]
pub struct Config {
channels: Box<[String]>,
channels: Vec<String>,
host: String,
mode: Option<String>,
nickname: Option<String>,
@ -55,6 +55,7 @@ pub enum CapMode {
}
/// IRC commands
/// Not reccomended to use, use the helper functions instead
#[derive(Debug)]
pub enum Command {
#[doc(hidden)]
@ -88,11 +89,8 @@ pub enum Command {
NICK(String),
/// Everything that is not a command
OTHER(String),
/// Ping another user or the server
PING(
/// target
String,
),
#[doc(hidden)]
PING(String),
#[doc(hidden)]
PONG(String),
/// Sends a message in a channel
@ -117,8 +115,8 @@ impl Command {
let new = s.trim();
if new.starts_with("PING") {
let command: String = String::from(new.split_whitespace().collect::<Vec<&str>>()[1]);
return Self::PING(command);
let command = new.split_whitespace().collect::<Vec<&str>>()[1].to_string();
return Self::PONG(command);
} else if new.contains("PRIVMSG") {
let parts: Vec<&str> = new.split_whitespace().collect();
@ -169,7 +167,7 @@ impl Client {
))?;
if let Some(nick) = self.config.nickname.clone() {
self.write_command(Command::NICK(nick))?;
self.write_command(Command::NICK(nick.to_string()))?;
} else {
self.write_command(Command::NICK(self.config.username.clone()))?;
}
@ -287,8 +285,8 @@ impl Client {
"Cannot write commands of type OTHER",
));
}
PING(code) => {
let formatted = format!("PING {}", code);
PING(target) => {
let formatted = format!("PING {}", target);
Cow::Owned(formatted) as Cow<str>
}
PONG(code) => {
@ -309,7 +307,7 @@ impl Client {
Ok(())
}
// Utility functions!
// Helper functions
/// Helper function for sending PRIVMSGs.
/// This makes
@ -383,38 +381,52 @@ impl Client {
}
impl Config {
/// Create a new config for the client<br>
/// <br>
/// channels: Channels to join on the IRC<br>
/// host: IP or domain of the IRC server<br>
/// mode: Mode to join the IRC with (optional)<br>
/// nickname: Nickname to join the IRC with (optional, defaults to the given username)<br>
/// port: Port of the IRC server<br>
/// username: Username to join the IRC with<br>
/// Create a new config for the client
///
/// ```rust
/// # use circe::*;
/// let config = Config::new(
/// Box::new(["#main".to_string(), "#main2".to_string()]),
/// "192.168.178.100",
/// Some("+B".to_string()),
/// Some("IRSC".to_string()),
/// 6667,
/// "IRSC",
/// vec!["#main", "#main2"],
/// "192.168.178.100",
/// Some("+B"),
/// Some("IRSC"),
/// 6667,
/// "circe",
/// );
/// ```
pub fn new(
channels: Box<[String]>,
channels: Vec<&'static str>,
host: &str,
mode: Option<String>,
nickname: Option<String>,
mode: Option<&'static str>,
nickname: Option<&'static str>,
port: u16,
username: &str,
) -> Self {
// Conversion from &'static str to String
let mut channels_config = Vec::new();
channels
.iter()
.for_each(|channel| channels_config.push(channel.to_string()));
let mode_config: Option<String>;
if mode.is_some() {
mode_config = Some(mode.unwrap().to_string());
} else {
mode_config = None;
}
let nickname_config: Option<String>;
if nickname.is_some() {
nickname_config = Some(nickname.unwrap().to_string());
} else {
nickname_config = Some(username.to_string());
}
Self {
channels,
channels: channels_config,
host: host.into(),
mode,
nickname,
mode: mode_config,
nickname: nickname_config,
port,
username: username.into(),
}
@ -431,9 +443,9 @@ impl Config {
/// channels = ["#main", "#main2"]
/// host = "192.168.178.100"
/// mode = "+B"
/// nickname = "IRSC"
/// nickname = "circe"
/// port = 6667
/// username = "IRSC"
/// username = "circe"
/// ```
///
/// Returns an Error if the file cannot be opened or if the TOML is invalid

View file

@ -10,15 +10,15 @@ macro_rules! loop_n {
}
fn main() -> Result<(), std::io::Error> {
let config = Config::from_toml("config.toml")?;
/*let config = Config::new(
Box::new(["#main".to_string(), "#main2".to_string()]),
"192.168.178.100",
Some("+B".to_string()),
Some("IRSC".to_string()),
6667,
"IRSC",
);*/
//let config = Config::from_toml("config.toml")?;
let config = Config::new(
vec!["#main", "#main2"],
"192.168.178.100",
Some("+B"),
Some("IRSC"),
6667,
"circe",
);
let mut client = Client::new(config)?;
client.identify()?;