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::fs::{ File };
|
||||||
use std::io;
|
|
||||||
use std::io::prelude::*;
|
|
||||||
|
|
||||||
use rocket_dyn_templates::tera::Value;
|
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
|
// Define File for language handling
|
||||||
pub const LANG_FILE : &str ="./lang.json";
|
pub const LANG_FILE : &str ="./lang.json";
|
||||||
|
|
||||||
// Store JSON Value in 'static variable
|
// Store Value in static variable initialized at runtime
|
||||||
lazy_static! {
|
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> {
|
// Convert JSON from file to Map<String, Value> structure
|
||||||
// let mut f = File::open(LANG_FILE)?;
|
fn json_to_map()-> Option<Map<String, Value>> {
|
||||||
// let mut buffer = String::new();
|
let file = File::open(LANG_FILE).expect("unable to open file");
|
||||||
// f.read_to_string(&mut buffer)?;
|
let json : Value = serde_json::from_reader(file).expect("file should be JSON syntax");
|
||||||
// Ok(buffer)
|
let value = json["lang"].as_object();
|
||||||
// }
|
match value {
|
||||||
|
None => None,
|
||||||
// pub fn init_lang() -> Value {
|
Some(value) => Some(value.clone()),
|
||||||
// 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()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")]
|
#[get("/test")]
|
||||||
// Render Template Responder
|
// Render Template Responder
|
||||||
pub fn test() -> Template {
|
pub fn test() -> Template {
|
||||||
println!("{}", &config::init_lang());
|
config::debug();
|
||||||
Template::render("test_extend", templates::return_json())
|
|
||||||
|
// 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
|
// Serde Data Model conversion
|
||||||
use rocket::serde::{ Serialize };
|
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
|
// Implement serde Serialize to create context
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct MyStruct {
|
pub struct Tpl {
|
||||||
pub title : String,
|
pub lang : String,
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
|
|
||||||
mod build_rocket;
|
mod build_rocket;
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<title>{{ title }}</title>
|
<title>{{ lang.title }}</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
{%include "test_include" %}
|
{%include "test_include" %}
|
||||||
|
|
Loading…
Reference in a new issue