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"
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"

View file

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

View file

@ -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<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();
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::<Value>().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(),
}
}