Last active
February 5, 2019 15:18
-
-
Save pimeys/c220c9cf82668e7c1cd8db044e19715b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Add to Cargo.toml: | |
/// serde = "1.0" | |
/// serde_derive = "1.0" | |
/// serde_yaml = "0.8" | |
/// serde_json = "1.0" | |
use serde_derive::Deserialize; | |
use serde_json::{json, self}; | |
#[derive(Deserialize, Debug)] | |
struct SchemaTemplate { | |
pub name: String, | |
pub models: Vec<ModelTemplate> | |
} | |
#[derive(Debug)] | |
struct Schema<'a> { | |
pub name: String, | |
pub models: Vec<Model<'a>>, | |
} | |
#[derive(Deserialize, Debug)] | |
struct ModelTemplate { | |
pub name: String | |
} | |
#[derive(Debug)] | |
struct Model<'a> { | |
pub name: String, | |
pub schema: &'a Schema<'a>, | |
} | |
impl<'a> ModelTemplate { | |
pub fn build(self, schema: &'a Schema<'a>) -> Model<'a> { | |
Model { | |
name: self.name, | |
schema: schema, | |
} | |
} | |
} | |
impl<'a> Into<Schema<'a>> for SchemaTemplate { | |
fn into(self) -> Schema<'a> { | |
let schema = Schema { | |
name: self.name, | |
models: Vec::new(), | |
}; | |
self.models.into_iter().for_each(|mt| { | |
schema.models.push(mt.build(&schema)); | |
}); | |
schema | |
} | |
} | |
impl<'a> Model<'a> { | |
pub fn print_schema(&self) { | |
println!("{}", self.schema.name); | |
} | |
} | |
impl<'a> Schema<'a> { | |
pub fn find_model(&self, name: &str) -> Option<&'a Model<'a>> { | |
self.models.iter().find(|model| model.name == name) | |
} | |
} | |
fn main() { | |
let json = json!({ | |
"name": "test", | |
"models": [{"name": "testo"}] | |
}); | |
let template: SchemaTemplate = serde_json::from_value(json).unwrap(); | |
let schema: Schema = template.into(); | |
let model = schema.find_model("testo").unwrap(); | |
model.print_schema(); | |
} |
The question number 4 is to find a nice library for this.
The question number 5 is easier with valgrind. It is very important to learn how to use it and how to catch memory leaks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Questions:
dbg!(schema_borrow);
in the last line of the main function? Is there a way to fix this?http://flyingfrogblog.blogspot.com/2013/10/herb-sutters-favorite-c-10-liner.html