From 18c6ada436bef45fe91031352d8e470c7eea08a8 Mon Sep 17 00:00:00 2001 From: Heikki Hellgren Date: Fri, 1 Oct 2021 14:18:01 +0300 Subject: [PATCH] refactor: hops to not publish list of hops Introduce get_hops functionality instead. Relates to #111 --- rustybeer-cli/src/commands/hops.rs | 4 ++-- rustybeer-server/src/handlers/hops.rs | 5 ++--- rustybeer/src/hops.rs | 6 +++++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/rustybeer-cli/src/commands/hops.rs b/rustybeer-cli/src/commands/hops.rs index e225f9e..afef890 100644 --- a/rustybeer-cli/src/commands/hops.rs +++ b/rustybeer-cli/src/commands/hops.rs @@ -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)] @@ -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"); diff --git a/rustybeer-server/src/handlers/hops.rs b/rustybeer-server/src/handlers/hops.rs index 69580e7..4e2619c 100644 --- a/rustybeer-server/src/handlers/hops.rs +++ b/rustybeer-server/src/handlers/hops.rs @@ -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}; @@ -81,9 +81,8 @@ pub fn search(q: Query) -> Json> { substituted: query.substituted, }; - let resp: Vec = HOPS + let resp: Vec = get_hops(Some(criteria)) .iter() - .filter(|hop| criteria.matches(hop)) .map(|hop| HopResponse::from_hop(&hop)) .collect(); diff --git a/rustybeer/src/hops.rs b/rustybeer/src/hops.rs index d298de5..1d43ba7 100644 --- a/rustybeer/src/hops.rs +++ b/rustybeer/src/hops.rs @@ -21,6 +21,7 @@ pub struct Hop { } const HOPS_JSON: &str = include_str!("json/hops.json"); +static HOPS: Lazy> = Lazy::new(|| serde_json::from_str(HOPS_JSON).unwrap()); fn percentage_to_float<'de, D>(deserializer: D) -> Result where @@ -36,7 +37,10 @@ impl Hop { } } -pub static HOPS: Lazy> = Lazy::new(|| serde_json::from_str(HOPS_JSON).unwrap()); +pub fn get_hops(criteria: Option) -> Vec<&'static Hop> { + let crit = criteria.unwrap_or_default(); + HOPS.iter().filter(|hop| crit.matches(hop)).collect() +} /// Criteria for selecting a hop. ///