Handle Step Durations

This commit is contained in:
Jeremy Wall 2021-11-21 15:09:13 -05:00
parent cae5f059e8
commit 30b800ce09
2 changed files with 67 additions and 4 deletions

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::str::FromStr; use std::str::FromStr;
use std::time::Duration;
use abortable_parser::{ use abortable_parser::{
ascii_digit, consume_all, discard, do_each, either, eoi, make_fn, must, not, optional, peek, ascii_digit, consume_all, discard, do_each, either, eoi, make_fn, must, not, optional, peek,
@ -78,24 +79,58 @@ make_fn!(
); );
make_fn!( make_fn!(
pub step_prefix<StrIter, &str>, pub step_time<StrIter, Duration>,
do_each!(
cnt => num,
_ => optional!(ws),
u => either!(
text_token!("ms"),
text_token!("sec"),
text_token!("s"),
text_token!("min"),
text_token!("m"),
text_token!("hrs"),
text_token!("hr"),
text_token!("h")
),
(
Duration::from_secs(
match u {
"ms" => (cnt / 1000),
"s" | "sec" => cnt.into(),
"m" | "min" => (dbg!(cnt) * 60),
"h" | "hr" | "hrs" => (cnt * 60 * 60),
_ => unreachable!(),
}.into()
)
)
)
);
make_fn!(
pub step_prefix<StrIter, Option<Duration>>,
do_each!( do_each!(
_ => text_token!("step:"), _ => text_token!("step:"),
dur => optional!(do_each!(
_ => ws,
dur => step_time,
(dbg!(dur))
)),
_ => optional!(ws), _ => optional!(ws),
_ => para_separator, _ => para_separator,
("") (dur)
) )
); );
make_fn!( make_fn!(
pub step<StrIter, Step>, pub step<StrIter, Step>,
do_each!( do_each!(
_ => step_prefix, dur => step_prefix,
ingredients => must!(ingredient_list), ingredients => must!(ingredient_list),
_ => para_separator, _ => para_separator,
desc => description, desc => description,
_ => either!(discard!(para_separator), eoi), _ => either!(discard!(para_separator), eoi),
(Step::new(None, desc).with_ingredients(ingredients)) (Step::new(dur, desc).with_ingredients(ingredients))
) )
); );

View File

@ -361,6 +361,34 @@ until thickens. Set aside to cool."
} }
} }
#[test]
fn test_single_step_with_duration() {
let step = "step: 30 min
1 tbsp flour
2 tbsp butter
1 cup apple (chopped)
Saute apples in butter until golden brown. Add flour slowly
until thickens. Set aside to cool.";
match parse::step(StrIter::new(step)) {
ParseResult::Complete(_, step) => {
assert_eq!(step.ingredients.len(), 3);
assert_eq!(
step.instructions,
"Saute apples in butter until golden brown. Add flour slowly
until thickens. Set aside to cool."
);
assert_eq!(
step.prep_time.unwrap(),
std::time::Duration::new(30 * 60, 0)
)
}
err => assert!(false, "{:?}", err),
}
}
#[test] #[test]
fn test_multiple_steps() { fn test_multiple_steps() {
let steps = "step: let steps = "step: