Skip to content

Commit 7058136

Browse files
committed
added into vec for allergens
1 parent 9614736 commit 7058136

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/parse/daily_menu/allergens.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ impl Display for AllergenInfo {
7878
}
7979
}
8080

81+
impl Into<Vec<&'static str>> for &AllergenInfo {
82+
fn into(self) -> Vec<&'static str> {
83+
(&self.0).into()
84+
}
85+
}
86+
8187
bitflags! {
8288
#[derive(Debug, PartialEq, Eq)]
8389
pub struct AllergenFlags: u16 {
@@ -99,9 +105,9 @@ bitflags! {
99105
}
100106
}
101107

102-
impl Display for AllergenFlags {
103-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
104-
let allergens = [
108+
impl Into<Vec<&'static str>> for &AllergenFlags {
109+
fn into(self) -> Vec<&'static str> {
110+
static ALLERGENS: [(AllergenFlags, &'static str); 15] = [
105111
(AllergenFlags::Egg, "Egg"),
106112
(AllergenFlags::Fish, "Fish"),
107113
(AllergenFlags::GlutenFriendly, "Gluten Friendly"),
@@ -118,18 +124,25 @@ impl Display for AllergenFlags {
118124
(AllergenFlags::Shellfish, "Shellfish"),
119125
(AllergenFlags::Sesame, "Sesame"),
120126
];
121-
let mut first = true;
122-
for (allergen_flag, allergen_name) in allergens.into_iter() {
123-
if self.contains(allergen_flag) {
124-
if first {
125-
first = false;
127+
ALLERGENS
128+
.iter()
129+
.filter_map(|(allergen_flag, allergen_name)| {
130+
let flag = AllergenFlags::from_bits(allergen_flag.bits())
131+
.expect("AllergenFlags should be valid");
132+
if self.contains(flag) {
133+
Some(*allergen_name)
126134
} else {
127-
write!(f, ", ")?;
135+
None
128136
}
129-
write!(f, "{}", allergen_name)?;
130-
}
131-
}
132-
Ok(())
137+
})
138+
.collect()
139+
}
140+
}
141+
142+
impl Display for AllergenFlags {
143+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
144+
let allergens: Vec<&str> = self.into();
145+
write!(f, "{}", allergens.join(", "))
133146
}
134147
}
135148

0 commit comments

Comments
 (0)