diff --git a/lang.json b/lang.json index a415af5..b3db076 100644 --- a/lang.json +++ b/lang.json @@ -1,3 +1,11 @@ { - "lang" : "json" + "lang" : { + "json" : "object", + "test" : ["a", "b", "c"], + "title" : "JSON" + }, + + "title": { + "en": "Hello Json" + } } \ No newline at end of file diff --git a/src/build_rocket/config.rs b/src/build_rocket/config.rs index 01dfdba..89509ae 100644 --- a/src/build_rocket/config.rs +++ b/src/build_rocket/config.rs @@ -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 = init_lang(); +} + +// Convert Map to Context +pub fn init_lang() -> Result { + let map = &json_to_map(); + let context : Result = 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 structure +fn json_to_map()-> Option> { + 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> { + 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 { -// 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 ); + } +} + diff --git a/src/build_rocket/get.rs b/src/build_rocket/get.rs index ba2921e..1726875 100644 --- a/src/build_rocket/get.rs +++ b/src/build_rocket/get.rs @@ -140,7 +140,11 @@ use crate::build_rocket::{ templates, config }; #[get("/test")] // Render Template Responder pub fn test() -> Template { - println!("{}", &config::init_lang()); - Template::render("test_extend", templates::return_json()) + config::debug(); + + // that's the one + config::get_key(); + + Template::render("test_extend", &config::LANG ) } \ No newline at end of file diff --git a/src/build_rocket/templates.rs b/src/build_rocket/templates.rs index 2fd3a1c..e82f1af 100644 --- a/src/build_rocket/templates.rs +++ b/src/build_rocket/templates.rs @@ -1,30 +1,29 @@ +use rocket_dyn_templates::tera::Context; + // Serde Data Model conversion use rocket::serde::{ Serialize }; -use serde_json::json; - -use std::error::Error; - -use crate ::build_rocket::{ config::LANG }; +use crate::build_rocket::{ config }; // Implement serde Serialize to create context #[derive(Serialize)] -pub struct MyStruct { - pub title : String, - pub lang: String, - pub contents: String, +pub struct Tpl { + pub lang : String, +} + + + + +pub fn print_value() { + let lang : &Context = &config::init_lang().unwrap(); + let value = lang.get("json").unwrap(); + println!("Value = {:#?}", value); } -pub fn return_json() -> Result<(), Box> { - let mystruct = MyStruct { - title : "Test".to_owned(), - lang: "English".to_owned(), - contents: String::from("Those are the contents"), - }; - let v = serde_json::to_value(mystruct).unwrap(); +pub fn print_object() { + let object = &config::init_lang().unwrap(); - Ok(()) - + println!("Context = {:#?}", object); } diff --git a/src/main.rs b/src/main.rs index 302d0a2..6035a11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ #[macro_use] extern crate rocket; -#[macro_use] +#[macro_use] extern crate lazy_static; mod build_rocket; diff --git a/templates/base.html.tera b/templates/base.html.tera index 85793ac..968f999 100644 --- a/templates/base.html.tera +++ b/templates/base.html.tera @@ -2,7 +2,7 @@ - {{ title }} + {{ lang.title }} {%include "test_include" %}