1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#[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
})
}
}