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
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:
```json
{
"name": <name>,
"calories": [<value_in_kJ>, <value_in_kcal>],
"fats": <fats_in_g>,
"carbs": <carbs_in_g>,
"proteins": <proteins_in_g>,
"nbr_of_portions": <portions>
```rust
Food {
name: <name>,
calories: [<value_in_kJ>, <value_in_kcal>],
fats: <fats_in_g>,
carbs: <carbs_in_g>,
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):
@ -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...).
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
- [serde](https://crates.io/crates/serde)
- [serde JSON](https://crates.io/crates/serde_json)
- [serde_derive](https://crates.io/crates/serde_derive)
- [json](https://docs.rs/json/0.12.4/json/) crate
### Usage
@ -47,28 +55,27 @@ Here is a program to test your function:
```rust
use macro_calculator::*;
fn main() {
let a = serde_json::json!(
{
"name": "light milk",
"calories": ["148kJ", "35kcal"],
"protein": 3.5,
"fats": 0.1,
"carbs": 5.0,
"nbr_of_portions": 0.7
});
let b = serde_json::json!({
"name": "oat cookies",
"calories": ["1996kJ", "477kcal"],
"protein": 8.2,
"fats": 21.0,
"carbs": 60.4,
"nbr_of_portions": 1.2,
});
let macros: Macros = serde_json::from_value(calculate_macros(vec![a, b])).unwrap();
println!("{:?}", macros);
fn main(){
let a = vec![
Food {
name: String::from("big mac"),
calories: ["2133.84kJ".to_string(), "510kcal".to_string()],
protein: 27.0,
fats: 26.0,
carbs: 41.0,
nbr_of_portions: 2.0,
},
Food {
name: "pizza margherita".to_string(),
calories: ["1500.59kJ".to_string(), "358.65kcal".to_string()],
protein: 13.89,
fats: 11.21,
carbs: 49.07,
nbr_of_portions: 4.9,
},
];
println!("{:#}", calculate_macros(a));
}
```
@ -76,6 +83,11 @@ And its output:
```sh
$ 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