|
|
@ -1,32 +1,73 @@ |
|
|
|
|
|
|
|
use std::fs::{ File };
|
|
|
|
use std::io;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
|
|
|
|
use rocket_dyn_templates::tera::Value;
|
|
|
|
use rocket_dyn_templates::tera::Map;
|
|
|
|
use rocket_dyn_templates::tera::Context;
|
|
|
|
use rocket_dyn_templates::tera::Error;
|
|
|
|
|
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
// Define File for language handling
|
|
|
|
pub const LANG_FILE : &str ="./lang.json";
|
|
|
|
|
|
|
|
// Store JSON Value in 'static variable
|
|
|
|
// Store Value in static variable initialized at runtime
|
|
|
|
lazy_static! {
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub static ref LANG: bool = init_lang();
|
|
|
|
|
|
|
|
#[derive(Serialize, Debug)]
|
|
|
|
pub static ref LANG : Result<Context, Error> = init_lang();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert Map to Context
|
|
|
|
pub fn init_lang() -> Result<Context, Error> {
|
|
|
|
let map = &json_to_map();
|
|
|
|
let context : Result<Context, Error> = Context::from_value(Value::Object(map.clone().unwrap()));
|
|
|
|
|
|
|
|
match context {
|
|
|
|
Ok(context) => Ok(context) ,
|
|
|
|
Err(error) => Err(Error::msg(error)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Convert JSON from file to Map<String, Value> structure
|
|
|
|
fn json_to_map()-> Option<Map<String, Value>> {
|
|
|
|
let file = File::open(LANG_FILE).expect("unable to open file");
|
|
|
|
let json : Value = serde_json::from_reader(file).expect("file should be JSON syntax");
|
|
|
|
let value = json["lang"].as_object();
|
|
|
|
match value {
|
|
|
|
None => None,
|
|
|
|
Some(value) => Some(value.clone()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn all_to_map() -> Option<Map<String, Value>> {
|
|
|
|
let file = File::open(LANG_FILE).expect("unable to open file");
|
|
|
|
let json : Value = serde_json::from_reader(file).expect("file should be JSON syntax");
|
|
|
|
|
|
|
|
// fn file_to_string() -> io::Result<String> {
|
|
|
|
// let mut f = File::open(LANG_FILE)?;
|
|
|
|
// let mut buffer = String::new();
|
|
|
|
// f.read_to_string(&mut buffer)?;
|
|
|
|
// Ok(buffer)
|
|
|
|
// }
|
|
|
|
let object = json.as_object();
|
|
|
|
|
|
|
|
// pub fn init_lang() -> Value {
|
|
|
|
// serde_json::from_str(&file_to_string().unwrap()).expect("unable to convert to JSON")
|
|
|
|
// }
|
|
|
|
match object {
|
|
|
|
None => None,
|
|
|
|
Some(object) => Some(object.clone()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn debug() {
|
|
|
|
let map = &all_to_map();
|
|
|
|
|
|
|
|
pub fn init_lang() -> bool {
|
|
|
|
let f = File::open(LANG_FILE).expect("unable to open file");
|
|
|
|
let json : Value = serde_json::from_reader(f).expect("file shoulde be JSON");
|
|
|
|
json.is_object()
|
|
|
|
println!("{:#?}", map );
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_key() {
|
|
|
|
|
|
|
|
let map = &all_to_map().unwrap();
|
|
|
|
|
|
|
|
let keys = map.keys();
|
|
|
|
|
|
|
|
for key in keys {
|
|
|
|
|
|
|
|
println!("{:?}", key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|