Add deck legality checking, deck/library import-export, and a command palette
Threads Scryfall's per-format legalities through the reference db and adds a deck-page indicator (structural rules + banned/restricted lists) that lists violation reasons inline. Decks and selected library cards can now be exported to/imported from plain-text decklists, with best-effort name resolution and a report of anything that didn't match. Also adds a shuffle- and-draw sample hand modal, and an Alt+/ command palette for jumping to a tab, deck, or card without leaving the keyboard. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -75,6 +75,73 @@ impl ColorSet {
|
||||
}
|
||||
}
|
||||
|
||||
/// The 8 formats this app tracks legality for, in a fixed order that defines
|
||||
/// their bit position within [`Legalities`] (2 bits each, in order). This
|
||||
/// order must never change once shipped — it's baked into every ingested
|
||||
/// reference.db. `.0` is the Scryfall `legalities` JSON key; `.1` is the
|
||||
/// display label used throughout the app (matches DeckView's
|
||||
/// `FORMATS`/`COMMANDER_FORMATS`).
|
||||
pub const LEGALITY_FORMATS: [(&str, &str); 8] = [
|
||||
("standard", "Standard"),
|
||||
("pioneer", "Pioneer"),
|
||||
("modern", "Modern"),
|
||||
("legacy", "Legacy"),
|
||||
("vintage", "Vintage"),
|
||||
("pauper", "Pauper"),
|
||||
("commander", "Commander"),
|
||||
("brawl", "Brawl"),
|
||||
];
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum LegalStatus {
|
||||
NotLegal,
|
||||
Legal,
|
||||
Banned,
|
||||
Restricted,
|
||||
}
|
||||
|
||||
/// Per-format legality, encoded as 2 bits/format x 8 formats packed into one
|
||||
/// i64 (see [`LEGALITY_FORMATS`] for bit-position order). Mirrors
|
||||
/// `ColorSet`'s bitmask-newtype pattern but with 2-bit fields instead of
|
||||
/// 1-bit flags, since each format has 4 possible statuses (not-legal / legal
|
||||
/// / banned / restricted), not just a binary flag.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
pub struct Legalities(pub i64);
|
||||
|
||||
impl Legalities {
|
||||
/// Build from Scryfall's `legalities` object (format-slug -> status-string
|
||||
/// map). Any format not present in `map`, or present with an unrecognized
|
||||
/// status string, decodes as `NotLegal`.
|
||||
pub fn from_scryfall(map: &std::collections::HashMap<String, String>) -> Self {
|
||||
let mut bits = 0i64;
|
||||
for (i, (key, _label)) in LEGALITY_FORMATS.iter().enumerate() {
|
||||
let status: i64 = match map.get(*key).map(String::as_str) {
|
||||
Some("legal") => 1,
|
||||
Some("banned") => 2,
|
||||
Some("restricted") => 3,
|
||||
_ => 0, // "not_legal", or key missing entirely
|
||||
};
|
||||
bits |= status << (i * 2);
|
||||
}
|
||||
Legalities(bits)
|
||||
}
|
||||
|
||||
/// Decode the status for a given format's display label (e.g.
|
||||
/// "Commander", "Modern" — matches DeckView's `FORMATS` strings).
|
||||
/// Unknown labels decode as `NotLegal`.
|
||||
pub fn status_for(self, format_label: &str) -> LegalStatus {
|
||||
let Some(i) = LEGALITY_FORMATS.iter().position(|(_, label)| *label == format_label) else {
|
||||
return LegalStatus::NotLegal;
|
||||
};
|
||||
match (self.0 >> (i * 2)) & 0b11 {
|
||||
1 => LegalStatus::Legal,
|
||||
2 => LegalStatus::Banned,
|
||||
3 => LegalStatus::Restricted,
|
||||
_ => LegalStatus::NotLegal,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A card as returned by search / library queries. Oracle-level (one per
|
||||
/// `oracle_id`), enriched with the caller's ownership + deck context where relevant.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -102,6 +169,10 @@ pub struct CardRef {
|
||||
/// USD market price (Scryfall's `prices.usd`) for the same printing that
|
||||
/// supplied `image_uri`, or 0 if unknown.
|
||||
pub price_usd: f64,
|
||||
/// Per-format legality (banned/restricted/legal). See
|
||||
/// [`LEGALITY_FORMATS`] for the bit-position order and
|
||||
/// [`Legalities::status_for`] to decode a specific format.
|
||||
pub legalities: Legalities,
|
||||
}
|
||||
|
||||
/// A library row: a card the user owns, with quantity and its deck memberships.
|
||||
@@ -212,3 +283,38 @@ pub struct LibraryFilter {
|
||||
/// Case-insensitive substring match against the card's oracle rules text.
|
||||
pub oracle_text: Option<String>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[test]
|
||||
fn legalities_round_trip_all_statuses() {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("standard".to_string(), "legal".to_string());
|
||||
map.insert("modern".to_string(), "banned".to_string());
|
||||
map.insert("vintage".to_string(), "restricted".to_string());
|
||||
// "pauper" deliberately absent
|
||||
|
||||
let legalities = Legalities::from_scryfall(&map);
|
||||
|
||||
assert_eq!(legalities.status_for("Standard"), LegalStatus::Legal);
|
||||
assert_eq!(legalities.status_for("Modern"), LegalStatus::Banned);
|
||||
assert_eq!(legalities.status_for("Vintage"), LegalStatus::Restricted);
|
||||
assert_eq!(legalities.status_for("Pauper"), LegalStatus::NotLegal);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legalities_unknown_format_label_is_not_legal() {
|
||||
let legalities = Legalities::from_scryfall(&HashMap::new());
|
||||
assert_eq!(legalities.status_for("Nonsense"), LegalStatus::NotLegal);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legalities_default_is_all_not_legal() {
|
||||
let legalities = Legalities::default();
|
||||
assert_eq!(legalities.status_for("Commander"), LegalStatus::NotLegal);
|
||||
assert_eq!(legalities.status_for("Brawl"), LegalStatus::NotLegal);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user