use hightorrent::{MagnetLink, TorrentFile};
use std::boxed::Box;
use crate::api_error::*;
#[async_trait]
pub trait ApiAdd<'a>: Send + Sync {
async fn api_add_send(&self, add: AddBuilder<'a, AddSource>) -> Result<(), ApiError>;
}
pub struct NoAddSource;
pub enum AddSource {
Magnet(Box<MagnetLink>),
Torrent(Box<TorrentFile>),
}
pub struct AddBuilder<'a, T> {
api: &'a dyn ApiAdd<'a>,
#[allow(dead_code)]
pub source: T,
pub save_path: Option<String>,
pub paused: Option<bool>,
pub tags: Option<Vec<String>>,
}
impl<'a> AddBuilder<'a, NoAddSource> {
pub fn new(api: &'a dyn ApiAdd<'a>) -> Self {
AddBuilder {
api,
source: NoAddSource,
save_path: None,
paused: None,
tags: None,
}
}
}
impl<'a> AddBuilder<'a, NoAddSource> {
pub fn magnet(self, magnet: MagnetLink) -> AddBuilder<'a, AddSource> {
let Self {
api,
save_path,
paused,
tags,
..
} = self;
AddBuilder {
api,
source: AddSource::Magnet(Box::new(magnet)),
save_path,
paused,
tags,
}
}
pub fn torrent(self, torrent: TorrentFile) -> AddBuilder<'a, AddSource> {
let Self {
api,
save_path,
paused,
tags,
..
} = self;
AddBuilder {
api,
source: AddSource::Torrent(Box::new(torrent)),
save_path,
paused,
tags,
}
}
}
impl<'a, S> AddBuilder<'a, S> {
pub fn paused(mut self, p: bool) -> AddBuilder<'a, S> {
self.paused = Some(p);
self
}
pub fn tags(mut self, t: Vec<String>) -> AddBuilder<'a, S> {
self.tags = Some(t);
self
}
pub fn save_path(mut self, s: &str) -> AddBuilder<'a, S> {
self.save_path = Some(s.to_string());
self
}
}
impl AddBuilder<'_, AddSource> {
pub async fn send(self) -> Result<(), ApiError> {
self.api.api_add_send(self).await
}
}