use std::fmt::{Debug, Display};
use derive_builder::Builder;
use serde::{Deserialize, Serialize};
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Debug)]
pub enum TorrentFormat {
#[serde(rename = "v1")]
V1,
#[serde(rename = "v2")]
V2,
#[serde(rename = "hybrid")]
Hybrid,
}
impl Default for TorrentFormat {
fn default() -> Self {
Self::Hybrid
}
}
impl Display for TorrentFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
TorrentFormat::V1 => "v1",
TorrentFormat::V2 => "v2",
TorrentFormat::Hybrid => "hybrid",
}
)
}
}
#[derive(
Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Builder,
)]
pub struct TorrentCreator {
#[builder(setter(into))]
pub source_path: String,
#[builder(setter(into, strip_option), default)]
pub format: Option<TorrentFormat>,
#[builder(setter(into, strip_option), default)]
pub piece_size: Option<TorrentPieceSize>,
#[builder(default = Some(false))]
pub optimize_alignment: Option<bool>,
#[builder(setter(into, strip_option), default = Some(-1))]
pub padded_file_size_limit: Option<i64>,
#[builder(setter(into, strip_option), default)]
pub private: Option<bool>,
#[builder(setter(into, strip_option), default)]
pub start_seeding: Option<bool>,
#[builder(setter(into, strip_option), default)]
pub torrent_file_path: Option<String>,
#[builder(setter(into, strip_option), default)]
pub trackers: Option<Vec<String>>,
#[builder(setter(into, strip_option), default)]
pub url_seeds: Option<Vec<String>>,
#[builder(setter(into, strip_option), default)]
pub source: Option<String>,
#[builder(setter(into, strip_option), default)]
pub comment: Option<String>,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct TorrentCreatorTask {
#[serde(rename = "taskID")]
pub task_id: String,
}
impl From<String> for TorrentCreatorTask {
fn from(value: String) -> Self {
Self { task_id: value }
}
}
impl Display for TorrentCreatorTask {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.task_id)
}
}
impl PartialEq<TorrentCreatorTask> for String {
fn eq(&self, other: &TorrentCreatorTask) -> bool {
*self == other.task_id
}
}
impl PartialEq<TorrentCreatorTask> for &str {
fn eq(&self, other: &TorrentCreatorTask) -> bool {
*self == other.task_id
}
}
impl PartialEq<TorrentCreatorTask> for &String {
fn eq(&self, other: &TorrentCreatorTask) -> bool {
**self == other.task_id
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct TorrentPieceSize(pub u64);
impl Default for TorrentPieceSize {
fn default() -> Self {
Self::auto()
}
}
impl From<u64> for TorrentPieceSize {
fn from(value: u64) -> Self {
Self(value)
}
}
impl TorrentPieceSize {
pub fn auto() -> Self {
Self(0)
}
pub fn k16() -> Self {
Self(16384)
}
pub fn k32() -> Self {
Self(32768)
}
pub fn k64() -> Self {
Self(65536)
}
pub fn k128() -> Self {
Self(131072)
}
pub fn k256() -> Self {
Self(262144)
}
pub fn k512() -> Self {
Self(524288)
}
pub fn m1() -> Self {
Self(1048576)
}
pub fn m2() -> Self {
Self(2097152)
}
pub fn m4() -> Self {
Self(4194304)
}
pub fn m8() -> Self {
Self(8388608)
}
pub fn m16() -> Self {
Self(16777216)
}
pub fn m32() -> Self {
Self(33554432)
}
pub fn m64() -> Self {
Self(67108864)
}
pub fn m128() -> Self {
Self(134217728)
}
pub fn m256() -> Self {
Self(268435456)
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum TaskStatus {
Failed,
Queued,
Running,
Finished,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TorrentCreatorTaskStatus {
pub format: Option<TorrentFormat>,
pub error_message: Option<String>,
pub comment: Option<String>,
pub optimize_alignment: Option<bool>,
pub padded_file_size_limit: Option<i64>,
pub piece_size: TorrentPieceSize,
pub private: bool,
pub source_path: String,
pub status: TaskStatus,
#[serde(rename = "taskID")]
pub task_id: String,
pub time_added: String,
pub time_finished: Option<String>,
pub time_started: Option<String>,
pub source: Option<String>,
pub trackers: Vec<String>,
pub url_seeds: Vec<String>,
}