81 lines
2.1 KiB
Rust
81 lines
2.1 KiB
Rust
use std::string::ToString;
|
|
|
|
#[cfg(feature = "toml_config")]
|
|
use serde_derive::Deserialize;
|
|
#[cfg(feature = "toml_config")]
|
|
use std::path::Path;
|
|
|
|
#[derive(Clone, Default)]
|
|
#[cfg_attr(feature = "toml_config", derive(Deserialize))]
|
|
pub struct Config {
|
|
pub host: String,
|
|
pub port: u16,
|
|
pub username: String,
|
|
pub nickname: Option<String>,
|
|
pub mode: Option<String>,
|
|
pub channels: Vec<String>,
|
|
}
|
|
|
|
impl Config {
|
|
/// Creates new config for a circe client
|
|
pub async fn new(
|
|
host: &'static str,
|
|
port: u16,
|
|
username: &'static str,
|
|
nickname: Option<&'static str>,
|
|
mode: Option<&'static str>,
|
|
channels: &[&'static str],
|
|
) -> Self {
|
|
let nickname = nickname.map(ToString::to_string);
|
|
let mode = mode.map(ToString::to_string);
|
|
let channels = {
|
|
let mut channel_vec = Vec::new();
|
|
for channel in channels {
|
|
channel_vec.push((*channel).to_string());
|
|
}
|
|
channel_vec
|
|
};
|
|
|
|
Config {
|
|
host: host.into(),
|
|
port,
|
|
username: username.into(),
|
|
nickname,
|
|
mode,
|
|
channels,
|
|
}
|
|
}
|
|
|
|
/// Creates new config for a circe client
|
|
pub async fn new_runtime(
|
|
host: String,
|
|
port: u16,
|
|
username: String,
|
|
nickname: Option<String>,
|
|
mode: Option<String>,
|
|
channels: Vec<String>,
|
|
) -> Self {
|
|
Config {
|
|
host,
|
|
port,
|
|
username,
|
|
nickname,
|
|
mode,
|
|
channels,
|
|
}
|
|
}
|
|
|
|
/// Creates new config for a circe client from a toml file
|
|
#[cfg(feature = "toml_config")]
|
|
pub async fn new_toml<P: AsRef<Path>>(file: P) -> Result<Self, tokio::io::Error> {
|
|
use tokio::fs::File;
|
|
use tokio::io::{AsyncReadExt, Error, ErrorKind};
|
|
|
|
let mut file = File::open(&file).await?;
|
|
let mut config = String::new();
|
|
file.read_to_string(&mut config).await?;
|
|
toml::from_str(&config)
|
|
.map_err(|e| Error::new(ErrorKind::Other, format!("Invalid TOML: {}", e)))
|
|
}
|
|
}
|