-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathalcohol_volume_weight.rs
64 lines (57 loc) · 2.02 KB
/
alcohol_volume_weight.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
pub use rustybeer::calculators::alcohol_volume_weight::{
calculate_abv_abw, calculate_abv_abw_density, calculate_abw_abv, calculate_abw_abv_density,
calculate_alc_vol, calculate_alc_weight,
};
use rustybeer::{conversions::VolumeParser, measurements::Volume};
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(
name = "abv_abw",
author = "Joseph Russell ([email protected])"
)]
/// Calculates Alcohol by Weight (ABW) from Alcohol By Volue (ABV) and vice versa
pub struct AbvAbwOptions {
#[structopt(short, long)]
/// 'From' alcohol percentage
percent: f64,
#[structopt(short, long, parse(try_from_str = VolumeParser::parse))]
/// Total beer volume
volume: Option<Volume>,
#[structopt(short, long, required_unless("fg"))]
/// Total density of beer in g/cm³
density: Option<f64>,
#[structopt(short, long)]
/// Calculates ABW to ABV
reverse: Option<bool>,
}
pub fn calculate_and_print(abv_abw: AbvAbwOptions) {
let end_percentage: f64;
// main ABV <-> ABW conversion
if Some(true) == abv_abw.reverse {
end_percentage = match abv_abw.density {
Some(density) => calculate_abw_abv_density(abv_abw.percent, density),
None => calculate_abw_abv(abv_abw.percent),
};
println!("ABV: {:.3}%", end_percentage);
} else {
end_percentage = match abv_abw.density {
Some(density) => calculate_abv_abw_density(abv_abw.percent, density),
None => calculate_abv_abw(abv_abw.percent),
};
println!("ABW: {:.3}%", end_percentage);
}
// Quantity of alcohol
if let Some(volume) = abv_abw.volume {
if Some(true) == abv_abw.reverse {
println!(
"Alcohol: {:.3} ml",
calculate_alc_vol(volume.as_millilitres(), end_percentage)
);
} else {
println!(
"Alcohol: {:.3} g",
calculate_alc_weight(volume.as_millilitres(), abv_abw.percent)
);
}
}
}