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
use chrono::NaiveDate;

use crate::messages::{GetCardByIndexResponse, GetCardByIDResponse};
use anyhow::Result;

#[derive(Debug)]
pub struct Card {
    pub number: u32,
    pub from: NaiveDate,
    pub to: NaiveDate,
    pub doors: Vec<u8>,
}

impl TryFrom<GetCardByIndexResponse> for Card {
    type Error = anyhow::Error;
    fn try_from(response: GetCardByIndexResponse) -> Result<Card> {
        Ok(Card {
            number: response.card_number,
            from: response.from.try_into()?,
            to: response.to.try_into()?,
            doors: vec![
                response.door_1,
                response.door_2,
                response.door_3,
                response.door_4,
            ],
        })
    }
}

impl TryFrom<GetCardByIDResponse> for Card {
    type Error = anyhow::Error;
    fn try_from(response: GetCardByIDResponse) -> Result<Card> {
        Ok(Card {
            number: response.card_number,
            from: response.from.try_into()?,
            to: response.to.try_into()?,
            doors: vec![
                response.door_1,
                response.door_2,
                response.door_3,
                response.door_4,
            ],
        })
    }
}