From 87f1d3171e9186d8974842a496cc2217137118b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Thu, 24 Dec 2020 00:55:57 +0100 Subject: [PATCH] init project, init toml config parser --- .gitignore | 1 + Cargo.lock | 23 +++++++++++++++ Cargo.toml | 10 +++++++ src/config.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 5 ++++ 5 files changed, 116 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/config.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..2934553 --- /dev/null +++ b/Cargo.lock @@ -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", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c380c21 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "himalaya" +version = "0.1.0" +authors = ["Clément DOUIN "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +toml = "0.5.8" diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..a66a148 --- /dev/null +++ b/src/config.rs @@ -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 { + 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 { + 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 { + 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::().unwrap(); + println!("{:?}", toml); + Config::new() + } + } + } + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..df3b12d --- /dev/null +++ b/src/main.rs @@ -0,0 +1,5 @@ +mod config; + +fn main() { + println!("{:?}", config::read_file()); +}