use pulldown_cmark::{CowStr, Event, LinkType, Tag};
pub(crate) struct UrlEvent<'a> {
link_type: LinkType,
dest_url: CowStr<'a>,
title: CowStr<'a>,
id: CowStr<'a>,
}
impl<'a> UrlEvent<'a> {
pub fn try_new_link(event: &Event<'a>) -> Option<Self> {
match event {
Event::Start(Tag::Link {
link_type,
dest_url,
title,
id,
}) => Some(Self {
link_type: *link_type,
dest_url: dest_url.clone(),
title: title.clone(),
id: id.clone(),
}),
_ => None,
}
}
pub fn try_new_image(event: &Event<'a>) -> Option<Self> {
match event {
Event::Start(Tag::Image {
link_type,
dest_url,
title,
id,
}) => Some(Self {
link_type: *link_type,
dest_url: dest_url.clone(),
title: title.clone(),
id: id.clone(),
}),
_ => None,
}
}
pub fn with_link_type(self, value: LinkType) -> Self {
Self {
link_type: value,
..self
}
}
pub fn with_dest_url(self, value: &str) -> Self {
Self {
dest_url: value.to_string().into(),
..self
}
}
pub fn link_type(&self) -> LinkType {
self.link_type
}
pub fn dest_url(&self) -> CowStr<'a> {
self.dest_url.clone()
}
pub fn title(&self) -> CowStr<'a> {
self.title.clone()
}
pub fn id(&self) -> CowStr<'a> {
self.id.clone()
}
pub fn to_link(&self) -> Event<'a> {
Event::Start(Tag::Link {
link_type: self.link_type,
dest_url: self.dest_url.clone(),
title: self.title.clone(),
id: self.id.clone(),
})
}
pub fn to_image(&self) -> Event<'a> {
Event::Start(Tag::Image {
link_type: self.link_type,
dest_url: self.dest_url.clone(),
title: self.title.clone(),
id: self.id.clone(),
})
}
}