Skip to content
1        10        20        30        40        50        60        70        80        901use serde::{Deserialize, Serialize};23#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]4pub struct PokemonMove {5    pub name: String,6    pub level: i32,7    pub method: String,8}910impl PokemonMove {11    #[must_use]12    pub fn formatted_name(&self) -> String {13        self.name.replace('-', " ")14    }1516    #[must_use]17    pub fn formatted_method(&self) -> String {18        match self.method.as_str() {19            "level-up" => {20                if self.level > 0 {21                    format!("Level {}", self.level)22                } else {23                    "Evolution / Start".to_string()24                }25            }26            "machine" => "TM / HM".to_string(),27            "tutor" => "Move Tutor".to_string(),28            "egg" => "Egg Move".to_string(),29            other => other.to_string(),30        }31    }32}3334#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]35pub struct Pokemon {36    pub id: i32,37    pub name: String,38    pub display_name: String,39    pub types: Vec<String>,40    pub hp: i32,41    pub attack: i32,42    pub defense: i32,43    pub sp_attack: i32,44    pub sp_defense: i32,45    pub speed: i32,46    pub height: i32,47    pub weight: i32,48    pub sprite_url: String,49    pub artwork_url: String,50    pub genus: String,51    pub description: String,52    pub moves: Vec<PokemonMove>,53}5455impl Pokemon {56    #[must_use]57    pub fn formatted_id(&self) -> String {58        format!("#{:03}", self.id)59    }6061    #[must_use]62    pub fn total_stats(&self) -> i32 {63        self.hp + self.attack + self.defense + self.sp_attack + self.sp_defense + self.speed64    }6566    #[must_use]67    pub fn height_meters(&self) -> String {68        #[allow(clippy::cast_precision_loss)]69        let m = f64::from(self.height) / 10.0;70        format!("{m:.1} m")71    }7273    #[must_use]74    pub fn weight_kg(&self) -> String {75        #[allow(clippy::cast_precision_loss)]76        let kg = f64::from(self.weight) / 10.0;77        format!("{kg:.1} kg")78    }7980    #[must_use]81    pub fn level_up_moves(&self) -> Vec<&PokemonMove> {82        let mut list: Vec<&PokemonMove> = self83            .moves84            .iter()85            .filter(|m| m.method == "level-up")86            .collect();87        list.sort_by_key(|m| m.level);88        list89    }9091    #[must_use]92    pub fn machine_tutor_moves(&self) -> Vec<&PokemonMove> {93        let mut list: Vec<&PokemonMove> = self94            .moves95            .iter()96            .filter(|m| m.method != "level-up")97            .collect();98        list.sort_by(|a, b| a.name.cmp(&b.name));99        list100    }101}