use minijinja::{State, Value};
pub fn excluding_pantry(state: &State, ingredients: Value) -> Value {
let pantry_content = state
.lookup("pantry_content")
.and_then(|v| v.as_str().map(String::from));
if let Some(content) = pantry_content {
let parse_result = cooklang::pantry::parse_lenient(&content);
if let Some(pantry_conf) = parse_result.output() {
let mut filtered = Vec::new();
if let Ok(iter) = ingredients.try_iter() {
for item in iter {
if let Ok(name) = item.get_attr("name")
&& let Some(name_str) = name.as_str() {
let in_pantry = pantry_conf.has_ingredient(name_str);
if !in_pantry {
filtered.push(item);
}
}
}
}
Value::from(filtered)
} else {
eprintln!("Warning: Failed to parse pantry configuration. Returning all ingredients.");
ingredients
}
} else {
ingredients
}
}
#[allow(clippy::needless_pass_by_value)]
pub fn from_pantry(state: &State, ingredients: Value) -> Value {
let pantry_content = state
.lookup("pantry_content")
.and_then(|v| v.as_str().map(String::from));
if let Some(content) = pantry_content {
let parse_result = cooklang::pantry::parse_lenient(&content);
if let Some(pantry_conf) = parse_result.output() {
let mut filtered = Vec::new();
if let Ok(iter) = ingredients.try_iter() {
for item in iter {
if let Ok(name) = item.get_attr("name")
&& let Some(name_str) = name.as_str() {
let in_pantry = pantry_conf.has_ingredient(name_str);
if in_pantry {
filtered.push(item);
}
}
}
}
Value::from(filtered)
} else {
eprintln!("Warning: Failed to parse pantry configuration. Returning empty list.");
Value::from(Vec::<Value>::new())
}
} else {
Value::from(Vec::<Value>::new())
}
}