Browse Source

sept11

master
Xsivax 3 years ago
parent
commit
f3683a0bdc
6 changed files with 93 additions and 41 deletions
  1. +9
    -1
      lang.json
  2. +59
    -18
      src/build_rocket/config.rs
  3. +6
    -2
      src/build_rocket/get.rs
  4. +17
    -18
      src/build_rocket/templates.rs
  5. +1
    -1
      src/main.rs
  6. +1
    -1
      templates/base.html.tera

+ 9
- 1
lang.json View File

@ -1,3 +1,11 @@
{ {
"lang" : "json"
"lang" : {
"json" : "object",
"test" : ["a", "b", "c"],
"title" : "JSON"
},
"title": {
"en": "Hello Json"
}
} }

+ 59
- 18
src/build_rocket/config.rs View File

@ -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)),
}
}
// 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 );
}
}

+ 6
- 2
src/build_rocket/get.rs View File

@ -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());
Template::render("test_extend", templates::return_json())
config::debug();
// that's the one
config::get_key();
Template::render("test_extend", &config::LANG )
} }

+ 17
- 18
src/build_rocket/templates.rs View File

@ -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 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<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();
pub fn print_object() {
let object = &config::init_lang().unwrap();
Ok(())
println!("Context = {:#?}", object);
} }

+ 1
- 1
src/main.rs View File

@ -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;

+ 1
- 1
templates/base.html.tera View File

@ -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…
Cancel
Save