use alloc::string::String;
use alloc::vec::Vec;
use crate::{EventBuilder, Kind, Tag, TagStandard, Timestamp};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WebBookmark {
pub description: String,
pub url: String,
pub published_at: Option<Timestamp>,
pub title: Option<String>,
pub hashtags: Vec<String>,
}
impl WebBookmark {
#[inline]
pub fn new<T>(description: T, url: T) -> Self
where
T: Into<String>,
{
Self {
description: description.into(),
url: url.into(),
published_at: None,
title: None,
hashtags: Vec::new(),
}
}
#[inline]
pub fn title<T>(mut self, title: T) -> Self
where
T: Into<String>,
{
self.title = Some(title.into());
self
}
#[inline]
pub fn published_at(mut self, timestamp: Timestamp) -> Self {
self.published_at = Some(timestamp);
self
}
pub fn hashtags<T>(mut self, hashtag: T) -> Self
where
T: Into<String>,
{
let hashtag = hashtag.into().to_lowercase();
if !self.hashtags.contains(&hashtag) {
self.hashtags.push(hashtag);
}
self
}
#[allow(clippy::wrong_self_convention)]
pub(crate) fn to_event_builder(self) -> EventBuilder {
let mut tags: Vec<Tag> = vec![TagStandard::Identifier(self.url).into()];
let mut add_if_some = |tag: Option<TagStandard>| {
if let Some(tag) = tag {
tags.push(tag.into());
}
};
add_if_some(self.published_at.map(TagStandard::PublishedAt));
add_if_some(self.title.map(TagStandard::Title));
for hashtag in self.hashtags.into_iter() {
tags.push(TagStandard::Hashtag(hashtag).into());
}
EventBuilder::new(Kind::WebBookmark, self.description).tags(tags)
}
}