All current work
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/target
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "rust-training-config-reader"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "rust-training-config-reader"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,4 @@
|
||||
host=localhost
|
||||
port=808
|
||||
debug=true
|
||||
workers=8
|
||||
@@ -0,0 +1,62 @@
|
||||
use std::fs;
|
||||
use std::io;
|
||||
|
||||
struct Config {
|
||||
host: String,
|
||||
port: u16,
|
||||
debug: bool,
|
||||
workers: u16,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
fn init(text: &str) -> Config {
|
||||
let mut new_conf = Config{host: String::new(),port: 0, debug: false, workers: 0};
|
||||
let lines = text.lines();
|
||||
for line in lines {
|
||||
let (key, value) = line.split_once("=").map(|(x,y)| (x.trim(),y.trim())).unwrap();
|
||||
match key {
|
||||
"host" => new_conf.add_host(value),
|
||||
"port" => new_conf.add_port(value),
|
||||
"debug" => new_conf.add_debug(value),
|
||||
"workers" => new_conf.add_workers(value),
|
||||
_ => println!("nothing ready")
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
new_conf
|
||||
}
|
||||
|
||||
fn add_host(&mut self, host: &str){
|
||||
self.host = host.to_string();
|
||||
println!("host ready");
|
||||
}
|
||||
|
||||
fn add_port(&mut self, port: &str){
|
||||
let port_ready:u16 = port.parse().expect("Not a u16");
|
||||
self.port = port_ready;
|
||||
println!("port ready");
|
||||
}
|
||||
|
||||
|
||||
fn add_debug(&mut self, debug: &str){
|
||||
let debug_ready: bool = debug.to_lowercase().parse().expect("Not a bool");
|
||||
self.debug = debug_ready;
|
||||
println!("debug ready");
|
||||
}
|
||||
|
||||
fn add_workers(&mut self, workers: &str){
|
||||
let workers_ready:u16 = workers.parse().expect("Not a u16");
|
||||
self.workers = workers_ready;
|
||||
println!("workers ready");
|
||||
}
|
||||
}
|
||||
|
||||
fn read_config(path: &str) -> Result<String, io::Error> {
|
||||
fs::read_to_string(path)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let text = read_config("config.txt").unwrap();
|
||||
let new_config = Config::init(&text);
|
||||
}
|
||||
Reference in New Issue
Block a user