Start rust implementation
This commit is contained in:
commit
d2118316d3
|
@ -0,0 +1,51 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "git-build-rs"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"glob",
|
||||
"lazy_static",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "glob"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed3d612bc64430efeb3f7ee6ef26d590dce0c43249217bddc62112540c7941e1"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.106"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "36df6ac6412072f67cf767ebbde4133a5b2e88e76dc6187fa7104cd16f783399"
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.52"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7894c8ed05b7a3a279aeb79025fdec1d3158080b75b98a08faf2806bb799edd"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
|
@ -0,0 +1,14 @@
|
|||
[package]
|
||||
name = "git-build-rs"
|
||||
version = "0.1.0"
|
||||
authors = ["southerntofu"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
glob = "0.3"
|
||||
lazy_static = "1.4"
|
||||
# Translations
|
||||
#serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,122 @@
|
|||
use std::env;
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::fs;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
lazy_static!{
|
||||
static ref LOGLEVEL: LogLevel = LogLevel::from_env();
|
||||
static ref LANG: String = lang_from_env();
|
||||
static ref TRANSLATIONS: HashMap<String, String> = load_translations();
|
||||
}
|
||||
|
||||
fn load_translations() -> HashMap<String, String> {
|
||||
let folder = env::var("I18N").unwrap_or("./i18n/".to_string());
|
||||
let mut path = PathBuf::from(folder);
|
||||
if !path.is_dir() {
|
||||
panic!("Could not find translations in {:?}", path);
|
||||
}
|
||||
path.push(format!("{}.json", *LANG));
|
||||
|
||||
if !path.is_file() {
|
||||
panic!("Could not find translation file: {:?}", path);
|
||||
}
|
||||
|
||||
match fs::read_to_string(&path) {
|
||||
Ok(content) => {
|
||||
//let trans: HashMap<String, String> = serde_json::from_str(&content).expect("Could not load translations");
|
||||
//return trans
|
||||
match serde_json::from_str(&content) {
|
||||
Ok(trans) => trans,
|
||||
Err(e) => panic!("JSON ERROR: {}", e)
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
panic!("IO ERROR: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn trans(key: &str) -> String {
|
||||
match TRANSLATIONS.get(key) {
|
||||
Some(t) => t.to_string(),
|
||||
None => {
|
||||
panic!("Unknown translation string in lang {}: {}", *LANG, key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn lang_from_env() -> String {
|
||||
let lang = env::var("LANG").expect("$LANG not set in environment. Your machine is misconfigured!");
|
||||
lang[0..2].to_string()
|
||||
}
|
||||
|
||||
|
||||
struct LogLevel {
|
||||
info: bool,
|
||||
debug: bool,
|
||||
error: bool
|
||||
}
|
||||
|
||||
impl LogLevel {
|
||||
fn from_env() -> LogLevel {
|
||||
// Default values
|
||||
let error = true;
|
||||
let mut info = true;
|
||||
let mut debug = false;
|
||||
|
||||
let env_log = env::var("LOG").unwrap_or("info".to_string());
|
||||
match env_log.to_lowercase().as_str() {
|
||||
"info" => {},
|
||||
"debug" => { debug = true; },
|
||||
"error" => { info = false; }
|
||||
_ => {
|
||||
// This happens before loglevel initialization
|
||||
// so we can't use warn function
|
||||
eprintln!("$LOG level is incorrect: {} (can be: debug, info, error", env_log);
|
||||
}
|
||||
}
|
||||
|
||||
return LogLevel {
|
||||
info,
|
||||
debug,
|
||||
error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn expand(msg: &str, vars: Option<&HashMap<String,&str>>) -> String {
|
||||
let mut trans_string = trans(msg);
|
||||
if vars.is_some() {
|
||||
for (key, val) in vars.unwrap() {
|
||||
trans_string = trans_string.replace(key, val);
|
||||
}
|
||||
}
|
||||
trans_string
|
||||
}
|
||||
|
||||
pub fn info(msg: &str, vars: Option<&HashMap<String,&str>>) {
|
||||
if LOGLEVEL.info {
|
||||
let trans_string = expand(msg, vars);
|
||||
println!("[git-build] {}", trans_string);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn error(msg: &str) {
|
||||
if LOGLEVEL.error {
|
||||
eprintln!("ERROR: {}", msg);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn warn(msg: &str) {
|
||||
if LOGLEVEL.error {
|
||||
eprintln!("WARNING: {}", msg);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn debug(msg: &str) {
|
||||
if LOGLEVEL.debug {
|
||||
println!("DEBUG: {}", msg);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
use std::env;
|
||||
use std::collections::HashMap;
|
||||
|
||||
mod log;
|
||||
|
||||
fn main() {
|
||||
|
||||
let home_dir = env::var("HOME").expect("$HOME not defined. WTF?");
|
||||
let base_dir = format!("{}/.git-build", home_dir);
|
||||
|
||||
let mut test = HashMap::new();
|
||||
test.insert("$i18n_task".to_string(), "test");
|
||||
log::info("process", Some(&test));
|
||||
|
||||
//let log_level = env::var("LOG").unwrap_or
|
||||
}
|
Loading…
Reference in New Issue