-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
calories.rs
131 lines (121 loc) · 5.6 KB
/
calories.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use rustybeer::calculators::calorie_counter::{
calculate_alcohol_calories, calculate_carbs_calories, calculate_total_calories,
};
use rustybeer::calculators::num_bottles::bottles;
use rustybeer::{
abv_calories::{Criteria, ABV_CALORIES},
conversions::{MassParser, RelativeDensity, RelativeDensityParser, VolumeParser},
measurements::Volume,
};
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(name = "calories", author = "Roger Yu (roger.yu27 [at] gmail.com)")]
/// Calculates calories by volume from original and final gravity or from alcohol by volume
pub struct CaloriesOptions {
#[structopt(short, long, parse(try_from_str = RelativeDensityParser::parse))]
/// Original gravity
og: Option<RelativeDensity>,
#[structopt(short, long, requires("og"), required_unless("abv"), parse(try_from_str = RelativeDensityParser::parse))]
/// Final gravity
fg: Option<RelativeDensity>,
#[structopt(short, long, required_unless("fg"))]
/// Alcohol by volume
abv: Option<f64>,
#[structopt(short, long, parse(try_from_str = VolumeParser::parse))]
/// Volume
volume: Option<Volume>,
}
pub fn calculate_and_print(calories: CaloriesOptions) {
let conversion = MassParser::parse("12oz").unwrap().as_grams();
if calories.og.is_some() && calories.fg.is_some() {
let fg = calories.fg.unwrap();
let og = calories.og.unwrap();
if let Some(volume) = calories.volume {
let volume = volume.as_milliliters();
let ac = calculate_alcohol_calories(&og, &fg) / conversion * volume;
let cc = calculate_carbs_calories(&og, &fg) / conversion * volume;
let tc = calculate_total_calories(&og, &fg) / conversion * volume;
println!("Estimated calories for: {} ml", volume);
println!("==============================");
println!("| {:<8} | {:<6} | {:<6} |", "", "kcal:", "kJ:");
println!("| {:<8} | {:>6.0} | {:>6.0} |", "Alcohol:", ac, ac * 4.184);
println!("| {:<8} | {:>6.0} | {:>6.0} |", "Carbs:", cc, cc * 4.184);
println!("| {:<8} | {:>6.0} | {:>6.0} |", "Total:", tc, tc * 4.184);
println!("==============================");
} else {
println!("Total estimated calories for:");
println!("==========================================================");
let bottles = calculate_calories_per_bottle(conversion, &og, &fg);
for bottle in bottles {
let output = format!(
"| Type: {: <20} | kcal: {: >6} | kJ: {: >6.0} |",
bottle.0,
bottle.1,
bottle.1 * 4.184
);
println!("{}", output);
}
println!("==========================================================");
}
} else if calories.abv.is_some() {
if let Some(volume) = calories.volume {
let volume = volume.as_milliliters();
let criteria = Criteria { abv: calories.abv };
for abv in ABV_CALORIES.iter() {
if criteria.matches(abv) {
let lc = abv.calories_low / conversion * volume;
let hc = abv.calories_high / conversion * volume;
println!("Total estimated calories range for: {} ml", volume);
println!("=========================");
println!("| {:>6.0} to {:<6.0} kcal |", lc, hc);
println!("| {:>6.0} to {:<6.0} kJ |", lc * 4.184, hc * 4.184);
println!("=========================");
return;
}
}
} else {
let criteria = Criteria { abv: calories.abv };
for abv in ABV_CALORIES.iter() {
if criteria.matches(abv) {
println!("Total estimated calories range for:");
println!("============================================================================");
let bottles = get_list_of_volumes_from_bottles();
for bottle in bottles {
let lc = abv.calories_low / conversion * bottle.1;
let hc = abv.calories_high / conversion * bottle.1;
let output =
format!(
"| Type: {: <20} | kcal: {:>5.0} to {:<5.0} | kJ: {:>6.0} to {:<6.0} |",
bottle.0, lc, hc, lc * 4.184, hc * 4.184
);
println!("{}", output);
}
println!("============================================================================");
return;
}
}
}
println!("Could not find any ABV to calories matching criteria (range: 0 to 22)");
}
}
pub fn calculate_calories_per_bottle(
conversion: f64,
og: &RelativeDensity,
fg: &RelativeDensity,
) -> Vec<(String, f64)> {
let bottle_types = bottles();
let mut bottle_counter: Vec<(String, f64)> = Vec::with_capacity(bottle_types.len());
for bottle in bottle_types {
let total_calories: f64 = (calculate_total_calories(og, fg) / conversion * bottle.1).ceil();
bottle_counter.push((bottle.0, total_calories));
}
bottle_counter
}
pub fn get_list_of_volumes_from_bottles() -> Vec<(String, f64)> {
let bottle_types = bottles();
let mut bottle_counter: Vec<(String, f64)> = Vec::with_capacity(bottle_types.len());
for bottle in bottle_types {
bottle_counter.push((bottle.0, bottle.1));
}
bottle_counter
}