From f9e75ee3b455aabb47df6401e999fa03801c1bda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Fri, 25 Dec 2020 00:30:37 +0100 Subject: [PATCH] deserialize config from toml #1 #2 --- Cargo.lock | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/config.rs | 51 +++++++++++++++++++++++++-------------------------- 3 files changed, 76 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2934553..2d334fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,14 +4,58 @@ name = "himalaya" version = "0.1.0" dependencies = [ + "serde", "toml", ] +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" +dependencies = [ + "proc-macro2", +] + [[package]] name = "serde" version = "1.0.118" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06c64263859d87aa2eb554587e2d23183398d617427327cf2b3d0ed8c69e4800" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.118" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c84d3526699cd55261af4b941e4e725444df67aa4f9e6a3564f18030d12672df" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "1.0.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a571a711dddd09019ccc628e1b17fe87c59b09d513c06c026877aa708334f37a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] [[package]] name = "toml" @@ -21,3 +65,9 @@ checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" dependencies = [ "serde", ] + +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" diff --git a/Cargo.toml b/Cargo.toml index c380c21..c86e095 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ edition = "2018" [dependencies] toml = "0.5.8" +serde = { version = "1.0", features = ["derive"] } diff --git a/src/config.rs b/src/config.rs index a66a148..0b4eac7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,22 +1,24 @@ +use serde::Deserialize; use std::env; use std::fs::File; -use std::io::Read; +use std::io::{self, Read}; use std::path::PathBuf; -use toml::Value; +use toml; -#[derive(Debug)] +#[derive(Debug, Deserialize)] +pub struct ServerInfo { + host: String, + port: usize, + login: String, + password: String, +} + +#[derive(Debug, Deserialize)] pub struct Config { name: String, email: String, -} - -impl Config { - fn new() -> Config { - Config { - name: String::new(), - email: String::new(), - } - } + imap: ServerInfo, + smtp: ServerInfo, } pub fn from_xdg() -> Option { @@ -58,20 +60,17 @@ pub fn file_path() -> PathBuf { } } -pub fn read_file() -> Config { +pub fn read_file_content() -> Result { let path = file_path(); - match File::open(path) { - Err(_) => panic!("Config file not found!"), - Ok(mut file) => { - let mut content = String::new(); - match file.read_to_string(&mut content) { - Err(err) => panic!(err), - Ok(_) => { - let toml = content.parse::().unwrap(); - println!("{:?}", toml); - Config::new() - } - } - } + let mut file = File::open(path)?; + let mut content = String::new(); + file.read_to_string(&mut content)?; + Ok(content) +} + +pub fn read_file() -> Config { + match read_file_content() { + Err(err) => panic!(err), + Ok(content) => toml::from_str(&content).unwrap(), } }