init project, init toml config parser

This commit is contained in:
Clément DOUIN 2020-12-24 00:55:57 +01:00
commit 87f1d3171e
No known key found for this signature in database
GPG key ID: 69C9B9CFFDEE2DEF
5 changed files with 116 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

23
Cargo.lock generated Normal file
View file

@ -0,0 +1,23 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "himalaya"
version = "0.1.0"
dependencies = [
"toml",
]
[[package]]
name = "serde"
version = "1.0.118"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06c64263859d87aa2eb554587e2d23183398d617427327cf2b3d0ed8c69e4800"
[[package]]
name = "toml"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
dependencies = [
"serde",
]

10
Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "himalaya"
version = "0.1.0"
authors = ["Clément DOUIN <soywod@users.noreply.github.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
toml = "0.5.8"

77
src/config.rs Normal file
View file

@ -0,0 +1,77 @@
use std::env;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use toml::Value;
#[derive(Debug)]
pub struct Config {
name: String,
email: String,
}
impl Config {
fn new() -> Config {
Config {
name: String::new(),
email: String::new(),
}
}
}
pub fn from_xdg() -> Option<PathBuf> {
match env::var("XDG_CONFIG_HOME") {
Err(_) => None,
Ok(path_str) => {
let mut path = PathBuf::from(path_str);
path.push("himalaya");
path.push("config.toml");
Some(path)
}
}
}
pub fn from_home() -> Option<PathBuf> {
match env::var("HOME") {
Err(_) => None,
Ok(path_str) => {
let mut path = PathBuf::from(path_str);
path.push(".config");
path.push("himalaya");
path.push("config.toml");
Some(path)
}
}
}
pub fn from_tmp() -> Option<PathBuf> {
let mut path = env::temp_dir();
path.push("himalaya");
path.push("config.toml");
Some(path)
}
pub fn file_path() -> PathBuf {
match from_xdg().or_else(from_home).or_else(from_tmp) {
None => panic!("Config file path not found."),
Some(path) => path,
}
}
pub fn read_file() -> Config {
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()
}
}
}
}
}

5
src/main.rs Normal file
View file

@ -0,0 +1,5 @@
mod config;
fn main() {
println!("{:?}", config::read_file());
}