fix: translate friendly nutrient keys to OFF keys in log_custom_food
log_custom_food's schema documents keys like 'protein', 'fat', 'salt' but
sum_nutriments() looks for OFF-style keys ('proteins_100g', 'fat_100g',
'salt_100g'). Nutrients passed with friendly keys were silently dropped
from daily totals. Now translates friendly keys to OFF keys before
storing. Both key styles work.
This commit is contained in:
parent
d0b7b003f9
commit
ba706a3f1f
1 changed files with 25 additions and 0 deletions
25
src/tools.rs
25
src/tools.rs
|
|
@ -503,7 +503,32 @@ impl NutritionServer {
|
||||||
|
|
||||||
// Build nutriments JSON — include calories in the energy-kcal_100g field
|
// Build nutriments JSON — include calories in the energy-kcal_100g field
|
||||||
let mut nutriments = params.nutriments.unwrap_or(json!({}));
|
let mut nutriments = params.nutriments.unwrap_or(json!({}));
|
||||||
|
|
||||||
|
// Translate friendly key names (as documented in the schema) to the
|
||||||
|
// OFF-style _100g keys that sum_nutriments() looks for. Callers may
|
||||||
|
// also pass OFF keys directly — those pass through unchanged.
|
||||||
|
let friendly_to_off: &[(&str, &str)] = &[
|
||||||
|
("protein", "proteins_100g"),
|
||||||
|
("carbohydrates", "carbohydrates_100g"),
|
||||||
|
("fat", "fat_100g"),
|
||||||
|
("fiber", "fiber_100g"),
|
||||||
|
("sugars", "sugars_100g"),
|
||||||
|
("saturated_fat", "saturated-fat_100g"),
|
||||||
|
("salt", "salt_100g"),
|
||||||
|
("fructose", "fructose_100g"),
|
||||||
|
("alcohol", "alcohol_100g"),
|
||||||
|
("potassium", "potassium_100g"),
|
||||||
|
("calcium", "calcium_100g"),
|
||||||
|
("magnesium", "magnesium_100g"),
|
||||||
|
("cholesterol", "cholesterol_100g"),
|
||||||
|
];
|
||||||
|
|
||||||
if let Some(obj) = nutriments.as_object_mut() {
|
if let Some(obj) = nutriments.as_object_mut() {
|
||||||
|
for (friendly, off_key) in friendly_to_off {
|
||||||
|
if let Some(val) = obj.remove(*friendly) {
|
||||||
|
obj.entry(off_key.to_string()).or_insert(val);
|
||||||
|
}
|
||||||
|
}
|
||||||
obj.insert("energy-kcal_100g".to_string(), json!(params.calories));
|
obj.insert("energy-kcal_100g".to_string(), json!(params.calories));
|
||||||
} else {
|
} else {
|
||||||
nutriments = json!({"energy-kcal_100g": params.calories});
|
nutriments = json!({"energy-kcal_100g": params.calories});
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue