Browse Source

fixes regarding switching of crates used

content-update
OGordoo 3 years ago
parent
commit
f0c1a52cf6
  1. 90
      subjects/macro_calculator/README.md

90
subjects/macro_calculator/README.md

@ -2,22 +2,20 @@
### Instructions ### Instructions
Create a **function** `calculate_macros` which receives a vector of `serde_json::Value` and returns a `serde_json::Value`. Create a **function** `calculate_macros` which receives a vector of `Food` structures and returns a `json::JsonValue`.
The vector the function will receive will contain jsons in the following format: ```rust
Food {
```json name: <name>,
{ calories: [<value_in_kJ>, <value_in_kcal>],
"name": <name>, fats: <fats_in_g>,
"calories": [<value_in_kJ>, <value_in_kcal>], carbs: <carbs_in_g>,
"fats": <fats_in_g>, proteins: <proteins_in_g>,
"carbs": <carbs_in_g>, nbr_of_portions: <portions>
"proteins": <proteins_in_g>,
"nbr_of_portions": <portions>
} }
``` ```
The name and the content of the calories array will be `strings`, all other values will be `floats`. Both values in the calories array will be `strings`, all other values will be `f64`s.
The function should return a json with the following format (Macros struct): The function should return a json with the following format (Macros struct):
@ -32,13 +30,23 @@ The number of portions should be taken into account. The values of the macros re
All values should be floats (f64) and should be the addition of every macronutrient in the provided array (cals is the addition of every calories, fats is the addition of every fats, and so on...). All values should be floats (f64) and should be the addition of every macronutrient in the provided array (cals is the addition of every calories, fats is the addition of every fats, and so on...).
Every value should be rounded to two decimal places (ex: 12.29) or one decimal place if it ends in 0 (ex: 18.90 -> 18.9). Every value should be rounded to two decimal places (ex: 12.29) or one decimal place if it ends in 0 (ex: 18.90 -> 18.9).
Hint: You will need the `serde`, `serde_json` and `serde_derive` crates. Hint: You will need the `json` crate.
### Expected Function
```rust
pub struct Food {
//expected public fields
}
pub fn calculate_macros(foods: Vec<Food>) -> json::JsonValue {
}
```
### Notions ### Notions
- [serde](https://crates.io/crates/serde) - [json](https://docs.rs/json/0.12.4/json/) crate
- [serde JSON](https://crates.io/crates/serde_json)
- [serde_derive](https://crates.io/crates/serde_derive)
### Usage ### Usage
@ -47,28 +55,27 @@ Here is a program to test your function:
```rust ```rust
use macro_calculator::*; use macro_calculator::*;
fn main() { fn main(){
let a = serde_json::json!( let a = vec![
{ Food {
"name": "light milk", name: String::from("big mac"),
"calories": ["148kJ", "35kcal"], calories: ["2133.84kJ".to_string(), "510kcal".to_string()],
"protein": 3.5, protein: 27.0,
"fats": 0.1, fats: 26.0,
"carbs": 5.0, carbs: 41.0,
"nbr_of_portions": 0.7 nbr_of_portions: 2.0,
}); },
Food {
let b = serde_json::json!({ name: "pizza margherita".to_string(),
"name": "oat cookies", calories: ["1500.59kJ".to_string(), "358.65kcal".to_string()],
"calories": ["1996kJ", "477kcal"], protein: 13.89,
"protein": 8.2, fats: 11.21,
"fats": 21.0, carbs: 49.07,
"carbs": 60.4, nbr_of_portions: 4.9,
"nbr_of_portions": 1.2, },
}); ];
let macros: Macros = serde_json::from_value(calculate_macros(vec![a, b])).unwrap(); println!("{:#}", calculate_macros(a));
println!("{:?}", macros);
} }
``` ```
@ -76,6 +83,11 @@ And its output:
```sh ```sh
$ cargo run $ cargo run
Macros { cals: 596.9, carbs: 75.98, proteins: 12.29, fats: 25.27 } {
"cals": 2777.39,
"carbs": 322.44,
"proteins": 122.06,
"fats": 106.93
}
$ $
``` ```

Loading…
Cancel
Save