| Crates.io | cooklang-reports |
| lib.rs | cooklang-reports |
| version | 0.2.0 |
| created_at | 2025-05-30 10:21:14.472745+00 |
| updated_at | 2025-08-28 05:26:53.347252+00 |
| description | A Rust library for generating reports from Cooklang recipes using Jinja2-style templates |
| homepage | |
| repository | https://github.com/cooklang/cooklang-reports |
| max_upload_size | |
| id | 1694883 |
| size | 191,161 |
A WIP Rust library for generating reports from Cooklang recipes using Jinja2-style templates.
Add this to your Cargo.toml:
[dependencies]
cooklang-reports = "0.1.0"
use indoc::indoc;
use cooklang_reports::render_template;
let recipe = r#"
Mix @eggs{3%large} with @milk{250%ml}, add @flour{125%g} to make batter.
Add @sugar{1.5%tbsp} and @salt{1/4%tsp} for flavor.
"#;
let template = indoc! {"
# Ingredients ({{ scale }}x)
{%- for ingredient in ingredients %}
- {{ ingredient.name }}
{%- endfor %}
"};
// Test default scaling (1x)
let result = render_template(&recipe, template).unwrap();
let expected = indoc! {"
# Ingredients (1.0x)
- eggs
- milk
- flour
- sugar
- salt"};
assert_eq!(result, expected);
use indoc::indoc;
use std::path::Path;
use cooklang_reports::render_template_with_config;
use cooklang_reports::config::Config;
let recipe = r#"
Mix @eggs{3%large} with @milk{250%ml}, add @flour{125%g} to make batter.
Add @sugar{1.5%tbsp} and @salt{1/4%tsp} for flavor.
"#;
let datastore_path = Path::new("test/data/db");
let template = indoc! {"
# Eggs Info
Density: {{ db('eggs.meta.density') }}
Shelf Life: {{ db('eggs.meta.storage.shelf life') }} days
Fridge Life: {{ db('eggs.meta.storage.fridge life') }} days
"};
let config = Config::builder().datastore_path(datastore_path).build();
let result = render_template_with_config(&recipe, template, &config).unwrap();
let expected = indoc! {"
# Eggs Info
Density: 1.03
Shelf Life: 30 days
Fridge Life: 60 days"};
assert_eq!(result, expected);
ingredients: List of recipe ingredients with their quantities and unitsscale: Current recipe scale factorrecipe_template: Full recipe template object with additional methodsdb(key_path): Access data from the YAML datastore
directory.file.key.subkeydb('eggs.meta.storage.shelf life')quantity: Format ingredient quantities with proper spacing
{{ ingredient.quantity | quantity }}src/
├── lib.rs # Main library code
├── filters/ # Template filters
│ ├── mod.rs
│ └── quantity.rs
└── functions/ # Template functions
├── mod.rs
└── datastore.rs
The datastore is a directory of YAML files organized by ingredient:
datastore/
├── eggs/
│ ├── meta.yml
│ └── shopping.yml
├── milk/
│ ├── meta.yml
│ └── shopping.yml
└── flour/
├── meta.yml
└── shopping.yml
Example YAML file (eggs/meta.yml):
density: 1.03
storage:
shelf life: 30
fridge life: 60
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MPL 2.0 License - see the LICENSE file for details.