lychee_lib/archive/
mod.rs

1use reqwest::{Error, Url};
2use serde::Deserialize;
3use std::time::Duration;
4use strum::{Display, EnumIter, EnumString, VariantNames};
5
6mod wayback;
7
8/// The different supported online archive sites for restoring broken links.
9#[non_exhaustive]
10#[derive(
11    Debug, Deserialize, Default, Clone, Display, EnumIter, EnumString, VariantNames, PartialEq, Eq,
12)]
13pub enum Archive {
14    #[serde(rename = "wayback")]
15    #[strum(serialize = "wayback", ascii_case_insensitive)]
16    #[default]
17    /// The most prominent digital archive provided by the [Interne Archive](https://archive.org)
18    WaybackMachine,
19}
20
21impl Archive {
22    /// Query the `Archive` to try and find the latest snapshot of the specified `url`.
23    /// Returns `None` if the specified `url` hasn't been archived in the past.
24    ///
25    /// # Errors
26    ///
27    /// Returns an error if the `reqwest` client cannot be built, the request itself fails
28    /// or the API response cannot be parsed.
29    pub async fn get_archive_snapshot(
30        &self,
31        url: &Url,
32        timeout: Duration,
33    ) -> Result<Option<Url>, Error> {
34        let function = match self {
35            Archive::WaybackMachine => wayback::get_archive_snapshot,
36        };
37
38        function(url, timeout).await
39    }
40}