config support

This commit is contained in:
poslop
2022-12-04 15:39:12 -06:00
parent 6acf1b2119
commit ccb6350519
3 changed files with 95 additions and 36 deletions

79
src/mindus.rs Normal file
View File

@@ -0,0 +1,79 @@
use std::fs::{File, OpenOptions};
use std::io::{Read, Write, Seek};
use std::time::Duration;
use std::net::TcpStream;
use serenity::prelude::TypeMapKey;
use std::str;
pub struct TcpSock {
pub stream: TcpStream,
}
impl TcpSock {
pub fn new(ip: String, port: String) -> std::io::Result<Self> {
let stream = TcpStream::connect(format!("{}:{}", ip, port)).expect("Tcp connection fail");
stream.set_read_timeout(Some(Duration::from_millis(200)))?;
Ok(TcpSock { stream })
}
}
impl TypeMapKey for TcpSock {
type Value = TcpSock;
}
#[derive(serde::Deserialize, serde::Serialize)]
pub struct Config {
pub ip: String,
pub port: String,
pub trigger: char,
pub roles: String
}
pub fn init_conf() -> Config {
let mut toml_file = OpenOptions::new()
.read(true)
.write(true)
.open("config.toml")
.unwrap_or_else(|_e| toml_make());
// .unwrap_or(toml_make());
let mut toml_str = String::new();
toml_file.read_to_string(&mut toml_str).unwrap();
println!("{}", toml_str);
let config: Config = toml::from_str(&toml_str).expect("unable to fill Config struct");
// let mut conf_vec: Vec<u8> = vec![];
// toml_file.read_to_end(&mut conf_vec).expect("unable to read toml to string");
// let toml_str = str::from_utf8(&conf_vec).expect("unable to convert to string");
// println!("{}", toml_str);
// let config: Config = toml::from_str(toml_str).expect("unable to fill Config struct");
config
}
fn toml_make() -> File {
println!("initializing config");
let mut toml_file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open("config.toml")
.unwrap();
let fill_conf = Config {
ip: String::from("localhost"),
port: String::from("6859"),
trigger: ';',
roles: String::new()
};
toml_file.write(toml::to_string(&fill_conf).unwrap().as_bytes()).expect("Unable to write to new file");
toml_file.flush().unwrap();
toml_file.rewind().unwrap();
toml_file
}