simulator_client/
recipes.rs1use std::collections::HashSet;
2
3use simulator_api::DirectFillTemplate;
4
5pub fn parse_templates(source: &str, raw: &str) -> Result<Vec<DirectFillTemplate>, String> {
11 let templates: Vec<DirectFillTemplate> =
12 serde_json::from_str(raw).map_err(|error| format!("parsing {source}: {error}"))?;
13 if templates.is_empty() {
14 return Err(format!("{source}: names no templates"));
15 }
16 let mut labels = HashSet::new();
17 if let Some(duplicate) = templates
18 .iter()
19 .map(|template| template.label.as_str())
20 .find(|label| !labels.insert(*label))
21 {
22 return Err(format!(
23 "{source}: template label {duplicate:?} appears more than once"
24 ));
25 }
26 Ok(templates)
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub struct Recipe {
31 pub name: &'static str,
32 json: &'static str,
33}
34
35impl Recipe {
36 pub fn templates(&self) -> Vec<DirectFillTemplate> {
37 serde_json::from_str(self.json)
38 .unwrap_or_else(|error| panic!("recipe {} ships invalid JSON: {error}", self.name))
39 }
40}
41
42pub const VENUES: Recipe = Recipe {
43 name: "venues",
44 json: include_str!("../recipes/venues.json"),
45};
46
47pub const ALL: [Recipe; 1] = [VENUES];
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 #[test]
54 fn every_recipe_parses() {
55 for recipe in ALL {
56 assert!(
57 !recipe.templates().is_empty(),
58 "{} carries no templates",
59 recipe.name
60 );
61 }
62 }
63
64 #[test]
65 fn labels_are_unique_across_every_recipe() {
66 let mut seen = HashSet::new();
67 for recipe in ALL {
68 for template in recipe.templates() {
69 assert!(
70 seen.insert(template.label.clone()),
71 "{} repeats the label {}",
72 recipe.name,
73 template.label
74 );
75 }
76 }
77 }
78}