From 057ef81ffe2d7740bcf15b5491340fcb60812b1e Mon Sep 17 00:00:00 2001 From: Heikki Hellgren Date: Tue, 7 Sep 2021 21:28:02 +0300 Subject: [PATCH] chore: merge util and rustybeer libraries this is to make handling conversions more easier for #109 --- Cargo.toml | 1 - README.md | 7 +- rustybeer-cli/Cargo.toml | 3 +- .../src/commands/alcohol_volume_weight.rs | 2 +- rustybeer-cli/src/commands/beer_style.rs | 2 +- rustybeer-cli/src/commands/calories.rs | 2 +- rustybeer-cli/src/commands/hops.rs | 2 +- rustybeer-cli/src/commands/num_bottles.rs | 2 +- rustybeer-cli/src/commands/priming.rs | 2 +- rustybeer-cli/src/commands/sg_correction.rs | 2 +- rustybeer-cli/src/commands/yeast.rs | 5 +- rustybeer-server/Cargo.toml | 1 - rustybeer-server/src/handlers/beer_style.rs | 2 +- rustybeer-server/src/handlers/hops.rs | 2 +- rustybeer-server/src/handlers/num_bottles.rs | 2 +- rustybeer-server/src/handlers/yeasts.rs | 4 +- rustybeer-util/Cargo.toml | 13 -- rustybeer-util/src/lib.rs | 16 -- rustybeer/Cargo.toml | 8 +- .../src/abv_calories.rs | 0 .../src/beer_styles.rs | 0 rustybeer/src/calculators/abv.rs | 12 ++ .../src/calculators/alcohol_volume_weight.rs | 47 ++++++ rustybeer/src/calculators/diluting.rs | 22 +++ rustybeer/src/calculators/ibu.rs | 6 +- rustybeer/src/calculators/priming.rs | 36 +++++ rustybeer/src/calculators/sg_correction.rs | 13 ++ rustybeer/src/calculators/yeast_viability.rs | 15 ++ .../src/conversions.rs | 0 {rustybeer-util => rustybeer}/src/hops.rs | 0 .../src/json/abv_calories.json | 0 .../src/json/beer_styles.json | 0 .../src/json/hops.json | 0 .../src/json/yeasts.json | 0 rustybeer/src/lib.rs | 27 ++-- {rustybeer-util => rustybeer}/src/macros.rs | 0 {rustybeer-util => rustybeer}/src/strings.rs | 0 {rustybeer-util => rustybeer}/src/yeasts.rs | 0 rustybeer/tests/calculators.rs | 140 ------------------ 39 files changed, 185 insertions(+), 211 deletions(-) delete mode 100644 rustybeer-util/Cargo.toml delete mode 100644 rustybeer-util/src/lib.rs rename {rustybeer-util => rustybeer}/src/abv_calories.rs (100%) rename {rustybeer-util => rustybeer}/src/beer_styles.rs (100%) rename {rustybeer-util => rustybeer}/src/conversions.rs (100%) rename {rustybeer-util => rustybeer}/src/hops.rs (100%) rename {rustybeer-util => rustybeer}/src/json/abv_calories.json (100%) rename {rustybeer-util => rustybeer}/src/json/beer_styles.json (100%) rename {rustybeer-util => rustybeer}/src/json/hops.json (100%) rename {rustybeer-util => rustybeer}/src/json/yeasts.json (100%) rename {rustybeer-util => rustybeer}/src/macros.rs (100%) rename {rustybeer-util => rustybeer}/src/strings.rs (100%) rename {rustybeer-util => rustybeer}/src/yeasts.rs (100%) delete mode 100644 rustybeer/tests/calculators.rs diff --git a/Cargo.toml b/Cargo.toml index ce63617..aa5725f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,5 @@ members = [ "rustybeer", "rustybeer-cli", - "rustybeer-util", "rustybeer-server" ] diff --git a/README.md b/README.md index 3c4d40c..e1a6afd 100644 --- a/README.md +++ b/README.md @@ -59,20 +59,15 @@ cargo test -- --nocapture - [**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 diff --git a/rustybeer-cli/Cargo.toml b/rustybeer-cli/Cargo.toml index b3a3d03..fc44000 100644 --- a/rustybeer-cli/Cargo.toml +++ b/rustybeer-cli/Cargo.toml @@ -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" diff --git a/rustybeer-cli/src/commands/alcohol_volume_weight.rs b/rustybeer-cli/src/commands/alcohol_volume_weight.rs index aef48c1..290a79a 100644 --- a/rustybeer-cli/src/commands/alcohol_volume_weight.rs +++ b/rustybeer-cli/src/commands/alcohol_volume_weight.rs @@ -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)] diff --git a/rustybeer-cli/src/commands/beer_style.rs b/rustybeer-cli/src/commands/beer_style.rs index 9b983d4..dde5b69 100644 --- a/rustybeer-cli/src/commands/beer_style.rs +++ b/rustybeer-cli/src/commands/beer_style.rs @@ -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)] diff --git a/rustybeer-cli/src/commands/calories.rs b/rustybeer-cli/src/commands/calories.rs index da8582b..a4a92f7 100644 --- a/rustybeer-cli/src/commands/calories.rs +++ b/rustybeer-cli/src/commands/calories.rs @@ -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, diff --git a/rustybeer-cli/src/commands/hops.rs b/rustybeer-cli/src/commands/hops.rs index b922910..e225f9e 100644 --- a/rustybeer-cli/src/commands/hops.rs +++ b/rustybeer-cli/src/commands/hops.rs @@ -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)] diff --git a/rustybeer-cli/src/commands/num_bottles.rs b/rustybeer-cli/src/commands/num_bottles.rs index 058b2e0..12059d0 100644 --- a/rustybeer-cli/src/commands/num_bottles.rs +++ b/rustybeer-cli/src/commands/num_bottles.rs @@ -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)] diff --git a/rustybeer-cli/src/commands/priming.rs b/rustybeer-cli/src/commands/priming.rs index c87679d..f5ecab3 100644 --- a/rustybeer-cli/src/commands/priming.rs +++ b/rustybeer-cli/src/commands/priming.rs @@ -1,5 +1,5 @@ use rustybeer::calculators::priming::{calculate_co2, calculate_sugars}; -use rustybeer_util::{ +use rustybeer::{ conversions::{TemperatureParser, VolumeParser}, measurements::{Temperature, Volume}, }; diff --git a/rustybeer-cli/src/commands/sg_correction.rs b/rustybeer-cli/src/commands/sg_correction.rs index 9b590e8..830caf0 100644 --- a/rustybeer-cli/src/commands/sg_correction.rs +++ b/rustybeer-cli/src/commands/sg_correction.rs @@ -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; diff --git a/rustybeer-cli/src/commands/yeast.rs b/rustybeer-cli/src/commands/yeast.rs index f644836..205d390 100644 --- a/rustybeer-cli/src/commands/yeast.rs +++ b/rustybeer-cli/src/commands/yeast.rs @@ -1,3 +1,4 @@ +use rustybeer::yeasts::YEASTS; use structopt::StructOpt; #[derive(Debug, StructOpt)] @@ -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); } } diff --git a/rustybeer-server/Cargo.toml b/rustybeer-server/Cargo.toml index bb747ff..4bfdd5a 100644 --- a/rustybeer-server/Cargo.toml +++ b/rustybeer-server/Cargo.toml @@ -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" diff --git a/rustybeer-server/src/handlers/beer_style.rs b/rustybeer-server/src/handlers/beer_style.rs index 2aaba56..16de463 100644 --- a/rustybeer-server/src/handlers/beer_style.rs +++ b/rustybeer-server/src/handlers/beer_style.rs @@ -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}; diff --git a/rustybeer-server/src/handlers/hops.rs b/rustybeer-server/src/handlers/hops.rs index 3965219..69580e7 100644 --- a/rustybeer-server/src/handlers/hops.rs +++ b/rustybeer-server/src/handlers/hops.rs @@ -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}; diff --git a/rustybeer-server/src/handlers/num_bottles.rs b/rustybeer-server/src/handlers/num_bottles.rs index ed98e89..7dae3c1 100644 --- a/rustybeer-server/src/handlers/num_bottles.rs +++ b/rustybeer-server/src/handlers/num_bottles.rs @@ -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}; diff --git a/rustybeer-server/src/handlers/yeasts.rs b/rustybeer-server/src/handlers/yeasts.rs index 1480ed4..7188677 100644 --- a/rustybeer-server/src/handlers/yeasts.rs +++ b/rustybeer-server/src/handlers/yeasts.rs @@ -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; diff --git a/rustybeer-util/Cargo.toml b/rustybeer-util/Cargo.toml deleted file mode 100644 index c385411..0000000 --- a/rustybeer-util/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "rustybeer-util" -version = "0.1.0" -authors = ["Heikki Hellgren ", "mlatief", "Joseph Russell"] -edition = "2018" - -[dependencies] -approx = "0.5.0" -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" diff --git a/rustybeer-util/src/lib.rs b/rustybeer-util/src/lib.rs deleted file mode 100644 index 219dcaf..0000000 --- a/rustybeer-util/src/lib.rs +++ /dev/null @@ -1,16 +0,0 @@ -//! Utilities for working with `rustybeer`: -//! * List of beer styles that can be indexed and requested -//! * List of hops and their AA% -//! * List of yeasts and their properties -//! * Units conversions from strings - -pub mod abv_calories; -pub mod beer_styles; -pub mod conversions; -pub mod hops; -pub mod yeasts; - -pub use measurements; - -mod macros; -mod strings; diff --git a/rustybeer/Cargo.toml b/rustybeer/Cargo.toml index 498a2d0..200c5c3 100644 --- a/rustybeer/Cargo.toml +++ b/rustybeer/Cargo.toml @@ -5,7 +5,9 @@ authors = ["Heikki Hellgren ", "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" diff --git a/rustybeer-util/src/abv_calories.rs b/rustybeer/src/abv_calories.rs similarity index 100% rename from rustybeer-util/src/abv_calories.rs rename to rustybeer/src/abv_calories.rs diff --git a/rustybeer-util/src/beer_styles.rs b/rustybeer/src/beer_styles.rs similarity index 100% rename from rustybeer-util/src/beer_styles.rs rename to rustybeer/src/beer_styles.rs diff --git a/rustybeer/src/calculators/abv.rs b/rustybeer/src/calculators/abv.rs index 25d4e10..d428934 100644 --- a/rustybeer/src/calculators/abv.rs +++ b/rustybeer/src/calculators/abv.rs @@ -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)); + } +} diff --git a/rustybeer/src/calculators/alcohol_volume_weight.rs b/rustybeer/src/calculators/alcohol_volume_weight.rs index 68e9dca..09fa324 100644 --- a/rustybeer/src/calculators/alcohol_volume_weight.rs +++ b/rustybeer/src/calculators/alcohol_volume_weight.rs @@ -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.)); + } +} diff --git a/rustybeer/src/calculators/diluting.rs b/rustybeer/src/calculators/diluting.rs index 4645570..e0d4b8c 100644 --- a/rustybeer/src/calculators/diluting.rs +++ b/rustybeer/src/calculators/diluting.rs @@ -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.)); + } +} diff --git a/rustybeer/src/calculators/ibu.rs b/rustybeer/src/calculators/ibu.rs index 1a3d1c9..f09fc7e 100644 --- a/rustybeer/src/calculators/ibu.rs +++ b/rustybeer/src/calculators/ibu.rs @@ -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)); /// ``` /// @@ -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()); @@ -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() { diff --git a/rustybeer/src/calculators/priming.rs b/rustybeer/src/calculators/priming.rs index 170d848..0908144 100644 --- a/rustybeer/src/calculators/priming.rs +++ b/rustybeer/src/calculators/priming.rs @@ -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); + } +} diff --git a/rustybeer/src/calculators/sg_correction.rs b/rustybeer/src/calculators/sg_correction.rs index a445874..11ebda8 100644 --- a/rustybeer/src/calculators/sg_correction.rs +++ b/rustybeer/src/calculators/sg_correction.rs @@ -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)); + } +} diff --git a/rustybeer/src/calculators/yeast_viability.rs b/rustybeer/src/calculators/yeast_viability.rs index 378e46c..e3c3c75 100644 --- a/rustybeer/src/calculators/yeast_viability.rs +++ b/rustybeer/src/calculators/yeast_viability.rs @@ -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)); + } +} diff --git a/rustybeer-util/src/conversions.rs b/rustybeer/src/conversions.rs similarity index 100% rename from rustybeer-util/src/conversions.rs rename to rustybeer/src/conversions.rs diff --git a/rustybeer-util/src/hops.rs b/rustybeer/src/hops.rs similarity index 100% rename from rustybeer-util/src/hops.rs rename to rustybeer/src/hops.rs diff --git a/rustybeer-util/src/json/abv_calories.json b/rustybeer/src/json/abv_calories.json similarity index 100% rename from rustybeer-util/src/json/abv_calories.json rename to rustybeer/src/json/abv_calories.json diff --git a/rustybeer-util/src/json/beer_styles.json b/rustybeer/src/json/beer_styles.json similarity index 100% rename from rustybeer-util/src/json/beer_styles.json rename to rustybeer/src/json/beer_styles.json diff --git a/rustybeer-util/src/json/hops.json b/rustybeer/src/json/hops.json similarity index 100% rename from rustybeer-util/src/json/hops.json rename to rustybeer/src/json/hops.json diff --git a/rustybeer-util/src/json/yeasts.json b/rustybeer/src/json/yeasts.json similarity index 100% rename from rustybeer-util/src/json/yeasts.json rename to rustybeer/src/json/yeasts.json diff --git a/rustybeer/src/lib.rs b/rustybeer/src/lib.rs index b21fe15..c451aff 100644 --- a/rustybeer/src/lib.rs +++ b/rustybeer/src/lib.rs @@ -1,14 +1,17 @@ -//! A library for calculating values used in the -//! process of brewing beer. -//! -//! Functions included: -//! - Calculating ABV (Alcohol By Volume) -//! - The specific gravity after dilution -//! - Beer priming calculator -//! - Specific gravity correction -//! - IBU calculator -//! -//! There's also a list of beer styles that can -//! be indexed and requested +//! Utilities for working with `rustybeer`: +//! * List of beer styles that can be indexed and requested +//! * List of hops and their AA% +//! * List of yeasts and their properties +//! * Units conversions from strings +pub mod abv_calories; +pub mod beer_styles; pub mod calculators; +pub mod conversions; +pub mod hops; +pub mod yeasts; + +pub use measurements; + +mod macros; +mod strings; diff --git a/rustybeer-util/src/macros.rs b/rustybeer/src/macros.rs similarity index 100% rename from rustybeer-util/src/macros.rs rename to rustybeer/src/macros.rs diff --git a/rustybeer-util/src/strings.rs b/rustybeer/src/strings.rs similarity index 100% rename from rustybeer-util/src/strings.rs rename to rustybeer/src/strings.rs diff --git a/rustybeer-util/src/yeasts.rs b/rustybeer/src/yeasts.rs similarity index 100% rename from rustybeer-util/src/yeasts.rs rename to rustybeer/src/yeasts.rs diff --git a/rustybeer/tests/calculators.rs b/rustybeer/tests/calculators.rs deleted file mode 100644 index 5779b89..0000000 --- a/rustybeer/tests/calculators.rs +++ /dev/null @@ -1,140 +0,0 @@ -use rustybeer_util::assert_approx; - -#[test] -fn abv() { - use rustybeer::calculators::abv::calculate_abv; - - assert_approx!(1050., calculate_abv(10., 2.)); - - assert_approx!(39.5548, calculate_abv(0.3026, 0.00123)); -} - -#[test] -fn alcohol_volume_weight() { - use rustybeer::calculators::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.)); -} - -#[test] -fn boil_off() { - use rustybeer::calculators::diluting::*; - - 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() { - use rustybeer::calculators::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.)); -} - -#[test] -fn priming() { - use rustybeer::calculators::priming::{calculate_co2, calculate_sugars, Sugar}; - - 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); -} - -#[test] -fn sg_correction() { - use rustybeer::calculators::sg_correction::correct_sg; - - 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)); -} - -#[test] -fn yeast_variability() { - use rustybeer::calculators::yeast_viability::{calculate_cc, calculate_yv}; - - 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)); -}