#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::card::{Card, CardCodeAndCount};
use crate::error::LorError;
use std::iter::FromIterator;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Default)]
pub struct Deck(Vec<CardCodeAndCount>);
impl Deck {
pub fn new() -> Self {
Default::default()
}
pub fn from_vec(vec: Vec<CardCodeAndCount>) -> Deck {
Deck { 0: vec }
}
pub fn add(&mut self, card: CardCodeAndCount) {
self.0.push(card);
}
pub fn add_from_data(&mut self, code: &str, count: i32) -> Result<(), LorError> {
let card = CardCodeAndCount::new(Card::from_code(code)?, count);
self.add(card);
Ok(())
}
pub fn cards(&self) -> &Vec<CardCodeAndCount> {
&self.0
}
}
impl PartialEq for Deck {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<'a> FromIterator<&'a (&'a str, i32)> for Deck {
fn from_iter<T: IntoIterator<Item = &'a (&'a str, i32)>>(iter: T) -> Self {
iter.into_iter().fold(Deck::new(), |mut deck, (card, code)| {
deck.add_from_data(card, *code).unwrap();
deck
})
}
}