Skip to content

Commit

Permalink
feat: add CLI option to find hops
Browse files Browse the repository at this point in the history
  • Loading branch information
drodil committed Sep 3, 2021
1 parent 303effc commit 1520e68
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 18 deletions.
20 changes: 10 additions & 10 deletions rustybeer-cli/src/commands/beer_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@ use structopt::StructOpt;
pub struct BeerStyleOptions {
#[structopt(short, long)]
/// Original gravity
og: f32,
og: Option<f32>,

#[structopt(short, long)]
/// Final gravity
fg: f32,
fg: Option<f32>,

#[structopt(short, long)]
/// Alcohol by volume
abv: f32,
abv: Option<f32>,

#[structopt(short, long)]
/// International Bittering Units
ibu: u8,
ibu: Option<u8>,

#[structopt(short, long)]
/// Standard Reference Model Color
color: f32,
color: Option<f32>,
}

pub fn calculate_and_print(beer_style_options: BeerStyleOptions) {
let criteria = Criteria {
og: Some(beer_style_options.og),
fg: Some(beer_style_options.fg),
abv: Some(beer_style_options.abv),
ibu: Some(beer_style_options.ibu),
srm: Some(beer_style_options.color),
og: beer_style_options.og,
fg: beer_style_options.fg,
abv: beer_style_options.abv,
ibu: beer_style_options.ibu,
srm: beer_style_options.color,
};
let mut resp: Vec<&BeerStyle> = Vec::new();

Expand Down
57 changes: 57 additions & 0 deletions rustybeer-cli/src/commands/hops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
pub use rustybeer_util::hops::{Criteria, Hop, HOPS};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[structopt(name = "hops", author = "Heikki Hellgren ([email protected])")]
/// Finds matches of hops based on parameters
pub struct HopOptions {
#[structopt(short, long)]
/// Alpha acid
alpha_acid: Option<f64>,

#[structopt(short, long)]
/// Beta acid
beta_acid: Option<f64>,

#[structopt(short, long)]
/// Hop purpose
purpose: Option<String>,

#[structopt(short, long)]
/// Substitution of the hop
substituted: Option<String>,
}

pub fn calculate_and_print(hop_options: HopOptions) {
let criteria = Criteria {
alpha_acid: hop_options.alpha_acid,
beta_acid: hop_options.beta_acid,
purpose: hop_options.purpose,
substituted: hop_options.substituted,
};
let mut resp: Vec<&Hop> = Vec::new();

for hop in HOPS.iter() {
if criteria.matches(hop) {
resp.push(hop)
}
}

if resp.is_empty() {
println!("Could not find any hops matching criteria");
return;
}

println!("Found the following hops with criteria:");
for x in &resp {
println!("---------------------");
println!("{}\n", x.name);
println!("{}", x.description);
println!("Country: {}", x.country);
println!("Purpose: {}\n", x.purpose.join(", "));
println!("Alpha acids: {}-{}", x.alpha_acid_min, x.alpha_acid_max);
println!("Beta acids: {}-{}", x.beta_acid_min, x.beta_acid_max);
println!("Substitutions: {}", x.substitutions.join(", "));
}
println!("---------------------");
}
1 change: 1 addition & 0 deletions rustybeer-cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod boil_off;
pub mod calories;
pub mod diluting;
pub mod fg;
pub mod hops;
pub mod num_bottles;
pub mod priming;
pub mod sg_correction;
Expand Down
2 changes: 2 additions & 0 deletions rustybeer-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum RustyBeer {
Abv(commands::abv::AbvOptions),
AbvAbw(commands::alcohol_volume_weight::AbvAbwOptions),
BeerStyle(commands::beer_style::BeerStyleOptions),
Hops(commands::hops::HopOptions),
BoilOff(commands::boil_off::BoilOffOptions),
Calories(commands::calories::CaloriesOptions),
Diluting(commands::diluting::DilutingOptions),
Expand All @@ -26,6 +27,7 @@ fn main() -> Result<()> {
RustyBeer::Abv(opts) => commands::abv::calculate_and_print(opts),
RustyBeer::AbvAbw(opts) => commands::alcohol_volume_weight::calculate_and_print(opts),
RustyBeer::BeerStyle(opts) => commands::beer_style::calculate_and_print(opts),
RustyBeer::Hops(opts) => commands::hops::calculate_and_print(opts),
RustyBeer::BoilOff(opts) => commands::boil_off::calculate_and_print(opts),
RustyBeer::Calories(opts) => commands::calories::calculate_and_print(opts),
RustyBeer::Diluting(opts) => commands::diluting::calculate_and_print(opts),
Expand Down
5 changes: 2 additions & 3 deletions 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::{Hop, Criteria, HOPS};
pub use rustybeer_util::hops::{Criteria, Hop, HOPS};
use rweb::*;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -42,7 +42,6 @@ pub struct HopResponse {
pub substitutions: Vec<String>,
}


impl HopResponse {
fn from_hop(hop: &Hop) -> HopResponse {
HopResponse {
Expand All @@ -55,7 +54,7 @@ impl HopResponse {
country: hop.country.clone(),
description: hop.description.clone(),
substitutions: hop.substitutions.clone(),
}
}
}
}

Expand Down
5 changes: 0 additions & 5 deletions rustybeer-util/src/hops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl Hop {

pub static HOPS: Lazy<Vec<Hop>> = Lazy::new(|| serde_json::from_str(HOPS_JSON).unwrap());


/// Criteria for selecting a hop.
///
/// If an attribute is `None`, it is ignored.
Expand Down Expand Up @@ -76,7 +75,6 @@ impl Criteria {
}
}


#[cfg(test)]
pub mod tests {
use super::*;
Expand All @@ -93,7 +91,6 @@ pub mod tests {
);
}


static TEST_HOP: Lazy<Hop> = Lazy::new(|| Hop {
name: "test hop".to_owned(),
alpha_acid_min: 4.0,
Expand Down Expand Up @@ -132,6 +129,4 @@ pub mod tests {
criteria.substituted = Some("Cascade".to_string());
assert!(criteria.matches(&TEST_HOP));
}


}

0 comments on commit 1520e68

Please sign in to comment.