mame_parser/core/models/
core_models.rs

1use serde::{Deserialize, Serialize};
2
3/// MAME machine, including all relevant metadata and resources.
4///
5/// The `Machine` struct stores detailed information about a specific MAME machine,
6/// including its configuration, associated ROMs, BIOS sets, devices, and other related metadata.
7/// This structure is used in parsing, processing, and exporting MAME-related data.
8#[derive(Debug, Serialize, Deserialize, Clone)]
9pub struct Machine {
10    /// The name of the machine.
11    pub name: String,
12    /// The source file associated with the machine (optional).
13    pub source_file: Option<String>,
14    /// Specifies the ROM that this machine is a variant of (optional).
15    pub rom_of: Option<String>,
16    /// Specifies the parent machine if this is a clone (optional).
17    pub clone_of: Option<String>,
18    /// Indicates if the machine is a BIOS set (optional).
19    pub is_bios: Option<bool>,
20    /// Indicates if the machine is a device (optional).
21    pub is_device: Option<bool>,
22    /// Indicates if the machine is runnable (optional).
23    pub runnable: Option<bool>,
24    /// Indicates if the machine is mechanical (optional).
25    pub is_mechanical: Option<bool>,
26    /// Specifies the sample set associated with the machine (optional).
27    pub sample_of: Option<String>,
28    /// A description of the machine (optional).
29    pub description: Option<String>,
30    /// The year the machine was released (optional).
31    pub year: Option<String>,
32    /// The manufacturer of the machine (optional).
33    pub manufacturer: Option<String>,
34    /// A list of BIOS sets associated with the machine.
35    pub bios_sets: Vec<BiosSet>,
36    /// A list of ROMs required by the machine.
37    pub roms: Vec<Rom>,
38    /// A list of device references associated with the machine.
39    pub device_refs: Vec<DeviceRef>,
40    /// A list of software lists associated with the machine.
41    pub software_list: Vec<Software>,
42    /// A list of samples used by the machine.
43    pub samples: Vec<Sample>,
44    /// The driver status of the machine (optional).
45    pub driver_status: Option<String>,
46    /// A list of supported languages for the machine.
47    pub languages: Vec<String>,
48    /// Indicates the number of players supported (optional).
49    pub players: Option<String>,
50    /// The series to which the machine belongs (optional).
51    pub series: Option<String>,
52    /// The category of the machine (optional).
53    pub category: Option<String>,
54    /// The subcategory of the machine (optional).
55    pub subcategory: Option<String>,
56    /// Indicates if the machine contains mature content (optional).
57    pub is_mature: Option<bool>,
58    /// A list of history sections associated with the machine.
59    pub history_sections: Vec<HistorySection>,
60    /// A list of disk data associated with the machine.
61    pub disks: Vec<Disk>,
62    /// Additional normalized data not present in the original MAME data (optional).
63    pub extended_data: Option<ExtendedData>,
64    /// A list of external resources, such as images and videos, associated with the machine.
65    pub resources: Vec<Resource>,
66}
67
68impl Machine {
69    /// Creates a new `Machine` instance with the specified name.
70    pub fn new(name: String) -> Self {
71        Machine {
72            name,
73            source_file: None,
74            rom_of: None,
75            clone_of: None,
76            is_bios: None,
77            is_device: None,
78            runnable: None,
79            is_mechanical: None,
80            sample_of: None,
81            description: None,
82            year: None,
83            manufacturer: None,
84            bios_sets: Vec::new(),
85            roms: Vec::new(),
86            device_refs: Vec::new(),
87            software_list: Vec::new(),
88            samples: Vec::new(),
89            driver_status: None,
90            languages: Vec::new(),
91            players: None,
92            series: None,
93            category: None,
94            subcategory: None,
95            is_mature: None,
96            history_sections: Vec::new(),
97            disks: Vec::new(),
98            extended_data: Some(Default::default()),
99            resources: Vec::new(),
100        }
101    }
102    /// Combines the metadata of this machine with another machine.
103    pub fn combine(&mut self, other: &Machine) {
104        if self.source_file.is_none() {
105            self.source_file = other.source_file.clone();
106        }
107        if self.rom_of.is_none() {
108            self.rom_of = other.rom_of.clone();
109        }
110        if self.clone_of.is_none() {
111            self.clone_of = other.clone_of.clone();
112        }
113        if self.is_bios.is_none() {
114            self.is_bios = other.is_bios;
115        }
116        if self.is_device.is_none() {
117            self.is_device = other.is_device;
118        }
119        if self.runnable.is_none() {
120            self.runnable = other.runnable;
121        }
122        if self.is_mechanical.is_none() {
123            self.is_mechanical = other.is_mechanical;
124        }
125        if self.sample_of.is_none() {
126            self.sample_of = other.sample_of.clone();
127        }
128        if self.description.is_none() {
129            self.description = other.description.clone();
130        }
131        if self.year.is_none() {
132            self.year = other.year.clone();
133        }
134        if self.manufacturer.is_none() {
135            self.manufacturer = other.manufacturer.clone();
136        }
137        if self.driver_status.is_none() {
138            self.driver_status = other.driver_status.clone();
139        }
140        if self.players.is_none() {
141            self.players = other.players.clone();
142        }
143        if self.series.is_none() {
144            self.series = other.series.clone();
145        }
146        if self.category.is_none() {
147            self.category = other.category.clone();
148        }
149        if self.subcategory.is_none() {
150            self.subcategory = other.subcategory.clone();
151        }
152        if self.is_mature.is_none() {
153            self.is_mature = other.is_mature;
154        }
155
156        self.bios_sets.extend(other.bios_sets.clone());
157        self.roms.extend(other.roms.clone());
158        self.device_refs.extend(other.device_refs.clone());
159        self.software_list.extend(other.software_list.clone());
160        self.samples.extend(other.samples.clone());
161        self.languages.extend(other.languages.clone());
162        self.history_sections.extend(other.history_sections.clone());
163        self.disks.extend(other.disks.clone());
164        self.resources.extend(other.resources.clone());
165
166        match (&mut self.extended_data, &other.extended_data) {
167            (Some(self_data), Some(other_data)) => {
168                self_data.combine(other_data);
169            }
170            (None, Some(other_data)) => {
171                self.extended_data = Some(other_data.clone());
172            }
173            _ => {}
174        }
175    }
176}
177
178/// BIOS set associated with a MAME machine.
179#[derive(Debug, Clone, Serialize, Deserialize)]
180pub struct BiosSet {
181    /// The name of the BIOS set.
182    pub name: String,
183    /// A description of the BIOS set.
184    pub description: String,
185}
186
187/// ROM file associated with a MAME machine.
188#[derive(Debug, Clone, Serialize, Deserialize)]
189pub struct Rom {
190    /// The name of the ROM file.
191    pub name: String,
192    /// The size of the ROM file in bytes.
193    pub size: u64,
194    /// Indicates if the ROM is merged with another ROM (optional).
195    pub merge: Option<String>,
196    /// The status of the ROM (optional).
197    pub status: Option<String>,
198    /// The CRC32 hash of the ROM file (optional).
199    pub crc: Option<String>,
200    /// The SHA-1 hash of the ROM file (optional).
201    pub sha1: Option<String>,
202}
203
204/// Device reference associated with a MAME machine.
205#[derive(Debug, Clone, Serialize, Deserialize)]
206pub struct DeviceRef {
207    /// The name of the device.
208    pub name: String,
209}
210
211/// Software list associated with a MAME machine.
212#[derive(Debug, Clone, Serialize, Deserialize)]
213pub struct Software {
214    /// The name of the software.
215    pub name: String,
216}
217
218/// Sample file associated with a MAME machine.
219#[derive(Debug, Clone, Serialize, Deserialize)]
220pub struct Sample {
221    /// The name of the sample file.
222    pub name: String,
223}
224
225/// Disk data associated with a MAME machine.
226#[derive(Debug, Clone, Serialize, Deserialize)]
227pub struct Disk {
228    /// The name of the disk.
229    pub name: String,
230    /// The SHA-1 hash of the disk file (optional).
231    pub sha1: Option<String>,
232    /// Indicates if the disk is merged with another disk (optional).
233    pub merge: Option<String>,
234    /// The status of the disk (optional).
235    pub status: Option<String>,
236    /// The region associated with the disk (optional).
237    pub region: Option<String>,
238}
239
240/// Historical section or trivia associated with a MAME machine.
241#[derive(Debug, Clone, Serialize, Deserialize)]
242pub struct HistorySection {
243    /// The name of the history section.
244    pub name: String,
245    /// The text content of the history section.
246    pub text: String,
247    /// The order in which this section should appear.
248    pub order: usize,
249}
250
251/// Represents additional normalized data for a MAME machine.
252///
253/// This structure is used to store normalized or additional data that is not present
254/// in the original MAME files but is useful for further processing or display.
255#[derive(Debug, Serialize, Deserialize, Clone, Default)]
256pub struct ExtendedData {
257    /// Normalized name of the machine (optional).
258    pub name: Option<String>,
259    /// Normalized manufacturer of the machine (optional).
260    pub manufacturer: Option<String>,
261    /// Normalized number of players (optional).
262    pub players: Option<String>,
263    /// Indicates if the machine is a parent (optional).
264    pub is_parent: Option<bool>,
265    /// Normalized release year (optional).
266    pub year: Option<String>,
267}
268
269impl ExtendedData {
270    /// Combines the metadata of this extended data with another extended data.
271    pub fn combine(&mut self, other: &ExtendedData) {
272        if self.name.is_none() {
273            self.name = other.name.clone();
274        }
275        if self.manufacturer.is_none() {
276            self.manufacturer = other.manufacturer.clone();
277        }
278        if self.players.is_none() {
279            self.players = other.players.clone();
280        }
281        if self.is_parent.is_none() {
282            self.is_parent = other.is_parent;
283        }
284        if self.year.is_none() {
285            self.year = other.year.clone();
286        }
287    }
288}
289
290/// External resource associated with a MAME machine, such as images or videos.
291#[derive(Debug, Clone, Serialize, Deserialize)]
292pub struct Resource {
293    /// The type of the resource (e.g., "image", "video").
294    pub type_: String,
295    /// The name of the resource.
296    pub name: String,
297    /// The size of the resource in bytes.
298    pub size: u64,
299    /// The CRC32 hash of the resource.
300    pub crc: String,
301    /// The SHA-1 hash of the resource.
302    pub sha1: String,
303}