mirror of
https://github.com/Hopiu/lychee.git
synced 2026-05-19 03:01:07 +00:00
Adds support for suggesting archived URLs for broken links. Uses Wayback Machine as the archive provider.
42 lines
1.1 KiB
Rust
42 lines
1.1 KiB
Rust
use reqwest::{Error, Url};
|
|
use serde::{Deserialize, Serialize};
|
|
use std::fmt::Display;
|
|
use strum::{Display, EnumIter, EnumString, EnumVariantNames};
|
|
|
|
use crate::color::{color, GREEN, PINK};
|
|
|
|
mod wayback;
|
|
|
|
#[derive(Debug, Serialize, Eq, Hash, PartialEq)]
|
|
pub(crate) struct Suggestion {
|
|
pub(crate) original: Url,
|
|
pub(crate) suggestion: Url,
|
|
}
|
|
|
|
impl Display for Suggestion {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
color!(f, PINK, "{}", self.original)?;
|
|
write!(f, " ")?;
|
|
color!(f, GREEN, "{}", self.suggestion)?;
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[non_exhaustive]
|
|
#[derive(Debug, Deserialize, Default, Clone, Display, EnumIter, EnumString, EnumVariantNames)]
|
|
pub(crate) enum Archive {
|
|
#[serde(rename = "wayback")]
|
|
#[strum(serialize = "wayback", ascii_case_insensitive)]
|
|
#[default]
|
|
WaybackMachine,
|
|
}
|
|
|
|
impl Archive {
|
|
pub(crate) async fn get_link(&self, original: &Url) -> Result<Option<Url>, Error> {
|
|
let function = match self {
|
|
Archive::WaybackMachine => wayback::get_wayback_link,
|
|
};
|
|
|
|
function(original).await
|
|
}
|
|
}
|