sept11
This commit is contained in:
parent
b45e7d5b6f
commit
f3683a0bdc
6 changed files with 95 additions and 43 deletions
10
lang.json
10
lang.json
|
@ -1,3 +1,11 @@
|
|||
{
|
||||
"lang" : "json"
|
||||
"lang" : {
|
||||
"json" : "object",
|
||||
"test" : ["a", "b", "c"],
|
||||
"title" : "JSON"
|
||||
},
|
||||
|
||||
"title": {
|
||||
"en": "Hello Json"
|
||||
}
|
||||
}
|
|
@ -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)),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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)
|
||||
// }
|
||||
|
||||
// pub fn init_lang() -> Value {
|
||||
// serde_json::from_str(&file_to_string().unwrap()).expect("unable to convert to JSON")
|
||||
// }
|
||||
|
||||
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()
|
||||
// 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");
|
||||
|
||||
let object = json.as_object();
|
||||
|
||||
match object {
|
||||
None => None,
|
||||
Some(object) => Some(object.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn debug() {
|
||||
let map = &all_to_map();
|
||||
|
||||
println!("{:#?}", map );
|
||||
}
|
||||
|
||||
pub fn get_key() {
|
||||
|
||||
let map = &all_to_map().unwrap();
|
||||
|
||||
let keys = map.keys();
|
||||
|
||||
for key in keys {
|
||||
|
||||
println!("{:?}", key );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 )
|
||||
}
|
||||
|
|
@ -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 struct Tpl {
|
||||
pub lang : String,
|
||||
pub contents: String,
|
||||
}
|
||||
|
||||
pub fn return_json() -> Result<(), Box<Error>> {
|
||||
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();
|
||||
|
||||
Ok(())
|
||||
|
||||
|
||||
pub fn print_value() {
|
||||
let lang : &Context = &config::init_lang().unwrap();
|
||||
let value = lang.get("json").unwrap();
|
||||
println!("Value = {:#?}", value);
|
||||
}
|
||||
|
||||
pub fn print_object() {
|
||||
let object = &config::init_lang().unwrap();
|
||||
|
||||
println!("Context = {:#?}", object);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>{{ title }}</title>
|
||||
<title>{{ lang.title }}</title>
|
||||
</head>
|
||||
<body>
|
||||
{%include "test_include" %}
|
||||
|
|
Loading…
Reference in a new issue