diff --git a/kitchen/src/main.rs b/kitchen/src/main.rs index ae49df0..7ffc1dc 100644 --- a/kitchen/src/main.rs +++ b/kitchen/src/main.rs @@ -40,8 +40,8 @@ where fn output_recipe_info(r: Recipe, print_ingredients: bool) { println!("Title: {}", r.title); println!(""); - println!("Ingredients:"); if print_ingredients { + println!("Ingredients:"); for (_, ing) in r.get_ingredients() { print!("\t* {}", ing.amt); println!(" {}", ing.name); diff --git a/recipes/src/parse.rs b/recipes/src/parse.rs index f28fd2c..d17ffd3 100644 --- a/recipes/src/parse.rs +++ b/recipes/src/parse.rs @@ -14,8 +14,8 @@ use std::str::FromStr; use abortable_parser::{ - ascii_digit, ascii_ws, consume_all, discard, do_each, either, eoi, make_fn, not, optional, - peek, repeat, separated, text_token, trap, until, Result, StrIter, + ascii_digit, ascii_ws, consume_all, discard, do_each, either, eoi, make_fn, must, not, + optional, peek, repeat, separated, text_token, trap, until, Result, StrIter, }; use num_rational::Ratio; @@ -91,7 +91,7 @@ make_fn!( pub step, do_each!( _ => step_prefix, - ingredients => ingredient_list, + ingredients => must!(ingredient_list), _ => para_separator, desc => description, _ => either!(discard!(para_separator), eoi), @@ -101,7 +101,15 @@ make_fn!( make_fn!( pub step_list>, - repeat!(step) + do_each!( + first_step => must!(step), + rest => repeat!(step), + ({ + let mut steps = vec![first_step]; + steps.extend(rest); + steps + }) + ) ); make_fn!(ws, @@ -148,6 +156,8 @@ make_fn!(unit, text_token!("floz"), text_token!("ml"), text_token!("ltr"), + text_token!("lb"), + text_token!("oz"), text_token!("cup"), text_token!("qrt"), text_token!("pint"), @@ -212,6 +222,9 @@ pub fn measure(i: StrIter) -> abortable_parser::Result { Some("qrt") | Some("quart") => Volume(Qrt(qty)), Some("pint") | Some("pnt") => Volume(Pint(qty)), Some("cnt") | Some("count") => Count(qty), + Some("lb") => Measure::lb(qty), + Some("oz") => Measure::oz(qty), + Some("kg") => Measure::kilogram(qty), Some("g") => Gram(qty), Some("gram") => Gram(qty), Some(u) => { @@ -237,8 +250,10 @@ pub fn measure(i: StrIter) -> abortable_parser::Result { make_fn!( pub ingredient_name, do_each!( - name => until!(ascii_ws), - _ => ws, + name => until!(either!( + discard!(text_token!("\n")), + eoi, + discard!(text_token!("(")))), (name) ) ); @@ -260,6 +275,7 @@ make_fn!( measure => measure, name => ingredient_name, modifier => optional!(ingredient_modifier), + _ => optional!(ws), (Ingredient::new(name, modifier.map(|s| s.to_owned()), measure, "")) ) ); diff --git a/recipes/src/test.rs b/recipes/src/test.rs index c983b2e..2fb2e4f 100644 --- a/recipes/src/test.rs +++ b/recipes/src/test.rs @@ -418,6 +418,7 @@ until thickened. Set aside to cool. match parse::recipe(StrIter::new(recipe)) { ParseResult::Complete(_, recipe) => { assert_eq!(recipe.steps.len(), 2); + assert_eq!(recipe.steps[0].ingredients.len(), 3); } err => assert!(false, "{:?}", err), } diff --git a/recipes/src/unit.rs b/recipes/src/unit.rs index 44b8734..fd265ed 100644 --- a/recipes/src/unit.rs +++ b/recipes/src/unit.rs @@ -263,6 +263,21 @@ impl Measure { Gram(qty) } + pub fn kilogram(qty: Quantity) -> Self { + Gram(qty * Quantity::Whole(1000)) + } + + // TODO(jwall): Should these be separate units with conversions? + pub fn lb(qty: Quantity) -> Self { + // This is an approximation obviously + Gram(qty * (Quantity::Whole(453) + Quantity::Frac(Ratio::new(6, 10)))) + } + + pub fn oz(qty: Quantity) -> Self { + // This is an approximation obviously + Gram(qty * (Quantity::Whole(28) + Quantity::Frac(Ratio::new(4, 10)))) + } + pub fn measure_type(&self) -> String { match self { Volume(_) => "Volume",