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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use serde::{Deserialize, Serialize};
use serde_xml_rs::{from_str, to_string};

/// Structure representing a XMLTV channel.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Channel {
    pub channel: String,
    pub id: String,
    pub display_names: Vec<ChannelDisplayName>,
    pub icons: Vec<String>,
    pub urls: Vec<String>,
}

/// Structure representing a XMLTV channel display name.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct ChannelDisplayName {
    pub name: String,
    pub lang: Option<String>,
}

/// Structure representing a XMLTV program element.
///
/// Two fields are mandatory :
/// * start
/// * channel
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Program {
    // Attributes
    pub start: String,
    pub stop: Option<String>,
    pub pdc_start: Option<String>,
    pub vps_start: Option<String>,
    pub showview: Option<String>,
    pub videoplus: Option<String>,
    pub channel: String,
    pub clumpidx: Option<String>,
    // Childs
    pub titles: Vec<Title>,
    pub sub_titles: Vec<SubTitle>,
    pub descs: Vec<Description>,
    pub credits: Option<Credits>,
    pub date: Option<String>,
    pub categories: Vec<Category>,
    pub keywords: Vec<Keyword>,
    pub language: Option<Language>,
    pub orig_language: Option<OrigLanguage>,
    pub length: Option<Length>,
    pub icons: Vec<Icon>,
    pub countries: Vec<Country>,
    // episode_num: Vec<String>,
    // video: Option<String>,
    // audio: Option<String>,
    // previously_shown: Option<String>,
    // premiere: Option<String>,
    // last_chance: Option<String>,
    // new: Option<String>,
    // subtitles: Vec<String>,
    // rating: Vec<String>,
    // star_rating: Vec<String>,
    // review: Vec<String>
}

/// XMLTV Program actor
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Actor {
    pub actor: String,
    pub role: Option<String>,
}

/// XMLTV Program category
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Category {
    pub category: String,
    pub lang: Option<String>,
}

/// XMLTV Program country

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Country {
    pub country: String,
    pub lang: Option<String>,
}

/// XMLTV Program credits
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Credits {
    pub directors: Vec<String>,
    pub actors: Vec<Actor>,
    pub writers: Vec<String>,
    pub adapters: Vec<String>,
    pub producers: Vec<String>,
    pub composers: Vec<String>,
    pub editors: Vec<String>,
    pub presenters: Vec<String>,
    pub commentators: Vec<String>,
    pub guests: Vec<String>,
}

/// Structure representing a XMLTV description
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Description {
    pub desc: String,
    pub lang: Option<String>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Icon {
    pub src: String,
    pub width: Option<String>,
    pub height: Option<String>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Keyword {
    pub keyword: String,
    pub lang: Option<String>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Language {
    pub language: String,
    pub lang: Option<String>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Length {
    pub length: u16,
    pub units: Units,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub enum Units {
    Seconds,
    Minutes,
    Hours,
}
impl Length {
    /// Get a tuple of hours, minutes and seconds
    pub fn to_hms(&self) -> (u8, u8, u8) {
        match self.units {
            Units::Seconds => {
                let (m, s) = divmod(self.length,60);
                let (h, m) = divmod(m.try_into().unwrap(), 60);
                (h, m, s)
            },
            Units::Minutes => {
                let (h, m) = divmod(self.length, 60);
                (h, m, 0)
            },
            Units::Hours => (self.length.try_into().unwrap(), 0, 0),
        }
    }
}

impl ToString for Units {
    fn to_string(&self) -> String {
        match self {
            Units::Seconds => String::from("seconds"),
            Units::Minutes => String::from("minutes"),
            Units::Hours => String::from("hours"),
        }
    }
}

/// Takes two numbers as arguments and returns their quotient and remainder when using integer division.
fn divmod(x: u16, y: u16) -> (u8, u8) {
    let quotient = x / y;
    let remainder = x % y;
    (quotient.try_into().unwrap(), remainder.try_into().unwrap())
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct OrigLanguage {
    pub orig_language: String,
    pub lang: Option<String>,
}

/// Structure representing a XMLTV sub title
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct SubTitle {
    pub title: String,
    pub lang: Option<String>,
}

/// Structure representing a XMLTV title
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Title {
    pub title: String,
    pub lang: Option<String>,
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn test_divmod() {
        // test secondes
        let time = Length{ length: 100, units: Units::Seconds };
        assert_eq!(time.to_hms(), (0, 1, 40));
        let time = Length{ length: 7000, units: Units::Seconds };
        assert_eq!(time.to_hms(), (1, 56, 40));
        //test minutes
        let time = Length{ length: 112, units: Units::Minutes };
        assert_eq!(time.to_hms(), (1, 52, 0));
        //test hours
        let time = Length{ length: 2, units: Units::Hours };
        assert_eq!(time.to_hms(), (2, 0, 0));
    }
}