mirror of
https://github.com/soywod/himalaya.git
synced 2024-11-21 18:40:19 +00:00
init project, init toml config parser
This commit is contained in:
commit
87f1d3171e
5 changed files with 116 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/target
|
23
Cargo.lock
generated
Normal file
23
Cargo.lock
generated
Normal 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
10
Cargo.toml
Normal 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
77
src/config.rs
Normal 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
5
src/main.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
mod config;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("{:?}", config::read_file());
|
||||||
|
}
|
Loading…
Reference in a new issue