deserialize config from toml #1 #2

This commit is contained in:
Clément DOUIN 2020-12-25 00:30:37 +01:00
parent 87f1d3171e
commit f9e75ee3b4
No known key found for this signature in database
GPG key ID: 69C9B9CFFDEE2DEF
3 changed files with 76 additions and 26 deletions

50
Cargo.lock generated
View file

@ -4,14 +4,58 @@
name = "himalaya" name = "himalaya"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"serde",
"toml", "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]] [[package]]
name = "serde" name = "serde"
version = "1.0.118" version = "1.0.118"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06c64263859d87aa2eb554587e2d23183398d617427327cf2b3d0ed8c69e4800" 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]] [[package]]
name = "toml" name = "toml"
@ -21,3 +65,9 @@ checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
dependencies = [ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"

View file

@ -8,3 +8,4 @@ edition = "2018"
[dependencies] [dependencies]
toml = "0.5.8" toml = "0.5.8"
serde = { version = "1.0", features = ["derive"] }

View file

@ -1,22 +1,24 @@
use serde::Deserialize;
use std::env; use std::env;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::{self, Read};
use std::path::PathBuf; 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 { pub struct Config {
name: String, name: String,
email: String, email: String,
} imap: ServerInfo,
smtp: ServerInfo,
impl Config {
fn new() -> Config {
Config {
name: String::new(),
email: String::new(),
}
}
} }
pub fn from_xdg() -> Option<PathBuf> { pub fn from_xdg() -> Option<PathBuf> {
@ -58,20 +60,17 @@ pub fn file_path() -> PathBuf {
} }
} }
pub fn read_file() -> Config { pub fn read_file_content() -> Result<String, io::Error> {
let path = file_path(); let path = file_path();
match File::open(path) { let mut file = File::open(path)?;
Err(_) => panic!("Config file not found!"), let mut content = String::new();
Ok(mut file) => { file.read_to_string(&mut content)?;
let mut content = String::new(); Ok(content)
match file.read_to_string(&mut content) { }
Err(err) => panic!(err),
Ok(_) => { pub fn read_file() -> Config {
let toml = content.parse::<Value>().unwrap(); match read_file_content() {
println!("{:?}", toml); Err(err) => panic!(err),
Config::new() Ok(content) => toml::from_str(&content).unwrap(),
}
}
}
} }
} }