Skip to main content

mtgjson_sdk/queries/
enums.rs

1//! Enum/keyword queries backed by JSON files loaded via the cache manager.
2//!
3//! These queries operate on `Keywords.json`, `CardTypes.json`, and `EnumValues.json`
4//! and do not require DuckDB at all.
5
6use serde_json::Value;
7
8use crate::connection::Connection;
9use crate::error::Result;
10
11// ---------------------------------------------------------------------------
12// EnumQuery
13// ---------------------------------------------------------------------------
14
15/// Query interface for MTGJSON enum/keyword data backed by cached JSON files.
16pub struct EnumQuery<'a> {
17    conn: &'a Connection,
18}
19
20impl<'a> EnumQuery<'a> {
21    /// Create a new `EnumQuery` bound to the given connection.
22    pub fn new(conn: &'a Connection) -> Self {
23        Self { conn }
24    }
25
26    /// Get all keyword categories.
27    ///
28    /// Loads `Keywords.json` and returns its `data` payload. The resulting object
29    /// has keys like `"abilityWords"`, `"keywordAbilities"`, `"keywordActions"`, each
30    /// mapping to an array of strings.
31    pub fn keywords(&self) -> Result<Value> {
32        let data = self.conn.cache.borrow_mut().load_json("keywords")?;
33        Ok(extract_data(data))
34    }
35
36    /// Get all card type definitions.
37    ///
38    /// Loads `CardTypes.json` and returns its `data` payload. The resulting object
39    /// has keys for each card type (e.g., `"creature"`, `"instant"`, `"land"`), each
40    /// containing `subTypes` and `superTypes` arrays.
41    pub fn card_types(&self) -> Result<Value> {
42        let data = self.conn.cache.borrow_mut().load_json("card_types")?;
43        Ok(extract_data(data))
44    }
45
46    /// Get the full enum values reference.
47    ///
48    /// Loads `EnumValues.json` and returns its `data` payload. Contains all valid
49    /// enum values used across the MTGJSON data model.
50    pub fn enum_values(&self) -> Result<Value> {
51        let data = self.conn.cache.borrow_mut().load_json("enum_values")?;
52        Ok(extract_data(data))
53    }
54}
55
56// ---------------------------------------------------------------------------
57// Helpers
58// ---------------------------------------------------------------------------
59
60/// Extract the `"data"` field from a JSON wrapper, or return the value as-is
61/// if there is no wrapper.
62fn extract_data(value: Value) -> Value {
63    match value {
64        Value::Object(ref map) => {
65            if let Some(data) = map.get("data") {
66                data.clone()
67            } else {
68                value
69            }
70        }
71        _ => value,
72    }
73}