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
use itertools::Itertools;
#[derive(Debug, Clone, PartialEq)]
pub struct MediaLanguage {
pub main: String,
pub sub: String,
pub weight: f32,
}
impl MediaLanguage {
pub fn parse_string(language: &str) -> MediaLanguage {
let types: Vec<&str> = language.splitn(2, '-').collect_vec();
if types.is_empty() || types[0].is_empty() {
MediaLanguage {
main: "*".to_string(),
sub: "".to_string(),
weight: 1.0,
}
} else {
MediaLanguage {
main: types[0].to_string(),
sub: if types.len() == 1 || types[1].is_empty() {
"".to_string()
} else {
types[1].to_string()
},
weight: 1.0,
}
}
}
pub fn with_weight(&self, weight: &str) -> MediaLanguage {
MediaLanguage {
main: self.main.clone(),
sub: self.sub.clone(),
weight: weight.parse().unwrap_or(1.0),
}
}
pub fn matches(&self, other: &MediaLanguage) -> bool {
if other.main == "*" || (self.main == other.main && self.sub == other.sub) {
true
} else {
let check = format!("{}-", self.to_string());
other.to_string().starts_with(&check)
}
}
pub fn to_string(&self) -> String {
if self.sub.is_empty() {
self.main.clone()
} else {
format!("{}-{}", self.main, self.sub)
}
}
}