Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

food data tracking #33

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
934 changes: 496 additions & 438 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.2.2"

[dependencies]
config = "0.13.3"
fooddata = { path = "../fooddata" }
reqwest = { workspace = true }
serde_json = "1.0.87"
tracing-subscriber = "0.3.16"
Expand Down
3 changes: 3 additions & 0 deletions crates/cli/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub(crate) mod server;

use std::path::PathBuf;

use server::ServerCommand;

#[derive(clap::Parser, Debug)]
Expand All @@ -25,4 +27,5 @@ pub(crate) struct Command {
#[derive(clap::Subcommand, Debug)]
pub(crate) enum BasicCommands {
Run,
FoodData { file: PathBuf },
}
18 changes: 18 additions & 0 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use annapurna_data::Facts;
use annapurna_logic::recipe;
use clap::Parser;
use std::collections::HashSet;

pub mod commands;
use commands::{BasicCommands, Commands};
Expand All @@ -21,6 +22,23 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Can make: {can_make:?}");
println!("Missing: {missing:?}");
}
BasicCommands::FoodData { file, .. } => {
let data: fooddata::FoundationFoods = {
let contents = tokio::fs::read_to_string(file).await?;
serde_json::from_str(&contents)?
};
let mut nutrients = HashSet::new();
data.foundation_foods.iter().for_each(|food| {
let macros = food.macros();
println!("{}: {macros:?}\n", food.description);
println!("{:?}\n", food.food_portions);
food.food_nutrients.iter().for_each(|nutrient| {
nutrients.insert(nutrient.nutrient.name.clone());
});
});

// nutrients.iter().for_each(|nutrient| println!("{nutrient:?}"));
}
},
Commands::Server(server) => server.run().await?,
}
Expand Down
10 changes: 10 additions & 0 deletions crates/fooddata/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[dependencies]
serde = { workspace = true }

[package]
name = "fooddata"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
7 changes: 7 additions & 0 deletions crates/fooddata/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("todo")]
Todo,
}

pub type Result<T> = std::result::Result<T, Error>;
159 changes: 159 additions & 0 deletions crates/fooddata/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct FoundationFoods {
#[serde(rename = "FoundationFoods")]
pub foundation_foods: Vec<FoodDetails>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct BrandedFoods {
#[serde(rename = "BrandedFoods")]
pub branded_foods: Vec<FoodDetails>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct FoodDetails {
#[serde(rename = "dataType")]
pub data_type: String,
pub description: String,
#[serde(rename = "fdcId")]
pub fdc_id: isize,
#[serde(rename = "foodClass")]
pub food_class: FoodClass,
#[serde(rename = "foodNutrients")]
pub food_nutrients: Vec<FoodNutrient>,
#[serde(rename = "foodPortions")]
pub food_portions: Option<Vec<FoodPortion>>,
}

#[derive(Clone, Debug)]
pub struct Macros {
pub amount: f32,
pub kcal: Option<FoodNutrient>,
pub protein: Option<FoodNutrient>,
pub fat: Option<FoodNutrient>,
pub carbohydrate: Option<FoodNutrient>,
}

impl FoodDetails {
pub fn macros(&self) -> Macros {
let nutrients: HashMap<NutrientName, FoodNutrient> = self.food_nutrients.iter().cloned().filter_map(|nutrient| {
match nutrient.nutrient.name {
NutrientName::Energy => Some((nutrient.nutrient.name.clone(), nutrient.clone())),
NutrientName::TotalLipid => Some((nutrient.nutrient.name.clone(), nutrient.clone())),
NutrientName::CarbohydrateBySummation => Some((nutrient.nutrient.name.clone(), nutrient.clone())),
NutrientName::Protein => Some((nutrient.nutrient.name.clone(), nutrient.clone())),
_ => None,
}
}).collect();
Macros {
amount: 0.0,
kcal: nutrients.get(&NutrientName::Energy).map(|n| n.to_owned()),
protein: nutrients.get(&NutrientName::Protein).map(|n| n.to_owned()),
fat: nutrients.get(&NutrientName::TotalLipid).map(|n| n.to_owned()),
carbohydrate: nutrients.get(&NutrientName::CarbohydrateBySummation).map(|n| n.to_owned()),
}

}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum FoodClass {
Branded,
Composite,
FinalFood,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct FoodPortion {
pub id: isize,
#[serde(rename = "measureUnit")]
pub measure_unit: MeasureUnit,
pub modifier: Option<String>,
#[serde(rename = "gramWeight")]
pub gram_weight: f32,
#[serde(rename = "sequenceNumber")]
pub sequence_number: isize,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MeasureUnit {
pub id: isize,
pub name: String,
pub abbreviation: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct FoodNutrient {
pub amount: Option<f32>,
pub id: isize,
pub nutrient: Nutrient,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Nutrient {
pub id: isize,
pub number: String,
pub name: NutrientName,
pub rank: isize,
#[serde(rename = "unitName")]
pub unit_name: UnitName,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum UnitName {
#[serde(rename = "g")]
Gram,
#[serde(rename = "mg")]
Milligram,
#[serde(rename = "µg")]
Microgram,
#[serde(rename = "kcal")]
Kcal,
#[serde(rename = "kJ")]
Kilojoule,
#[serde(rename = "IU")]
InternationalUnit,
#[serde(rename = "sp gr")]
SpecificGravity,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
pub enum NutrientName {
#[serde(rename = "Carbohydrate, by summation")]
CarbohydrateBySummation,
#[serde(rename = "Carbohydrate, by difference")]
CarbohydrateByDifference,
Cholesterol,
Energy,
#[serde(rename = "Energy (Atwater Specific Factors)")]
EnergyAtwaterSpecific,
#[serde(rename = "Energy (Atwater General Factors)")]
EnergyAtwaterGeneral,
#[serde(rename = "Fatty acids, total saturated")]
FattyAcidsSaturated,
Fructose,
Glucose,
#[serde(rename = "Fiber, insoluble")]
InsolubleFiber,
#[serde(rename = "Fiber, soluble")]
SolubleFiber,
Lactose,
Protein,
#[serde(rename = "Sodium, NA")]
Sodium,
Starch,
#[serde(rename = "Fiber, total dietary")]
TotalDietaryFiber,
#[serde(rename = "Total fat (NLEA)")]
TotalFat,
#[serde(rename = "Total lipid (fat)")]
TotalLipid,
#[serde(rename = "Sugars, Total")]
TotalSugars,
#[serde(untagged)]
Other(String),
}
18 changes: 9 additions & 9 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.