~pola/prismatic-vista

ref: b686d11c61ebe3971282fea89bffdec4bd4f8391 prismatic-vista/src/aaa_filter_all_printings.rs -rw-r--r-- 2.0 KiB
b686d11c — Doma Problem: cat_object fails expression problem test 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use serde_json::Value;
use std::cmp::max;
use std::collections::HashMap;
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;

pub fn read_json<P: AsRef<Path>>(fp: P) -> Result<Value, Box<dyn Error>> {
  let fh = File::open(fp)?;
  let reader = BufReader::new(fh);
  let v: Value = serde_json::from_reader(reader)?;
  Ok(v)
}

#[derive(Hash, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Category {
  Regular,
  Irregular,
  Outlandish,
}

fn cat_object(v: &Value) -> Category {
  if let Some("saga") = v["layout"].as_str() {
    Category::Outlandish
  } else if let Some("black") | Some("white") = v["borderColor"].as_str() {
    Category::Regular
  } else {
    Category::Irregular
  }
}

fn cat_set(v: &str) -> Category {
  match v {
    "SLD" => Category::Outlandish,
    _ => Category::Regular,
  }
}

fn get_data<P: AsRef<Path>>(all_printings: P) -> HashMap<String, Vec<Value>> {
  let printings = read_json(all_printings).unwrap();
  let mut retval = HashMap::new();
  for (set, set_data) in o(&printings["data"]) {
    retval.insert(set.clone(), a(&set_data["cards"]).clone());
  }
  retval
}

pub fn all_printings_to_unique_items(
  data: HashMap<String, Vec<Value>>,
) -> HashMap<Category, HashMap<String, Vec<String>>> {
  let mut retval = HashMap::new();
  for (set, objects) in data {
    for obj in objects {
      if let (Some(uuid), Some(name)) = (obj["uuid"].as_str(), obj["name"].as_str()) {
        retval
          .entry(max(cat_set(set.as_str()), cat_object(&obj)))
          .or_insert(HashMap::new())
          .entry(name.to_string())
          .or_insert(Vec::new())
          .push(uuid.to_string())
      }
    }
  }
  retval
}

pub fn o(x: &Value) -> &serde_json::Map<String, Value> {
  x.as_object().unwrap()
}
pub fn a(x: &Value) -> &Vec<Value> {
  x.as_array().unwrap()
}
pub fn main() {
  let mtgjson_json_path = "../../priv/mtgjson/AllPrintings.json";
  let retval = all_printings_to_unique_items(get_data(mtgjson_json_path));
  println!("{:?}", retval)
}