Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: hops to not publish list of hops #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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