// SPDX-License-Identifier: MIT //! The board's cart table: facts per payload SHA-1, built from data by the reference's //! `scripts/build_cart_table.py` (`cart/table.py`). //! //! docs/cartridge.md, "The table". Sources, none of them code: //! //! - `nointro`: the No-Intro DAT "Atari - Atari Lynx (LYX)" (names, sizes, hashes); //! - `softlist`: MAME 0.285 `hash/lynx.xml` (CC0-1.0): `granularity` (block size), //! `rotation`, `audin_offset`; an entry there with no rotation or AUDIN feature //! means none, and MAME has no EEPROM support, so a softlist entry says "no EEPROM" //! only in the sense that the commercial carts it lists had none; //! - `lnx-header`: the LNX headers of our own files whose payload has that hash //! (our observation of files we hold). //! //! Where two sources give a field, the softlist wins inside the table and the //! disagreement is kept in the entry's `conflicts` (C14: Zaku (Beta); Raiden). //! //! `table.json` is the reference's own file at the pinned commit, copied unchanged //! (NOTICE: the softlist rows are CC0-1.0, MAME 0.285). //! //! Differences forced by Rust: field values (Python's int, bool, str, tuple or None) are //! [`FieldValue`]; `default_table` (an `functools.cache`) is a `OnceLock`. use std::collections::BTreeMap; use std::sync::OnceLock; use serde_json::Value; pub const FIELDS: [&str; 5] = ["bank0_block", "bank1_block", "audin", "rotation", "eeprom"]; /// A resolved field's value. #[derive(Clone, Debug, PartialEq, Eq)] pub enum FieldValue { Int(i64), Bool(bool), Str(String), Eeprom(Option<(String, u32)>), } impl FieldValue { /// How the reference's warnings show a value (`_show`). pub fn show(&self) -> String { match self { FieldValue::Int(v) => v.to_string(), FieldValue::Bool(true) => "True".into(), FieldValue::Bool(false) => "False".into(), FieldValue::Str(s) => s.clone(), FieldValue::Eeprom(None) => "None".into(), FieldValue::Eeprom(Some((k, o))) => format!("{k}:{o}"), } } pub fn as_int(&self) -> i64 { match self { FieldValue::Int(v) => *v, FieldValue::Bool(b) => *b as i64, _ => 0, } } pub fn as_bool(&self) -> bool { match self { FieldValue::Bool(b) => *b, FieldValue::Int(v) => *v != 0, _ => false, } } } #[derive(Clone, Debug)] pub struct TableEntry { pub sha1: String, pub name: Option, pub size: Option, pub crc32: Option, pub fields: BTreeMap, // field -> (value, source) pub lnx_header: Option, pub conflicts: Vec, pub sources: Vec, } pub struct CartTable { pub about: Value, entries: serde_json::Map, } fn field_value(key: &str, value: &Value) -> FieldValue { if key == "eeprom" { return match value { Value::Array(a) if a.len() == 2 => FieldValue::Eeprom(Some(( a[0].as_str().unwrap_or("").to_string(), a[1].as_u64().unwrap_or(16) as u32, ))), _ => FieldValue::Eeprom(None), }; } match value { Value::Bool(b) => FieldValue::Bool(*b), Value::Number(n) => FieldValue::Int(n.as_i64().unwrap_or(0)), Value::String(s) => FieldValue::Str(s.clone()), _ => FieldValue::Eeprom(None), } } impl CartTable { pub fn new(raw: Value) -> CartTable { CartTable { about: raw.get("about").cloned().unwrap_or(Value::Null), entries: raw .get("entries") .and_then(|e| e.as_object()) .cloned() .unwrap_or_default(), } } pub fn len(&self) -> usize { self.entries.len() } pub fn is_empty(&self) -> bool { self.entries.is_empty() } pub fn contains(&self, sha1: &str) -> bool { self.entries.contains_key(sha1) } pub fn get(&self, sha1: &str) -> Option { let raw = self.entries.get(sha1)?; let mut fields = BTreeMap::new(); if let Some(map) = raw.get("fields").and_then(|f| f.as_object()) { for (key, item) in map { let value = field_value(key, &item["value"]); let source = item["source"].as_str().unwrap_or("").to_string(); fields.insert(key.clone(), (value, source)); } } Some(TableEntry { sha1: sha1.to_string(), name: raw.get("name").and_then(|v| v.as_str()).map(String::from), size: raw.get("size").and_then(|v| v.as_u64()), crc32: raw.get("crc32").and_then(|v| v.as_str()).map(String::from), fields, lnx_header: raw.get("lnx_header").cloned(), conflicts: raw .get("conflicts") .and_then(|v| v.as_array()) .cloned() .unwrap_or_default(), sources: raw .get("sources") .and_then(|v| v.as_array()) .map(|a| { a.iter() .filter_map(|s| s.as_str().map(String::from)) .collect() }) .unwrap_or_default(), }) } pub fn sha1s(&self) -> Vec { self.entries.keys().cloned().collect() } } /// The table shipped with the crate (`cart/table.json`). pub fn default_table() -> &'static CartTable { static TABLE: OnceLock = OnceLock::new(); TABLE.get_or_init(|| { CartTable::new(serde_json::from_str(include_str!("table.json")).expect("table.json")) }) } pub fn empty_table() -> CartTable { CartTable::new(Value::Object(Default::default())) }