Skip to content

Commit

Permalink
refactor: hops to not publish list of hops
Browse files Browse the repository at this point in the history
Introduce get_hops functionality instead. Relates to #111
  • Loading branch information
drodil committed Oct 1, 2021
1 parent ed52b84 commit 18c6ada
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions rustybeer-cli/src/commands/hops.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use rustybeer::hops::{Criteria, Hop, HOPS};
pub use rustybeer::hops::{get_hops, Criteria, Hop};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
Expand Down Expand Up @@ -40,7 +40,7 @@ pub fn calculate_and_print(hop_options: HopOptions) {
substituted: hop_options.substituted,
};

let resp: Vec<&Hop> = HOPS.iter().filter(|hop| criteria.matches(hop)).collect();
let resp = get_hops(Some(criteria));

if resp.is_empty() {
println!("Could not find any hops matching criteria");
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::hops::{Criteria, Hop, HOPS};
pub use rustybeer::hops::{get_hops, Criteria, Hop};
use rweb::*;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -81,9 +81,8 @@ pub fn search(q: Query<HopQuery>) -> Json<Vec<HopResponse>> {
substituted: query.substituted,
};

let resp: Vec<HopResponse> = HOPS
let resp: Vec<HopResponse> = get_hops(Some(criteria))
.iter()
.filter(|hop| criteria.matches(hop))
.map(|hop| HopResponse::from_hop(&hop))
.collect();

Expand Down
6 changes: 5 additions & 1 deletion rustybeer/src/hops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct Hop {
}

const HOPS_JSON: &str = include_str!("json/hops.json");
static HOPS: Lazy<Vec<Hop>> = Lazy::new(|| serde_json::from_str(HOPS_JSON).unwrap());

fn percentage_to_float<'de, D>(deserializer: D) -> Result<f64, D::Error>
where
Expand All @@ -36,7 +37,10 @@ impl Hop {
}
}

pub static HOPS: Lazy<Vec<Hop>> = Lazy::new(|| serde_json::from_str(HOPS_JSON).unwrap());
pub fn get_hops(criteria: Option<Criteria>) -> Vec<&'static Hop> {
let crit = criteria.unwrap_or_default();
HOPS.iter().filter(|hop| crit.matches(hop)).collect()
}

/// Criteria for selecting a hop.
///
Expand Down

0 comments on commit 18c6ada

Please sign in to comment.