Skip to content

Commit

Permalink
chore: merge util and rustybeer libraries
Browse files Browse the repository at this point in the history
this is to make handling conversions more easier for #109
  • Loading branch information
drodil committed Sep 7, 2021
1 parent d599c38 commit 057ef81
Show file tree
Hide file tree
Showing 39 changed files with 185 additions and 211 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
members = [
"rustybeer",
"rustybeer-cli",
"rustybeer-util",
"rustybeer-server"
]
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,15 @@ cargo test -- --nocapture <test name>
- [**rustybeer**](rustybeer) - The folder containing everything for the business logic
- [**src**](rustybeer/src) - The folder containing the business logic source code
- [**calculators**](rustybeer/src/calculators) - The folder containing calculators to be used in lib or CLI tool
- [**tests**](rustybeer/tests) - The folder containing test source code
- [**calculators.rs**](rustybeer/tests/calculators.rs) The file containing the test source code for the calculators
- [**Cargo.toml**](rustybeer/Cargo.toml) - The file containing build and dependency infomation
- [**rustybeer-cli**](rustybeer-cli) - The folder containing everything for the CLI
- [**src**](rustybeer-cli/src) - The folder containing the CLI source code
- [**commands**](rustybeer-cli/src/commands) - The folder containing subcommands for CLI
- [**main.rs**](rustybeer-cli/src/main.rs) - The file containing the main function
- [**Cargo.toml**](rustybeer-cli/Cargo.toml)- The file containing build and dependency infomation
- [**rustybeer-util**](rustybeer-util) - The folder containing extra utilities
- [**src**](rustybeer-util/src) - The folder containing the utilities source code
- [**Cargo.toml**](rustybeer-util/Cargo.toml) - The file containing build and dependency infomation
- [**rustybeer-server**](rustybeer-server) - The folder containing the HTTP server implementation
- [**src**](rustybeer-server/src) - The folder containing server source code
- [**handlers**](rustybeer-server/handlers) - HTTP request handlers
- [**handlers**](rustybeer-server/src/handlers) - HTTP request handlers
- [**Cargo.toml**](rustybeer-server/Cargo.toml) - The file containing server build and dependency information
- [**Cargo.toml**](Cargo.toml) - The file containing build and dependency infomation
- [**CONTRIBUTING.md**](CONTRIBUTING.md) - Contribution guidelines for this repository
Expand Down
3 changes: 1 addition & 2 deletions rustybeer-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ edition = "2018"

[dependencies]
anyhow = "1.0"
rustybeer = { version = "0.1.0", path = "../rustybeer"}
rustybeer-util = { version = "0.1.0", path = "../rustybeer-util"}
rustybeer = { path = "../rustybeer"}
structopt = "0.3.20"
chrono = "0.4"

