Struct ingredient::IngredientParser
source · pub struct IngredientParser {
pub units: HashSet<String>,
pub adjectives: HashSet<String>,
pub is_rich_text: bool,
}
Fields§
§units: HashSet<String>
§adjectives: HashSet<String>
§is_rich_text: bool
Implementations§
source§impl IngredientParser
impl IngredientParser
pub fn new(is_rich_text: bool) -> Self
sourcepub fn from_str(self, input: &str) -> Ingredient
pub fn from_str(self, input: &str) -> Ingredient
wrapper for [self.parse_ingredient]
use ingredient::{from_str};
assert_eq!(from_str("one whole egg").to_string(),"1 whole egg");
sourcepub fn parse_amount(&self, input: &str) -> Vec<Measure>
pub fn parse_amount(&self, input: &str) -> Vec<Measure>
Parses one or two amounts, e.g. 12 grams
or 120 grams / 1 cup
. Used by [self.parse_ingredient].
use ingredient::{IngredientParser,unit::Measure};
let ip = IngredientParser::new(false);
assert_eq!(
ip.parse_amount("120 grams"),
vec![Measure::parse_new("grams",120.0)]
);
assert_eq!(
ip.parse_amount("120 grams / 1 cup"),
vec![Measure::parse_new("grams",120.0),Measure::parse_new("cup", 1.0)]
);
assert_eq!(
ip.parse_amount("120 grams / 1 cup / 1 whole"),
vec![Measure::parse_new("grams",120.0),Measure::parse_new("cup", 1.0),Measure::parse_new("whole", 1.0)]
);
sourcepub fn parse_ingredient(self, input: &str) -> Res<&str, Ingredient>
pub fn parse_ingredient(self, input: &str) -> Res<&str, Ingredient>
Parse an ingredient line item, such as 120 grams / 1 cup whole wheat flour, sifted lightly
.
returns an Ingredient, Can be used as a wrapper to return verbose errors.
supported formats include:
- 1 g name
- 1 g / 1g name, modifier
- 1 g; 1 g name
- ¼ g name
- 1/4 g name
- 1 ¼ g name
- 1 1/4 g name
- 1 g (1 g) name
- 1 g name (about 1 g; 1 g)
- name
- 1 name
use ingredient::{IngredientParser, ingredient::Ingredient, unit::Measure};
let ip = IngredientParser::new(false);
assert_eq!(
ip.parse_ingredient("1¼ cups / 155.5 grams flour"),
Ok((
"",
Ingredient {
name: "flour".to_string(),
amounts: vec![
Measure::parse_new("cups", 1.25),
Measure::parse_new("grams", 155.5),
],
modifier: None,
}
))
);
Trait Implementations§
source§impl Clone for IngredientParser
impl Clone for IngredientParser
source§fn clone(&self) -> IngredientParser
fn clone(&self) -> IngredientParser
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for IngredientParser
impl Debug for IngredientParser
source§impl Default for IngredientParser
impl Default for IngredientParser
source§fn default() -> IngredientParser
fn default() -> IngredientParser
Returns the “default value” for a type. Read more
source§impl PartialEq<IngredientParser> for IngredientParser
impl PartialEq<IngredientParser> for IngredientParser
source§fn eq(&self, other: &IngredientParser) -> bool
fn eq(&self, other: &IngredientParser) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.