mtg_cardbase/
cards.rs

1use std::ops::{Deref, DerefMut};
2
3pub struct AllCardsIter(Vec<crate::Card>);
4
5impl AllCardsIter {
6    pub fn new() -> Self {
7        /* All possible serach paths ? */
8        const SEARCH_PATHS: &[&'static str] = &[
9            /* Cargo bin run the executables from the workspaces */
10            "mtg-cardbase/data/cards.json",
11            /* Cargo test run them from the crates, so we need to go back */
12            "../mtg-cardbase/data/cards.json",
13            /* Brainstorm can be used as a lib, so we shall account for that too */
14            "../brainstorm/mtg-cardbase/data/cards.json",
15        ];
16        /*
17            If this throws an error, you might be missing the card database.
18            Run the python script "data_fetcher.py" to get it.
19        */
20        let mut cards_json = None;
21        for search_path in SEARCH_PATHS {
22            if let Ok(cards) = std::fs::read_to_string(search_path) {
23                cards_json = Some(cards);
24            }
25        }
26        let cards_json = cards_json.expect("Missing json card database!");
27        let all_cards: Vec<crate::Card> = serde_json::from_str(&cards_json).expect("Invalid json for provided card list!");
28        AllCardsIter(all_cards)
29    }
30
31    pub fn len(&self) -> usize {
32        self.0.len()
33    }
34}
35
36impl Deref for AllCardsIter {
37    type Target = [crate::Card];
38    fn deref(&self) -> &Self::Target {
39        &self.0
40    }
41}
42
43impl DerefMut for AllCardsIter {
44    fn deref_mut(&mut self) -> &mut Self::Target {
45        &mut self.0
46    }
47}