|
|
@ -1,12 +1,11 @@ |
|
|
|
|
|
|
|
use std::fs::{ File };
|
|
|
|
|
|
|
|
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 };
|
|
|
|
|
|
|
|
use serde::Serialize;
|
|
|
|
use std::io::BufReader;
|
|
|
|
|
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
// Define File for language handling
|
|
|
|
pub const LANG_FILE : &str ="./lang.json";
|
|
|
@ -15,59 +14,89 @@ pub const LANG_FILE : &str ="./lang.json"; |
|
|
|
lazy_static! {
|
|
|
|
|
|
|
|
#[derive(Serialize, Debug)]
|
|
|
|
pub static ref LANG : Result<Context, Error> = init_lang();
|
|
|
|
pub static ref LANG : Value = 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)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init_lang() -> Value {
|
|
|
|
let file = File::open(LANG_FILE).expect("can not open file");
|
|
|
|
// read file into buffer
|
|
|
|
let reader = BufReader::new(file);
|
|
|
|
|
|
|
|
let data : Value = serde_json::from_reader(reader).expect("can not parse Json contents");
|
|
|
|
|
|
|
|
// 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()),
|
|
|
|
}
|
|
|
|
data
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
let object = json.as_object();
|
|
|
|
|
|
|
|
match object {
|
|
|
|
None => None,
|
|
|
|
Some(object) => Some(object.clone()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn debug() {
|
|
|
|
let map = &all_to_map();
|
|
|
|
// 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()));
|
|
|
|
|
|
|
|
println!("{:#?}", map );
|
|
|
|
}
|
|
|
|
// match context {
|
|
|
|
// Ok(context) => {
|
|
|
|
// Ok(context)
|
|
|
|
// },
|
|
|
|
// Err(error) => Err(Error::msg(error)),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
pub fn get_key() {
|
|
|
|
// // Convert JSON from file to Map<String, Value> structure
|
|
|
|
// pub fn json_to_map()-> Option<Map<String, Value>> {
|
|
|
|
// // Open lang.json
|
|
|
|
// let file = File::open(LANG_FILE).expect("can not open file");
|
|
|
|
// // read file into buffer
|
|
|
|
// let reader = BufReader::new(file);
|
|
|
|
|
|
|
|
let map = &all_to_map().unwrap();
|
|
|
|
// // read JSON into Value
|
|
|
|
// let json : Value = serde_json::from_reader(reader).expect("can not read Json contents");
|
|
|
|
|
|
|
|
let keys = map.keys();
|
|
|
|
// // store JSON key/value pairs in Map
|
|
|
|
// let obj: Map<String, Value> = json.as_object().unwrap().clone();
|
|
|
|
|
|
|
|
for key in keys {
|
|
|
|
// // return struct Map
|
|
|
|
// Some(obj)
|
|
|
|
// }
|
|
|
|
|
|
|
|
println!("{:?}", key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// fn return_value( key: &str, value: &str ) -> Option<String>{
|
|
|
|
// let data = json_to_map().unwrap();
|
|
|
|
|
|
|
|
// let value = data.get(key).unwrap().get(value).unwrap();
|
|
|
|
|
|
|
|
// Some(value.to_string())
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// pub fn debug() {
|
|
|
|
// println!("context is {:#?}", init_lang());
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// pub fn print_key( key : &str) {
|
|
|
|
|
|
|
|
// let data = json_to_map().unwrap();
|
|
|
|
|
|
|
|
// let mut keys = data.keys();
|
|
|
|
|
|
|
|
// let my_key = keys.find(|&x| x == key ).unwrap();
|
|
|
|
|
|
|
|
// let result = data.get(my_key).unwrap();
|
|
|
|
|
|
|
|
// println!("requested value is {:?}", result );
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|