diff --git a/Sources/OpenFoodFacts/Schemas/Ingredient.swift b/Sources/OpenFoodFacts/Schemas/Ingredient.swift index aa64ffa..4043dcf 100644 --- a/Sources/OpenFoodFacts/Schemas/Ingredient.swift +++ b/Sources/OpenFoodFacts/Schemas/Ingredient.swift @@ -7,6 +7,9 @@ public struct Ingredient: Codable, ObjectDebugger { public var text: String? = nil public var vegan: String? = nil public var vegetarian: String? = nil + public var ciqualFoodCode: String? = nil + public var ecobalyseCode: String? = nil + public var percentEstimate: Float? = nil private enum CodingKeys: String, CodingKey { case fromPalmOil = "from_palm_oil" @@ -17,5 +20,12 @@ public struct Ingredient: Codable, ObjectDebugger { case text case vegan case vegetarian + case ciqualFoodCode = "ciqual_food_code" + case ecobalyseCode = "ecobalyse_code" + case percentEstimate = "percent_estimate" + } + + public var isWater: Bool { + ciqualFoodCode == "18066" } } diff --git a/Sources/OpenFoodFacts/Schemas/Product.swift b/Sources/OpenFoodFacts/Schemas/Product.swift index 298834a..cabbdc3 100644 --- a/Sources/OpenFoodFacts/Schemas/Product.swift +++ b/Sources/OpenFoodFacts/Schemas/Product.swift @@ -1,3 +1,5 @@ +import Units + public class Product: Codable, ObjectDebugger { // public var images: Images? = Images() public var ingredients: [Ingredient]? = [] @@ -200,6 +202,23 @@ public class Product: Codable, ObjectDebugger { public var vitaminsPrevTags: [String]? public var vitaminsTags: [String]? + public var waterQuantity: Units.UnitValue? { + guard let ingredient = ingredients?.first(where: { $0.isWater }) + else { + return nil + } + + guard let quantity = productQuantity, + let estimate = ingredient.percentEstimate, + let rawUnit = productQuantityUnit, + let unit = Units.Unit(rawValue: rawUnit) + else { + return nil + } + + return Double(quantity * (estimate / 100))[unit] + } + private enum CodingKeys: String, CodingKey { // case images case ingredients