1use crate::Country;
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Serialize, Deserialize)]
7pub(crate) struct Raw {
8 url: String,
9 protocol: String,
10 last_sync: Option<String>,
11 completion_pct: f64,
12 duration_avg: Option<f64>,
13 duration_stddev: Option<f64>,
14 score: Option<f64>,
15 active: bool,
16 country: String,
17 country_code: String,
18 isos: bool,
19 ipv4: bool,
20 ipv6: bool,
21 details: String,
22}
23
24#[derive(Debug, Clone, PartialOrd, PartialEq)]
26pub struct Url {
27 pub url: url::Url,
29
30 pub protocol: crate::Protocol,
32
33 pub last_sync: Option<chrono::DateTime<chrono::Utc>>,
35
36 pub completion_pct: f64,
38
39 pub duration_average: Option<f64>,
41
42 pub duration_stddev: Option<f64>,
44
45 pub score: Option<f64>,
48
49 pub active: bool,
51
52 pub country: crate::Country,
54
55 pub isos: bool,
57
58 pub ipv4: bool,
60
61 pub ipv6: bool,
63
64 pub details: String,
66}
67
68impl From<Raw> for Url {
69 fn from(raw: Raw) -> Self {
70 let url: url::Url = raw
71 .url
72 .parse()
73 .expect("failed to parse url field from raw url");
74 let protocol: crate::Protocol = raw
75 .protocol
76 .parse()
77 .expect("failed to parse protocol field from raw url");
78 let last_sync = raw.last_sync.map(|raw| {
79 raw.parse::<chrono::DateTime<chrono::Utc>>()
80 .expect("failed to parse last_sync field from raw url")
81 });
82 let country = Country::new(&raw.country, &raw.country_code);
83
84 Self {
85 url,
86 protocol,
87 last_sync,
88 completion_pct: raw.completion_pct,
89 duration_average: raw.duration_avg,
90 duration_stddev: raw.duration_stddev,
91 score: raw.score,
92 active: raw.active,
93 country,
94 isos: raw.isos,
95 ipv4: raw.ipv4,
96 ipv6: raw.ipv6,
97 details: raw.details,
98 }
99 }
100}