use pulldown_cmark::{CowStr, Event, LinkType, Tag};
pub(crate) struct UrlEvent<'a> {
pub link_type: LinkType,
pub dest_url: CowStr<'a>,
pub title: CowStr<'a>,
pub 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 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(),
})
}
}
pub(crate) struct TextEvent<'a> {
pub text: CowStr<'a>,
pub is_code: bool,
}
impl<'a> TextEvent<'a> {
pub fn try_new(event: &Event<'a>) -> Option<Self> {
match event {
Event::Text(cow_str) => Some(Self {
text: cow_str.clone(),
is_code: false,
}),
Event::Code(cow_str) => Some(Self {
text: cow_str.clone(),
is_code: true,
}),
_ => None,
}
}
pub fn with_text(self, value: &str) -> Self {
Self {
text: CowStr::Boxed(value.to_string().into_boxed_str()),
..self
}
}
pub fn to_event(&self) -> Event<'a> {
match self.is_code {
false => Event::Text(self.text.clone()),
true => Event::Code(self.text.clone()),
}
}
}