93 lines
1.9 KiB
Rust
93 lines
1.9 KiB
Rust
#[derive(Debug)]
|
|
pub struct Pokemon {
|
|
pub number: PokemonNumber,
|
|
name: PokemonName,
|
|
types: PokemonTypes,
|
|
}
|
|
|
|
impl Pokemon {
|
|
pub fn new(number: PokemonNumber, name: PokemonName, types: PokemonTypes) -> Self {
|
|
Self {
|
|
number,
|
|
name,
|
|
types,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug,PartialOrd, PartialEq, Copy, Clone)]
|
|
pub struct PokemonNumber(u16);
|
|
|
|
impl TryFrom<u16> for PokemonNumber {
|
|
type Error = ();
|
|
|
|
fn try_from(value: u16) -> Result<Self, Self::Error> {
|
|
if value > 0 && value < 899 {
|
|
Ok(Self(value))
|
|
} else {
|
|
Err(())
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<PokemonNumber> for u16 {
|
|
fn from(p: PokemonNumber) -> Self {
|
|
p.0
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct PokemonName(String);
|
|
|
|
impl TryFrom<String> for PokemonName {
|
|
type Error = ();
|
|
|
|
fn try_from(value: String) -> Result<Self, Self::Error> {
|
|
if value.is_empty() {
|
|
Err(())
|
|
} else {
|
|
Ok(Self(value))
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct PokemonTypes(Vec<PokemonType>);
|
|
|
|
impl TryFrom<Vec<String>> for PokemonTypes {
|
|
type Error = ();
|
|
|
|
fn try_from(value: Vec<String>) -> Result<Self, Self::Error> {
|
|
if value.is_empty() {
|
|
Err(())
|
|
} else {
|
|
let mut pts = vec![];
|
|
for t in value.iter() {
|
|
match PokemonType::try_from(t.to_string()) {
|
|
Ok(pt) => pts.push(pt),
|
|
_ => return Err(())
|
|
}
|
|
}
|
|
Ok(Self(pts))
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
enum PokemonType {
|
|
Electric,
|
|
Fire,
|
|
}
|
|
|
|
impl TryFrom<String> for PokemonType {
|
|
type Error = ();
|
|
|
|
fn try_from(value: String) -> Result<Self, Self::Error> {
|
|
match value.as_str() {
|
|
"Electric" => Ok(Self::Electric),
|
|
"Fire" => Ok(Self::Fire),
|
|
_ => Err(())
|
|
}
|
|
}
|
|
}
|