db: add build script

This commit is contained in:
Dmitriy Pleshevskiy 2022-05-08 00:58:41 +03:00
parent e692bd4b90
commit bd8855fde6
20 changed files with 509 additions and 84 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
target

75
Cargo.lock generated Normal file
View File

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

4
Cargo.toml Normal file
View File

@ -0,0 +1,4 @@
[workspace]
members = [
"db"
]

View File

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

View File

@ -1,8 +0,0 @@
[[ingredients]]
key = "water"
measure = "ml"
ru = "вода"
en = "water"

View File

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

View File

@ -1,15 +0,0 @@
[[ingredients]]
key = "salt"
measure = "g"
ru = "соль"
en = "salt"
[[ingredients]]
key = "sugar"
measure = "g"
ru = "сахар"
en = "sugar"

View File

@ -1,14 +0,0 @@
[[ingredients]]
key = "carrot"
measure = "kg"
ru = "морковь"
en = "carrot"
[[ingredients]]
key = "potato"
measure = "kg"
ru = "картофель"
en = "potato"

75
db/Cargo.lock generated Normal file
View File

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

10
db/Cargo.toml Normal file
View File

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

181
db/build.rs Normal file
View File

@ -0,0 +1,181 @@
use std::fs;
use std::io::{BufWriter, Write};
use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub struct Main {
ingredients: Option<Vec<Ingredient>>,
recipes: Option<Vec<Recipe>>,
}
#[derive(Deserialize, Debug)]
pub struct Ingredient {
key: String,
translates: IngredientTranslate,
}
#[derive(Deserialize, Debug)]
pub struct IngredientTranslate {
ru: String,
en: Option<String>,
}
#[derive(Deserialize, Debug)]
pub struct Recipe {
key: String,
ingredients: Vec<RecipeIngredient>,
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<RecipeTranslate>,
}
#[derive(Deserialize, Debug)]
pub struct RecipeTranslate {
name: String,
instructions: Vec<String>,
}
fn write_structs(file: &mut BufWriter<fs::File>) -> 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<RecipeIngredient>,")?;
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<RecipeTranslate>,")?;
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<fs::File>) -> 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);
}
}

View File

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

View File

@ -0,0 +1,8 @@
[[ingredients]]
key = "water"
measure = "ml"
[ingredients.translates]
ru = "вода"
en = "water"

View File

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

View File

@ -0,0 +1,15 @@
[[ingredients]]
key = "salt"
measure = "g"
[ingredients.translates]
ru = "соль"
en = "salt"
[[ingredients]]
key = "sugar"
measure = "g"
[ingredients.translates]
ru = "сахар"
en = "sugar"

View File

@ -0,0 +1,14 @@
[[ingredients]]
key = "carrot"
measure = "kg"
[ingredients.translates]
ru = "морковь"
en = "carrot"
[[ingredients]]
key = "potato"
measure = "kg"
[ingredienst.translates]
ru = "картофель"
en = "potato"

View File

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

0
db/src/config.rs Normal file
View File

62
db/src/data.rs Normal file
View File

@ -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<RecipeIngredient>,
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<RecipeTranslate>,
}
#[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"),
}
},
];

17
db/src/main.rs Normal file
View File

@ -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::<Vec<data::Ingredient>>());
// 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);
}