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
use regex::Regex;
#[derive(Serialize, Clone, Default, Debug, PartialEq, Eq)]
pub enum Store {
Steam,
Gog,
#[default]
Unknown,
}
#[derive(Serialize, Clone, Default, Debug, PartialEq, Eq)]
pub struct StoreLink {
pub store: Store,
pub url: String,
pub id: Option<usize>,
}
impl StoreLink {
pub fn from(url: &str) -> Self {
if url.contains("steampowered") {
Self {
store: Store::Steam,
url: url.to_string(),
id: get_steam_id(url),
}
} else if url.contains("gog.com") {
Self {
store: Store::Gog,
url: url.to_string(),
id: None,
}
} else {
Self {
store: Store::Unknown,
url: url.to_string(),
id: None,
}
}
}
}
fn get_steam_id(url: &str) -> Option<usize> {
let re = Regex::new(r"https://store.steampowered.com/app/(\d+)(/?.+)?").unwrap();
if let Some(cap) = re.captures(url) {
if let Some(cap) = cap.get(1) {
return cap.as_str().parse::<usize>().ok();
};
};
None
}
#[derive(Serialize, Clone, Default, Debug, PartialEq, Eq)]
pub struct StoreLinks(pub Vec<StoreLink>);
impl StoreLinks {
pub fn new(items: Vec<StoreLink>) -> Self {
Self(items)
}
pub fn push(&mut self, store: StoreLink) {
self.0.push(store)
}
pub fn inner_ref(&self) -> &Vec<StoreLink> {
&self.0
}
pub fn inner_mut_ref(&mut self) -> &mut Vec<StoreLink> {
&mut self.0
}
pub fn into_inner(self) -> Vec<StoreLink> {
self.0
}
}
#[cfg(test)]
mod store_link_tests {
use super::*;
#[test]
fn test_get_id_steam() {
let store = StoreLink::from("https://store.steampowered.com/app/1878910/LoupLaine/");
assert_eq!(store.id, Some(1878910));
let store = StoreLink::from("https://store.steampowered.com/app/1878910");
assert_eq!(store.id, Some(1878910));
let store = StoreLink::from("https://store.steampowered.com/app/1878910/");
assert_eq!(store.id, Some(1878910));
let store = StoreLink::from("https://store.steampowered.com/app/1878910/LoupLaine");
assert_eq!(store.id, Some(1878910));
}
#[test]
fn test_get_id_gog() {
let store = StoreLink::from("https://gog.com/app/1878910/LoupLaine/");
assert_eq!(store.id, None);
}
}