Expand Down
2 changes: 1 addition & 1 deletion rustybeer-cli/src/commands/alcohol_volume_weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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_util::{conversions::VolumeParser, measurements::Volume};
use rustybeer::{conversions::VolumeParser, measurements::Volume};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
Expand Down
2 changes: 1 addition & 1 deletion rustybeer-cli/src/commands/beer_style.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use rustybeer_util::beer_styles::{BeerStyle, Criteria, BEER_STYLES};
pub use rustybeer::beer_styles::{BeerStyle, Criteria, BEER_STYLES};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
Expand Down
2 changes: 1 addition & 1 deletion rustybeer-cli/src/commands/calories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustybeer::calculators::calorie_counter::{
calculate_alcohol_calories, calculate_carbs_calories, calculate_total_calories,
};
use rustybeer::calculators::num_bottles::bottles;
use rustybeer_util::{
use rustybeer::{
abv_calories::{Criteria, ABV_CALORIES},
conversions::{MassParser, VolumeParser},
measurements::Volume,
Expand Down
2 changes: 1 addition & 1 deletion rustybeer-cli/src/commands/hops.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use rustybeer_util::hops::{Criteria, Hop, HOPS};
pub use rustybeer::hops::{Criteria, Hop, HOPS};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
Expand Down
2 changes: 1 addition & 1 deletion rustybeer-cli/src/commands/num_bottles.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustybeer::calculators::num_bottles::calculate_num_bottles;
use rustybeer_util::{conversions::VolumeParser, measurements::Volume};
use rustybeer::{conversions::VolumeParser, measurements::Volume};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
Expand Down
2 changes: 1 addition & 1 deletion rustybeer-cli/src/commands/priming.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustybeer::calculators::priming::{calculate_co2, calculate_sugars};
use rustybeer_util::{
use rustybeer::{
conversions::{TemperatureParser, VolumeParser},
measurements::{Temperature, Volume},
};
Expand Down
2 changes: 1 addition & 1 deletion rustybeer-cli/src/commands/sg_correction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustybeer::calculators::sg_correction::correct_sg;
use rustybeer_util::{conversions::TemperatureParser, measurements::Temperature};
use rustybeer::{conversions::TemperatureParser, measurements::Temperature};

use structopt::StructOpt;

Expand Down
5 changes: 3 additions & 2 deletions rustybeer-cli/src/commands/yeast.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rustybeer::yeasts::YEASTS;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
Expand All @@ -12,13 +13,13 @@ pub struct YeastOptions {
pub fn search_and_print(opt: YeastOptions) {
if let Some(name) = opt.name {
let criteria = name.to_lowercase();
for yeast in rustybeer_util::yeasts::YEASTS.iter() {
for yeast in YEASTS.iter() {
if yeast.name.to_lowercase().contains(&criteria) {
println!("{:?}", yeast);
}
}
} else {
for yeast in rustybeer_util::yeasts::YEASTS.iter() {
for yeast in YEASTS.iter() {
println!("{:?}", yeast);
}
}
Expand Down
1 change: 0 additions & 1 deletion rustybeer-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2018"

[dependencies]
rustybeer = { path = "../rustybeer" }
rustybeer-util = { version = "0.1.0", path = "../rustybeer-util"}
rweb = { version = "0.13.0", features = ["openapi"] }
serde = "1"
tokio = "1"
2 changes: 1 addition & 1 deletion rustybeer-server/src/handlers/beer_style.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use rustybeer_util::beer_styles::{BeerStyle, Criteria, BEER_STYLES};
pub use rustybeer::beer_styles::{BeerStyle, Criteria, BEER_STYLES};
use rweb::*;
use serde::{Deserialize, Serialize};

Expand Down
2 changes: 1 addition & 1 deletion rustybeer-server/src/handlers/hops.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use rustybeer_util::hops::{Criteria, Hop, HOPS};
pub use rustybeer::hops::{Criteria, Hop, HOPS};
use rweb::*;
use serde::{Deserialize, Serialize};

Expand Down
2 changes: 1 addition & 1 deletion rustybeer-server/src/handlers/num_bottles.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustybeer::calculators::num_bottles::calculate_num_bottles;
use rustybeer_util::conversions::VolumeParser;
use rustybeer::conversions::VolumeParser;
use rweb::*;
use serde::{Deserialize, Serialize};

Expand Down
4 changes: 2 additions & 2 deletions rustybeer-server/src/handlers/yeasts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustybeer_util::conversions::temp_to_map;
pub use rustybeer_util::yeasts::{Criteria, Yeast, YEASTS};
use rustybeer::conversions::temp_to_map;
pub use rustybeer::yeasts::{Criteria, Yeast, YEASTS};
use rweb::*;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
Expand Down
13 changes: 0 additions & 13 deletions rustybeer-util/Cargo.toml

This file was deleted.

16 changes: 0 additions & 16 deletions rustybeer-util/src/lib.rs

This file was deleted.

8 changes: 5 additions & 3 deletions rustybeer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ authors = ["Heikki Hellgren <[email protected]>", "mlatief", "Joseph Russell"]
edition = "2018"

[dependencies]

[dev-dependencies]
approx = "0.5.0"
rustybeer-util = { version = "0.1.0", path = "../rustybeer-util"}
measurements = "0.10.3"
once_cell = "1.4.1"
regex = "1.3.9"
serde = { version = "1.0.116", features = ["derive"] }
serde_json = "1.0.58"
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions rustybeer/src/calculators/abv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ pub fn calculate_abv(og: f32, fg: f32) -> f32 {
pub fn calculate_fg(og: f32, abv: f32) -> f32 {
og - (abv / 131.25)
}

#[cfg(test)]
pub mod tests {
use super::*;
use crate::assert_approx;

#[test]
fn abv() {
assert_approx!(1050., calculate_abv(10., 2.));
assert_approx!(39.5548, calculate_abv(0.3026, 0.00123));
}
}
47 changes: 47 additions & 0 deletions rustybeer/src/calculators/alcohol_volume_weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,50 @@ pub fn calculate_alc_vol(total_volume: f64, abv: f64) -> f64 {
pub fn calculate_alc_weight(total_volume: f64, abv: f64) -> f64 {
((abv / 100.0) * total_volume) * ETHANOL_DENSITY
}

#[cfg(test)]
pub mod tests {
use super::*;
use crate::assert_approx;

#[test]
fn alcohol_volume_weight() {
assert_approx!(4., calculate_abv_abw(5.));
assert_approx!(0., calculate_abv_abw(0.));
assert_approx!(400., calculate_abv_abw(500.));

assert_approx!(3.945, calculate_abv_abw_density(5., 1.));
assert_approx!(5., calculate_abv_abw_density(5., 0.789));
assert_approx!(0.789, calculate_abv_abw_density(5., 5.));
assert_approx!(100., calculate_abv_abw_density(5., 0.03945));

assert_approx!(6.25, calculate_abw_abv(5.));
assert_approx!(0., calculate_abw_abv(0.));
assert_approx!(625., calculate_abw_abv(500.));

assert_approx!(6.3371, calculate_abw_abv_density(5., 1.));
assert_approx!(5., calculate_abw_abv_density(5., 0.789));
assert_approx!(31.6857, calculate_abw_abv_density(5., 5.));
assert_approx!(0., calculate_abw_abv_density(5., 0.));

assert_approx!(50., calculate_alc_vol(1000., 5.));
assert_approx!(28.4, calculate_alc_vol(568., 5.));
assert_approx!(0., calculate_alc_vol(0., 5.));
assert_approx!(0.00005, calculate_alc_vol(0.001, 5.));
assert_approx!(5000., calculate_alc_vol(100000., 5.));

assert_approx!(0., calculate_alc_vol(1000., 0.));
assert_approx!(7.89, calculate_alc_vol(1000., 0.789));
assert_approx!(1000., calculate_alc_vol(1000., 100.));

assert_approx!(39.45, calculate_alc_weight(1000., 5.));
assert_approx!(22.4076, calculate_alc_weight(568., 5.));
assert_approx!(0., calculate_alc_weight(0., 5.));
assert_approx!(0.00003945, calculate_alc_weight(0.001, 5.));
assert_approx!(3945., calculate_alc_weight(100000., 5.));

assert_approx!(0., calculate_alc_weight(1000., 0.));
assert_approx!(6.22521, calculate_alc_weight(1000., 0.789));
assert_approx!(789., calculate_alc_weight(1000., 100.));
}
}
22 changes: 22 additions & 0 deletions rustybeer/src/calculators/diluting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,25 @@ pub fn calculate_new_gravity(current_gravity: f32, current_volume: f32, target_v
pub fn calculate_new_volume(current_gravity: f32, current_volume: f32, target_gravity: f32) -> f32 {
current_volume * (current_gravity - 1.) / (target_gravity - 1.)
}

#[cfg(test)]
pub mod tests {
use super::*;
use crate::assert_approx;

#[test]
fn boil_off() {
assert_approx!(2., calculate_new_volume(2., 2., 2.));
assert_approx!(15., calculate_new_volume(7., 5., 3.));
assert_approx!(11., calculate_new_gravity(7., 5., 3.));
assert_approx!(69.5714, calculate_new_gravity(4., 3.2, 0.14));
}

#[test]
fn diluting() {
assert_approx!(14.1625, calculate_new_gravity(9.1, 5.2, 3.2));
assert_approx!(4.5305, calculate_new_gravity(9.1, 3.16, 7.25));
assert_approx!(2.0, calculate_new_volume(2., 2., 2.));
assert_approx!(15., calculate_new_volume(7., 5., 3.));
}
}
6 changes: 3 additions & 3 deletions rustybeer/src/calculators/ibu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub struct NegativeIbuError;
///
/// ```
/// use rustybeer::calculators::ibu::{HopAddition, calculate_ibu};
/// use rustybeer_util::assert_approx;
/// use rustybeer::assert_approx;
/// assert_approx!(18.9723, calculate_ibu(vec![HopAddition::new(28.0, 0.064, 45, Default::default())], 20.0, 1.050));
/// ```
///
Expand Down Expand Up @@ -175,7 +175,7 @@ pub fn calculate_ibu(
/// * No other hops additions
/// ```
/// use rustybeer::calculators::ibu::calculate_bittering_weight;
/// use rustybeer_util::assert_approx;
/// use rustybeer::assert_approx;
///
/// let bittering = calculate_bittering_weight(None, 0.085, None, 22., 1.058, 17.);
/// assert_approx!( 20.4973, bittering.unwrap());
Expand Down Expand Up @@ -233,7 +233,7 @@ pub mod tests {
calculate_bittering_weight, calculate_ibu, HopAddition, HopAdditionType, NegativeIbuError,
_calculate_ibu_single_hop, _calculate_utilization,
};
use rustybeer_util::assert_approx;
use crate::assert_approx;

#[test]
fn utilization() {
Expand Down
36 changes: 36 additions & 0 deletions rustybeer/src/calculators/priming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,39 @@ impl Sugar {
Self { name, ratio }
}
}

#[cfg(test)]
pub mod tests {
use super::*;
use crate::assert_approx;

#[test]
fn priming() {
assert_approx!(2.3466, calculate_co2(15.));
assert_approx!(2.4556, calculate_co2(12.45));

let stream = calculate_sugars(77., 5., 2.);

let expected = vec![
Sugar::new(String::from("Table Sugar (sucrose)"), 24.850561000000013),
Sugar::new(String::from("Corn Sugar (dextrose)"), 27.308308791208802),
Sugar::new(String::from("DME - All Varieties"), 36.54494264705884),
Sugar::new(String::from("DME - Laaglander"), 49.701122000000026),
Sugar::new(String::from("Turbinado"), 24.850561000000013),
Sugar::new(String::from("Demarara"), 24.850561000000013),
Sugar::new(String::from("Corn Syrup"), 36.015305797101476),
Sugar::new(String::from("Brown Sugar"), 27.92197865168541),
Sugar::new(String::from("Molasses"), 35.00079014084509),
Sugar::new(String::from("Maple Syrup"), 32.27345584415586),
Sugar::new(String::from("Sorghum Syrup"), 36.015305797101476),
Sugar::new(String::from("Honey"), 33.5818391891892),
Sugar::new(String::from("Belgian Candy Syrup"), 39.44533492063494),
Sugar::new(String::from("Belgian Candy Sugar"), 33.13408133333335),
Sugar::new(String::from("Invert Sugar Syrup"), 27.308308791208802),
Sugar::new(String::from("Black Treacle"), 28.563863218390818),
Sugar::new(String::from("Rice Solids"), 31.45640632911394),
];

assert_eq!(expected, stream);
}
}
13 changes: 13 additions & 0 deletions rustybeer/src/calculators/sg_correction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,16 @@ pub fn correct_sg(sg: f64, ctf: f64, mtf: f64) -> f64 {
/ (1.00130346 - 0.000134722124 * ctf + 0.00000204052596 * (ctf * ctf)
- 0.00000000232820948 * (ctf * ctf * ctf)))
}

#[cfg(test)]
pub mod tests {
use super::*;
use crate::assert_approx;

#[test]
fn sg_correction() {
assert_approx!(5.001, correct_sg(5.0, 2.9, 1.37));
assert_approx!(7.3023, correct_sg(7.3, 8.1, 5.12));
assert_approx!(7.4175, correct_sg(7.413, 28.1, 55.1212));
}
}
15 changes: 15 additions & 0 deletions rustybeer/src/calculators/yeast_viability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,18 @@ pub fn calculate_yv(days: f32) -> f32 {
pub fn calculate_cc(cc: f32, days: f32) -> f32 {
cc * (calculate_yv(days) / 100.0)
}

#[cfg(test)]
pub mod tests {
use super::*;
use crate::assert_approx;
#[test]
fn yeast_viability() {
assert_approx!(970.0, calculate_cc(1000.0, 0.0));
assert_approx!(115.048_26, calculate_cc(123.45, 5.0));
assert_approx!(0.0, calculate_cc(9001.0, 3650.0));
assert_approx!(97.0, calculate_yv(0.0));
assert_approx!(65.004_616, calculate_yv(50.0));
assert_approx!(0.0, calculate_yv(3650.0));
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 057ef81

Please sign in to comment.