Skip to content

Commit

Permalink
loading from json file
Browse files Browse the repository at this point in the history
  • Loading branch information
Joris Rehm committed Jun 3, 2017
1 parent 592637e commit 5fa5815
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 3 deletions.
146 changes: 146 additions & 0 deletions ressources/catalog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
{
"groups": [
{
"id": "964F1752EA6B1F8F",
"name": "Protéine"
},
{
"id": "9C7E204D4FB525A7",
"name": "Fromage"
},
{
"id": "965EB81880FDBD60",
"name": "Fruit"
},
{
"id": "8401482BB760EBC1",
"name": "Féculent"
},
{
"id": "6FB75507B86AA70F",
"name": "Graisse"
},
{
"id": "B734CD6F825CBE05",
"name": "Légume"
},
{
"id": "EF230FC81D90DEC2",
"name": "Laitage"
}
],
"ingredients": [
{
"group": "965EB81880FDBD60",
"id": "A98F391382877A2B",
"name": "pomme",
"quantity": {
"unit": "p",
"val": 1.0
},
"section": "D0D1A0ACE7105443"
},
{
"group": "8401482BB760EBC1",
"id": "7E506FAFAEC6D43D",
"name": "tranche de pain",
"quantity": {
"unit": "p",
"val": 1.0
},
"section": "2C650EE9BE715FB2"
},
{
"group": "964F1752EA6B1F8F",
"id": "7B3B9E355A514066",
"name": "steak haché",
"quantity": {
"unit": "p",
"val": 1.0
},
"section": "75D62D767CF7276D"
},
{
"group": "B734CD6F825CBE05",
"id": "4402552E66A79AB6",
"name": "tomate fraiche",
"quantity": {
"unit": "p",
"val": 1.0
},
"section": "D0D1A0ACE7105443"
},
{
"group": "9C7E204D4FB525A7",
"id": "32DADA3DE5C51799",
"name": "emmental",
"quantity": {
"unit": "g",
"val": 30.0
},
"section": "0EACFBABA033FF44"
},
{
"group": "6FB75507B86AA70F",
"id": "869E9097D6F32C8E",
"name": "huile",
"quantity": {
"unit": "cl",
"val": 5.0
},
"section": "2C650EE9BE715FB2"
},
{
"group": "EF230FC81D90DEC2",
"id": "101E8A1E6C94A777",
"name": "lait",
"quantity": {
"unit": "p",
"val": 1.0
},
"section": "0EACFBABA033FF44"
}
],
"sections": [
{
"id": "2C650EE9BE715FB2",
"name": "Autre"
},
{
"id": "D0D1A0ACE7105443",
"name": "Primeur"
},
{
"id": "FF7E33A03AB79878",
"name": "Surgelé"
},
{
"id": "75D62D767CF7276D",
"name": "Viande"
},
{
"id": "4C4422E13210AFA9",
"name": "Poisson"
},
{
"id": "E9DC279F3AB47562",
"name": "Féculents"
},
{
"id": "AADACEA2CB68E086",
"name": "Bio"
},
{
"id": "0EACFBABA033FF44",
"name": "Crémerie"
},
{
"id": "3FBC0599B7484ECD",
"name": "Condiment"
},
{
"id": "F4702AE6AE606B8A",
"name": "Boisson"
}
]
}
25 changes: 25 additions & 0 deletions ressources/recipes_book.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"recipes": [
{
"id": "46034B9BC531DF13",
"ingredients": [
{
"id": "7B3B9E355A514066",
"quantity": {
"unit": "p",
"val": 1.0
}
},
{
"id": "101E8A1E6C94A777",
"quantity": {
"unit": "p",
"val": 1.0
}
}
],
"name": "gloubiboula",
"note": "mélanger tout"
}
]
}
90 changes: 90 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,93 @@ fn serialize_ingredient_use(ingredient : & Ingredient) -> Value {
"quantity" : ingredient.quantity,
})
}

