use alloc::string::{String, ToString};
use alloc::vec::Vec;
use hashes::sha1::Hash as Sha1Hash;
use crate::types::url::Url;
use crate::{EventBuilder, Kind, Tag, TagKind};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TorrentFile {
pub name: String,
pub size: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Torrent {
pub title: String,
pub description: String,
pub info_hash: Sha1Hash,
pub files: Vec<TorrentFile>,
pub trackers: Vec<Url>,
pub categories: Vec<String>,
pub hashtags: Vec<String>,
}
impl Torrent {
pub fn to_event_builder(self) -> EventBuilder {
let mut tags: Vec<Tag> = Vec::with_capacity(
2 + self.files.len()
+ self.trackers.len()
+ self.categories.len()
+ self.hashtags.len(),
);
tags.push(Tag::title(self.title));
tags.push(Tag::custom(TagKind::x(), [self.info_hash.to_string()]));
for file in self.files.into_iter() {
tags.push(Tag::custom(
TagKind::File,
[file.name, file.size.to_string()],
));
}
for tracker in self.trackers.into_iter() {
tags.push(Tag::custom(TagKind::Tracker, [tracker.to_string()]));
}
for cat in self.categories.into_iter() {
tags.push(Tag::custom(TagKind::i(), [format!("tcat:{cat}")]));
}
for tag in self.hashtags.into_iter() {
tags.push(Tag::hashtag(tag));
}
EventBuilder::new(Kind::Torrent, self.description).tags(tags)
}
}