arch_mirrors/
url.rs

1//! This is where the [`Url`] struct and all of its dependencies go.
2use crate::Country;
3use serde::{Deserialize, Serialize};
4
5/// Raw, typed form of the JSON output of each url listed in [`country::Raw`](crate::country::Raw).
6#[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/// An Arch Linux mirror and its statistics.
25#[derive(Debug, Clone, PartialOrd, PartialEq)]
26pub struct Url {
27    /// The url of the mirror.
28    pub url: url::Url,
29
30    /// The protocol that this mirror uses.
31    pub protocol: crate::Protocol,
32
33    /// The last time it synced from Arch Linux server.
34    pub last_sync: Option<chrono::DateTime<chrono::Utc>>,
35
36    /// Completion PCT. Unknown what this means.
37    pub completion_pct: f64,
38
39    /// The average duration. Unknown what this means.
40    pub duration_average: Option<f64>,
41
42    /// Duration StdDev. Unknown what this means.
43    pub duration_stddev: Option<f64>,
44
45    /// The score of the mirror. This is currently calculated as `(hours delay + average duration + standard deviation) / completion percentage`.
46    /// Lower is better.
47    pub score: Option<f64>,
48
49    /// Whether or not the mirror is active.
50    pub active: bool,
51
52    /// The country where the mirror resides in.
53    pub country: crate::Country,
54
55    /// Whether or not this mirror has Arch Linux ISOs(?)
56    pub isos: bool,
57
58    /// Whether or not this mirror supports IPv4.
59    pub ipv4: bool,
60
61    /// Whether or not this mirror supports IPv6.
62    pub ipv6: bool,
63
64    /// The details of the mirror.
65    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}