disable macro-error diagnostic for this workspace.

This commit is contained in:
Jeremy Wall 2021-11-02 21:09:00 -04:00
parent 821cc098fb
commit b21bdd0252
4 changed files with 172 additions and 3 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"rust-analyzer.diagnostics.disabled": [
"macro-error"
]
}

View File

@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
mod parse;
pub mod parse;
pub mod unit;
use std::collections::BTreeMap;
@ -20,7 +20,7 @@ use std::str::FromStr;
use chrono::NaiveDate;
use uuid::{self, Uuid};
use parse::{ingredient, measure};
use parse::*;
use unit::*;
#[derive(Debug, Clone, PartialEq)]
@ -141,6 +141,14 @@ impl Step {
}
}
pub fn with_ingredients<Iter>(mut self, ingredients: Iter) -> Step
where
Iter: IntoIterator<Item = Ingredient>,
{
self.add_ingredients(ingredients);
self
}
pub fn add_ingredients<Iter>(&mut self, ingredients: Iter)
where
Iter: IntoIterator<Item = Ingredient>,

View File

@ -21,9 +21,81 @@ use num_rational::Ratio;
use crate::{
unit::{Measure, Measure::*, Quantity, VolumeMeasure::*},
Ingredient,
Ingredient, Recipe, Step,
};
make_fn!(
pub recipe<StrIter, Recipe>,
do_each!(
title => title,
_ => optional!(para_separator),
desc => optional!(do_each!(
_ => peek!(not!(step_prefix)),
desc => description,
(desc)
)),
_ => optional!(para_separator),
steps => step_list,
(Recipe::new(title, desc).with_steps(steps))
)
);
make_fn!(
pub title<StrIter, &str>,
do_each!(
_ => text_token!("title:"),
_ => optional!(ws),
title => until!(text_token!("\n")),
_ => text_token!("\n"),
(title)
)
);
make_fn!(
para_separator<StrIter, &str>,
do_each!(
_ => text_token!("\n"),
_ => optional!(ws),
_ => text_token!("\n"),
("")
)
);
make_fn!(
pub description<StrIter, &str>,
until!(either!(
discard!(para_separator),
eoi,
))
);
make_fn!(
pub step_prefix<StrIter, &str>,
do_each!(
_ => text_token!("step:"),
_ => optional!(ws),
_ => para_separator,
("")
)
);
make_fn!(
pub step<StrIter, Step>,
do_each!(
_ => step_prefix,
ingredients => ingredient_list,
_ => para_separator,
desc => description,
_ => either!(discard!(para_separator), eoi),
(Step::new(None, desc).with_ingredients(ingredients))
)
);
make_fn!(
pub step_list<StrIter, Vec<Step>>,
repeat!(step)
);
make_fn!(ws<StrIter, &str>,
consume_all!(either!(
text_token!(" "),

View File

@ -338,3 +338,87 @@ fn test_ingredient_list_parse() {
}
}
}
#[test]
fn test_single_step() {
let step = "step:
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."
);
}
err => assert!(false, "{:?}", err),
}
}
#[test]
fn test_multiple_steps() {
let steps = "step:
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.
step:
1 tbsp flour
2 tbsp butter
Saute apples in butter until golden brown. Add flour slowly
until thickened. Set aside to cool.
";
match parse::step_list(StrIter::new(steps)) {
ParseResult::Complete(_, steps) => {
assert_eq!(steps.len(), 2);
}
err => assert!(false, "{:?}", err),
}
}
#[test]
fn test_recipe_multiple_steps() {
let recipe = "title: gooey apple bake
A simple gooey apple bake recipe.
step:
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.
step:
1 tbsp flour
2 tbsp butter
Saute apples in butter until golden brown. Add flour slowly
until thickened. Set aside to cool.
";
match parse::recipe(StrIter::new(recipe)) {
ParseResult::Complete(_, recipe) => {
assert_eq!(recipe.steps.len(), 2);
}
err => assert!(false, "{:?}", err),
}
}