use std::fmt;
use crate::arguments::{ArgumentScanner, ExpectArg, FromArgs};
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct Hyperlink<S = String> {
pub href: S,
pub hint: S,
pub expire: Option<S>,
}
impl<S> Hyperlink<S> {
pub fn map_text<T, F>(self, mut f: F) -> Hyperlink<T>
where
F: FnMut(S) -> T,
{
Hyperlink {
href: f(self.href),
hint: f(self.hint),
expire: self.expire.map(f),
}
}
}
impl_into_owned!(Hyperlink);
impl<S: AsRef<str>> Hyperlink<S> {
pub fn borrow_text(&self) -> Hyperlink<&str> {
Hyperlink {
href: self.href.as_ref(),
hint: self.hint.as_ref(),
expire: self.expire.as_ref().map(AsRef::as_ref),
}
}
}
impl_partial_eq!(Hyperlink);
impl<'a, S: AsRef<str> + Clone> FromArgs<'a, S> for Hyperlink<S> {
fn from_args<A: ArgumentScanner<'a, Decoded = S>>(mut scanner: A) -> crate::Result<Self> {
let href = scanner.get_next_or("href")?.expect_some("href")?;
let hint = scanner.get_next_or("hint")?.unwrap_or_else(|| href.clone());
let expire = scanner.get_next_or("expire")?;
scanner.expect_end()?;
Ok(Self { href, hint, expire })
}
}
impl_from_str!(Hyperlink);
impl<S: AsRef<str>> fmt::Display for Hyperlink<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let Hyperlink {
href,
mut hint,
expire,
} = self.borrow_text();
if hint == href {
hint = "";
}
crate::display::ElementFormatter {
name: "A",
arguments: &[&href, &hint, &expire],
keywords: &[],
}
.fmt(f)
}
}