#![allow(clippy::missing_errors_doc)] #![allow(clippy::doc_markdown)] use std::{collections::HashMap, fmt}; use serde::{Deserialize, Serialize}; /// A game #[derive(Clone, Serialize, Deserialize)] pub struct Game { /// example: aoe2 pub id: String, /// example: Age of Empires 2 pub name: String, /// example: Dieses Paket enthält die original AoE 2 Version,... pub description: String, /// example: 1999 pub release_year: String, /// Microsoft pub publisher: String, /// example: 8 pub max_players: u32, /// example: 3.5 pub version: String, /// example: Echtzeit-Strategie pub genre: String, /// size in bytes: example: 3455063152 pub size: u64, } impl fmt::Debug for Game { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "{}: {} ({} MB)", self.id, self.name, self.size / 1024 / 1024, ) } } impl fmt::Display for Game { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.name) } } impl PartialEq for Game { fn eq(&self, other: &Self) -> bool { self.id == other.id } } impl Eq for Game {} impl PartialOrd for Game { fn partial_cmp(&self, other: &Self) -> Option { Some(self.name.cmp(&other.name)) } } impl Ord for Game { fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.name.cmp(&other.name) } } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct GameDB { pub games: HashMap, } impl GameDB { #[must_use] pub fn new() -> Self { GameDB { games: HashMap::new(), } } #[must_use] pub fn from(games: Vec) -> Self { let mut db = GameDB::new(); for game in games { db.games.insert(game.id.clone(), game); } db } #[must_use] pub fn get_game_by_id(&self, id: S) -> Option<&Game> where S: AsRef, { self.games.get(id.as_ref()) } #[must_use] pub fn get_game_by_name(&self, name: &str) -> Option<&Game> { self.games.values().find(|game| game.name == name) } #[must_use] pub fn all_games(&self) -> Vec<&Game> { self.games.values().collect() } } impl Default for GameDB { fn default() -> Self { Self::new() } }