From bd8855fde6df1423cf9992efe659a6ce45d4ae86 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Sun, 8 May 2022 00:58:41 +0300 Subject: [PATCH] db: add build script --- .gitignore | 1 + Cargo.lock | 75 ++++++++++ Cargo.toml | 4 + database/ingredients/fruit.toml | 20 --- database/ingredients/liquid.toml | 8 - database/ingredients/product.toml | 21 --- database/ingredients/seasoning.toml | 15 -- database/ingredients/vegetable.toml | 14 -- db/Cargo.lock | 75 ++++++++++ db/Cargo.toml | 10 ++ db/build.rs | 181 +++++++++++++++++++++++ db/data/ingredients/fruit.toml | 20 +++ db/data/ingredients/liquid.toml | 8 + db/data/ingredients/product.toml | 21 +++ db/data/ingredients/seasoning.toml | 15 ++ db/data/ingredients/vegetable.toml | 14 ++ {database => db/data}/recipes/pizza.toml | 12 +- db/src/config.rs | 0 db/src/data.rs | 62 ++++++++ db/src/main.rs | 17 +++ 20 files changed, 509 insertions(+), 84 deletions(-) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml delete mode 100644 database/ingredients/fruit.toml delete mode 100644 database/ingredients/liquid.toml delete mode 100644 database/ingredients/product.toml delete mode 100644 database/ingredients/seasoning.toml delete mode 100644 database/ingredients/vegetable.toml create mode 100644 db/Cargo.lock create mode 100644 db/Cargo.toml create mode 100644 db/build.rs create mode 100644 db/data/ingredients/fruit.toml create mode 100644 db/data/ingredients/liquid.toml create mode 100644 db/data/ingredients/product.toml create mode 100644 db/data/ingredients/seasoning.toml create mode 100644 db/data/ingredients/vegetable.toml rename {database => db/data}/recipes/pizza.toml (87%) create mode 100644 db/src/config.rs create mode 100644 db/src/data.rs create mode 100644 db/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..93f7c0b --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,75 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "db" +version = "0.1.0" +dependencies = [ + "serde", + "toml", +] + +[[package]] +name = "proc-macro2" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9027b48e9d4c9175fa2218adf3557f91c1137021739951d4932f5f8268ac48aa" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.137" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.137" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f26faba0c3959972377d3b2d306ee9f71faee9714294e41bb777f83f88578be" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "1.0.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ff7c592601f11445996a06f8ad0c27f094a58857c2f89e97974ab9235b92c52" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "toml" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" +dependencies = [ + "serde", +] + +[[package]] +name = "unicode-xid" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e78e3d9 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,4 @@ +[workspace] +members = [ + "db" +] diff --git a/database/ingredients/fruit.toml b/database/ingredients/fruit.toml deleted file mode 100644 index 068fa36..0000000 --- a/database/ingredients/fruit.toml +++ /dev/null @@ -1,20 +0,0 @@ -[[ingredients]] -key = "apple" -measure = "kg" - -ru = "яблоко" -en = "apple" - -[[ingredients]] -key = "banana" -measure = "kg" - -ru = "банан" -en = "banana" - -[[ingredients]] -key = "orange" -measure = "kg" - -ru = "апельсин" -en = "orange" diff --git a/database/ingredients/liquid.toml b/database/ingredients/liquid.toml deleted file mode 100644 index ae83ccf..0000000 --- a/database/ingredients/liquid.toml +++ /dev/null @@ -1,8 +0,0 @@ - -[[ingredients]] -key = "water" -measure = "ml" - -ru = "вода" -en = "water" - diff --git a/database/ingredients/product.toml b/database/ingredients/product.toml deleted file mode 100644 index 0cd55d2..0000000 --- a/database/ingredients/product.toml +++ /dev/null @@ -1,21 +0,0 @@ -[[ingredients]] -key = "wheat_flour" -measure = "g" - -ru = "пшеничная мука" -en = "wheat flour" - -[[ingredients]] -key = "olive_oil" -measure = "ml" - -ru = "оливковое масло" -en = "olive oil" - -[[ingredients]] -key = "dry_yeast" -measure = "g" - -ru = "сухие дрожжи" -en = "dry yeast" - diff --git a/database/ingredients/seasoning.toml b/database/ingredients/seasoning.toml deleted file mode 100644 index 8a06308..0000000 --- a/database/ingredients/seasoning.toml +++ /dev/null @@ -1,15 +0,0 @@ - -[[ingredients]] -key = "salt" -measure = "g" - -ru = "соль" -en = "salt" - -[[ingredients]] -key = "sugar" -measure = "g" - -ru = "сахар" -en = "sugar" - diff --git a/database/ingredients/vegetable.toml b/database/ingredients/vegetable.toml deleted file mode 100644 index d2aea8f..0000000 --- a/database/ingredients/vegetable.toml +++ /dev/null @@ -1,14 +0,0 @@ -[[ingredients]] -key = "carrot" -measure = "kg" - -ru = "морковь" -en = "carrot" - -[[ingredients]] -key = "potato" -measure = "kg" - -ru = "картофель" -en = "potato" - diff --git a/db/Cargo.lock b/db/Cargo.lock new file mode 100644 index 0000000..93f7c0b --- /dev/null +++ b/db/Cargo.lock @@ -0,0 +1,75 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "db" +version = "0.1.0" +dependencies = [ + "serde", + "toml", +] + +[[package]] +name = "proc-macro2" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9027b48e9d4c9175fa2218adf3557f91c1137021739951d4932f5f8268ac48aa" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.137" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.137" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f26faba0c3959972377d3b2d306ee9f71faee9714294e41bb777f83f88578be" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "1.0.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ff7c592601f11445996a06f8ad0c27f094a58857c2f89e97974ab9235b92c52" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "toml" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" +dependencies = [ + "serde", +] + +[[package]] +name = "unicode-xid" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04" diff --git a/db/Cargo.toml b/db/Cargo.toml new file mode 100644 index 0000000..94560e9 --- /dev/null +++ b/db/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "db" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[build-dependencies] +serde = { version = "1.0.137", features = ["derive"] } +toml = "0.5.9" diff --git a/db/build.rs b/db/build.rs new file mode 100644 index 0000000..2109b2d --- /dev/null +++ b/db/build.rs @@ -0,0 +1,181 @@ +use std::fs; +use std::io::{BufWriter, Write}; + +use serde::Deserialize; + +#[derive(Deserialize, Debug)] +pub struct Main { + ingredients: Option>, + recipes: Option>, +} + +#[derive(Deserialize, Debug)] +pub struct Ingredient { + key: String, + translates: IngredientTranslate, +} + +#[derive(Deserialize, Debug)] +pub struct IngredientTranslate { + ru: String, + en: Option, +} + +#[derive(Deserialize, Debug)] +pub struct Recipe { + key: String, + ingredients: Vec, + steps: u8, + translates: RecipeTranslates, +} + +#[derive(Deserialize, Debug)] +pub struct RecipeIngredient { + key: String, + #[serde(flatten)] + measure: RecipeIngredientMeasure, +} + +#[derive(Deserialize, Debug)] +pub enum RecipeIngredientMeasure { + #[serde(rename = "g")] + Gram(u32), + #[serde(rename = "kg")] + KiloGram(u32), + #[serde(rename = "ml")] + MilliLiter(u32), + #[serde(rename = "l")] + Liter(u32), +} + +#[derive(Deserialize, Debug)] +pub struct RecipeTranslates { + ru: RecipeTranslate, + en: Option, +} + +#[derive(Deserialize, Debug)] +pub struct RecipeTranslate { + name: String, + instructions: Vec, +} + +fn write_structs(file: &mut BufWriter) -> Result<(), std::io::Error> { + writeln!(file, "#[derive(Debug)]")?; + writeln!(file, "pub struct Ingredient {{")?; + writeln!(file, " pub key: &'static str,")?; + writeln!(file, " translates: IngredientTranslate,")?; + writeln!(file, "}}")?; + + writeln!(file, "#[derive(Debug)]")?; + writeln!(file, "pub struct IngredientTranslate {{")?; + writeln!(file, " ru: &'static str,")?; + writeln!(file, " en: Option<&'static str>,")?; + writeln!(file, "}}")?; + + writeln!(file, "#[derive(Debug)]")?; + writeln!(file, "pub struct Recipe {{")?; + writeln!(file, " key: &'static str,")?; + writeln!(file, " ingredients: Vec,")?; + writeln!(file, " steps: u8,")?; + writeln!(file, " translates: RecipeTranslates,")?; + writeln!(file, "}}")?; + + writeln!(file, "#[derive(Debug)]")?; + writeln!(file, "pub struct RecipeIngredient {{")?; + writeln!(file, " key: &'static str,")?; + writeln!(file, " measure: RecipeIngredientMeasure,")?; + writeln!(file, "}}")?; + + writeln!(file, "#[derive(Debug)]")?; + writeln!(file, "pub enum RecipeIngredientMeasure {{")?; + writeln!(file, " Gram(u32),")?; + writeln!(file, " KiloGram(u32),")?; + writeln!(file, " MilliLiter(u32),")?; + writeln!(file, " Liter(u32),")?; + writeln!(file, "}}")?; + + writeln!(file, "#[derive(Debug)]")?; + writeln!(file, "pub struct RecipeTranslates {{")?; + writeln!(file, " ru: RecipeTranslate,")?; + writeln!(file, " en: Option,")?; + writeln!(file, "}}")?; + + writeln!(file, "#[derive(Debug)]")?; + writeln!(file, "pub struct RecipeTranslate {{")?; + writeln!(file, " name: &'static str,")?; + writeln!(file, " instructions: Vec<&'static str>,")?; + writeln!(file, "}}")?; + Ok(()) +} + +fn write_ingredients(file: &mut BufWriter) -> Result<(), std::io::Error> { + // fs::read_dir("data/ingredients") + let fruit = fs::read_to_string("data/ingredients/fruit.toml")?; + let cfg: Main = toml::from_str(&fruit).unwrap(); + + let mut ins: usize = 0; + + writeln!( + file, + "pub const INGREDIENTS: [Ingredient; {}] = [", + cfg.ingredients + .as_ref() + .map(|i| i.len()) + .unwrap_or_default() + )?; + ins += 1; + if let Some(ingredients) = cfg.ingredients { + for ingredient in ingredients { + writeln!(file, "{}Ingredient {{", indent(ins))?; + ins += 1; + writeln!(file, "{}key: {:?},", indent(ins), to_str(ingredient.key))?; + writeln!(file, "{}translates: IngredientTranslate {{", indent(ins))?; + ins += 1; + writeln!( + file, + "{}ru: {:?},", + indent(ins), + to_str(ingredient.translates.ru) + )?; + writeln!( + file, + "{}en: {:?},", + indent(ins), + ingredient.translates.en.map(to_str) + )?; + ins -= 1; + writeln!(file, "{}}}", indent(ins))?; + ins -= 1; + writeln!(file, "{}}},", indent(ins))?; + } + } + ins -= 1; + writeln!(file, "{}];", indent(ins))?; + + Ok(()) +} + +fn gen_data_mod() -> Result<(), std::io::Error> { + let file = fs::File::create("src/data.rs")?; + let mut buf = BufWriter::new(file); + write_structs(&mut buf)?; + println!("cargo:rerun-if-changed=data"); + write_ingredients(&mut buf)?; + Ok(()) +} + +fn indent(indent_size: usize) -> String { + std::iter::repeat(" ").take(indent_size * 4).collect() +} + +fn to_str(string: String) -> &'static str { + Box::leak(string.into_boxed_str()) +} + +fn main() { + println!("cargo:rerun-if-changed=build.rs"); + if let Err(e) = gen_data_mod() { + eprintln!("Error: {}", e); + } +} diff --git a/db/data/ingredients/fruit.toml b/db/data/ingredients/fruit.toml new file mode 100644 index 0000000..17d201c --- /dev/null +++ b/db/data/ingredients/fruit.toml @@ -0,0 +1,20 @@ +[[ingredients]] +key = "apple" +measure = "kg" + [ingredients.translates] + ru = "яблоко" + en = "apple" + +[[ingredients]] +key = "banana" +measure = "kg" + [ingredients.translates] + ru = "банан" + en = "banana" + +[[ingredients]] +key = "orange" +measure = "kg" + [ingredients.translates] + ru = "апельсин" + en = "orange" diff --git a/db/data/ingredients/liquid.toml b/db/data/ingredients/liquid.toml new file mode 100644 index 0000000..505d54d --- /dev/null +++ b/db/data/ingredients/liquid.toml @@ -0,0 +1,8 @@ + +[[ingredients]] +key = "water" +measure = "ml" + [ingredients.translates] + ru = "вода" + en = "water" + diff --git a/db/data/ingredients/product.toml b/db/data/ingredients/product.toml new file mode 100644 index 0000000..db0c9ef --- /dev/null +++ b/db/data/ingredients/product.toml @@ -0,0 +1,21 @@ +[[ingredients]] +key = "wheat_flour" +measure = "g" + [ingredients.translates] + ru = "пшеничная мука" + en = "wheat flour" + +[[ingredients]] +key = "olive_oil" +measure = "ml" + [ingredients.translates] + ru = "оливковое масло" + en = "olive oil" + +[[ingredients]] +key = "dry_yeast" +measure = "g" + [ingredients.translates] + ru = "сухие дрожжи" + en = "dry yeast" + diff --git a/db/data/ingredients/seasoning.toml b/db/data/ingredients/seasoning.toml new file mode 100644 index 0000000..026e9b3 --- /dev/null +++ b/db/data/ingredients/seasoning.toml @@ -0,0 +1,15 @@ + +[[ingredients]] +key = "salt" +measure = "g" + [ingredients.translates] + ru = "соль" + en = "salt" + +[[ingredients]] +key = "sugar" +measure = "g" + [ingredients.translates] + ru = "сахар" + en = "sugar" + diff --git a/db/data/ingredients/vegetable.toml b/db/data/ingredients/vegetable.toml new file mode 100644 index 0000000..84344f3 --- /dev/null +++ b/db/data/ingredients/vegetable.toml @@ -0,0 +1,14 @@ +[[ingredients]] +key = "carrot" +measure = "kg" + [ingredients.translates] + ru = "морковь" + en = "carrot" + +[[ingredients]] +key = "potato" +measure = "kg" + [ingredienst.translates] + ru = "картофель" + en = "potato" + diff --git a/database/recipes/pizza.toml b/db/data/recipes/pizza.toml similarity index 87% rename from database/recipes/pizza.toml rename to db/data/recipes/pizza.toml index e250472..eb6138a 100644 --- a/database/recipes/pizza.toml +++ b/db/data/recipes/pizza.toml @@ -2,12 +2,12 @@ key = "thin_crispy_pizza_base" ingredients = [ - { key: "seasoning:salt", g: 5 }, - { key: "seasoning:sugar", g: 4 }, - { key: "product:wheat_flour", g: 500 }, - { key: "product:dry_yeast", g: 7 }, - { key: "product:olive_oil", ml: 25 }, - { key: "liquid:water", ml: 250 }, + { key = "salt", g = 5 }, + { key = "sugar", g = 4 }, + { key = "wheat_flour", g = 500 }, + { key = "dry_yeast", g = 7 }, + { key = "olive_oil", ml = 25 }, + { key = "water", ml = 250 }, ] steps = 7 diff --git a/db/src/config.rs b/db/src/config.rs new file mode 100644 index 0000000..e69de29 diff --git a/db/src/data.rs b/db/src/data.rs new file mode 100644 index 0000000..51fbbf2 --- /dev/null +++ b/db/src/data.rs @@ -0,0 +1,62 @@ +#[derive(Debug)] +pub struct Ingredient { + pub key: &'static str, + translates: IngredientTranslate, +} +#[derive(Debug)] +pub struct IngredientTranslate { + ru: &'static str, + en: Option<&'static str>, +} +#[derive(Debug)] +pub struct Recipe { + key: &'static str, + ingredients: Vec, + steps: u8, + translates: RecipeTranslates, +} +#[derive(Debug)] +pub struct RecipeIngredient { + key: &'static str, + measure: RecipeIngredientMeasure, +} +#[derive(Debug)] +pub enum RecipeIngredientMeasure { + Gram(u32), + KiloGram(u32), + MilliLiter(u32), + Liter(u32), +} +#[derive(Debug)] +pub struct RecipeTranslates { + ru: RecipeTranslate, + en: Option, +} +#[derive(Debug)] +pub struct RecipeTranslate { + name: &'static str, + instructions: Vec<&'static str>, +} +pub const INGREDIENTS: [Ingredient; 3] = [ + Ingredient { + key: "apple", + translates: IngredientTranslate { + ru: "яблоко", + en: Some("apple"), + } + }, + Ingredient { + key: "banana", + translates: IngredientTranslate { + ru: "банан", + en: Some("banana"), + } + }, + Ingredient { + key: "orange", + translates: IngredientTranslate { + ru: "апельсин", + en: Some("orange"), + } + }, +]; diff --git a/db/src/main.rs b/db/src/main.rs new file mode 100644 index 0000000..7ae3ee3 --- /dev/null +++ b/db/src/main.rs @@ -0,0 +1,17 @@ +use crate::data::Ingredient; + +mod data; + +fn main() { + println!("Hello, world!"); + + dbg!(data::INGREDIENTS + .into_iter() + .filter(|i| i.key == "banana") + .collect::>()); + + // let cfg: config::Main = toml::from_str(include_str!("../data/ingredients/fruit.toml")).unwrap(); + // let cfg: config::Main = toml::from_str(include_str!("../data/recipes/pizza.toml")).unwrap(); + + // dbg!(cfg); +}