pub fn load_all(data : &mut Data, path : &Path) {
use std::fs::File;
use serde_json::value::from_value;
{
let file = File::open(path.join(CATALOG_FILE_NAME)).unwrap();
let json: Value = serde_json::from_reader(file).unwrap();
let json = json.as_object().unwrap();

let sections = json
.get("sections").unwrap()
.as_array().unwrap();
let groups = json
.get("groups").unwrap()
.as_array().unwrap();
let ingredients = json
.get("ingredients").unwrap()
.as_array().unwrap();

for section in sections {
let id = deserialize_id(&section["id"]);
let name = section["name"].as_str().unwrap();
// TODO convertion own/ref pas performante et pas logique
data.add_section(&Section{id:id, name:name.to_string()});
}

for group in groups {
let id = deserialize_id(&group["id"]);
let name = group["name"].as_str().unwrap();
// TODO convertion own/ref pas performante et pas logique
data.add_group(&Group{id:id, name:name.to_string()});
}

for ingredient in ingredients {
let id = deserialize_id(&ingredient["id"]);
let name = ingredient["name"].as_str().unwrap();
let group_id = deserialize_id(&ingredient["group"]);
let group = data.get_group(group_id).unwrap().clone();
let section_id = deserialize_id(&ingredient["section"]);
let section = data.get_section(section_id).unwrap().clone();
let quantity = ingredient["quantity"].clone();
let quantity : Quantity = from_value(quantity).unwrap();
data.add_ingredient(&Ingredient{
id : id,
name : name.to_string(),
group : group,
section : section,
quantity : quantity,
});
}
}
{
let file = File::open(path.join(BOOK_FILE_NAME)).unwrap();
let json: Value = serde_json::from_reader(file).unwrap();
let json = json.as_object().unwrap();

let recipes = json
.get("recipes").unwrap()
.as_array().unwrap();
for recipe in recipes {
let id = deserialize_id(&recipe["id"]);
let name = recipe["name"].as_str().unwrap();
let note = recipe["note"].as_str().unwrap();
let ingredients = recipe
.get("ingredients").unwrap()
.as_array().unwrap();
let mut r = Recipe {
id : id,
name : name.to_string(),
note : note.to_string(),
ingredients : vec![],
};
for ingredient in ingredients {
let id = deserialize_id(&ingredient["id"]);
let quantity = ingredient["quantity"].clone();
let quantity : Quantity = from_value(quantity).unwrap();
let mut i = data.get_ingredient(id).unwrap().clone();
i.quantity = quantity;
r.ingredients.push(i);
}
data.add_recipe(&r);
}
}
}

fn deserialize_id(json : &Value) -> Id {
let id = json.as_str().unwrap();
let id = u64::from_str_radix(id, 16).unwrap();
Id(id)
}
15 changes: 14 additions & 1 deletion src/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ impl Data {
}
}

// TODO faire un move plutot ? (et renvoyer une référece ?)
pub fn add_section(&mut self, section : &Section) {
// TODO check if id is unique
self.catalog.sections.insert(section.id, section.clone());
}

pub fn get_section(&self, id : Id) -> Option<&Section> {
self.catalog.sections.get(&id)
}

pub fn iter_sections(&self) -> hash_map::Values<Id, Section> {
self.catalog.sections.values()
}
Expand All @@ -47,6 +52,10 @@ impl Data {
self.catalog.groups.insert(group.id, group.clone());
}

pub fn get_group(&self, id : Id) -> Option<&Group> {
self.catalog.groups.get(&id)
}

pub fn iter_groups(&self) -> hash_map::Values<Id, Group> {
self.catalog.groups.values()
}
Expand All @@ -56,6 +65,10 @@ impl Data {
self.catalog.ingredients.insert(ingredient.id, ingredient.clone());
}

pub fn get_ingredient(&self, id : Id) -> Option<&Ingredient> {
self.catalog.ingredients.get(&id)
}

pub fn iter_ingredients(&self) -> hash_map::Values<Id, Ingredient> {
self.catalog.ingredients.values()
}
Expand Down Expand Up @@ -203,7 +216,7 @@ impl fmt::Display for Unit {
}

#[derive(Clone,Copy,PartialEq,Eq,Hash)]
pub struct Id( u64 );
pub struct Id( pub u64 );

impl Id {
fn new() -> Id {
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ mod file; use file::*;

fn main(){
let data : Rc<RefCell<Data>> = Rc::new(RefCell::new(Data::new()));
add_default_data(&mut data.borrow_mut());
save_all(& data.borrow(),std::path::Path::new("."));
load_all(&mut data.borrow_mut(), std::path::Path::new("ressources"));
save_all(& data.borrow(), std::path::Path::new("."));
}

// fn main() {
Expand Down

0 comments on commit 5fa5815

Please sign in to comment.