Files
music-agregator/src/torrent/parsed.rs
T
2026-07-19 17:36:38 +02:00

78 lines
2.1 KiB
Rust

use super::{AudioFormat, ReleaseSource, ReleaseType};
/// Structured release information extracted from a torrent title and/or its
/// metainfo bytes.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ParsedTorrent {
pub raw_title: String,
pub url: String,
/// Lowercase-hex SHA-1 info-hash; populated only from torrent metainfo.
pub info_hash: String,
pub artist: String,
pub album: String,
pub year: u32,
pub release_type: ReleaseType,
pub genres: Vec<String>,
pub label: String,
pub source: ReleaseSource,
pub rip_type: String,
pub format: AudioFormat,
/// e.g. "320 kbps" (lossy only).
pub bitrate: String,
/// e.g. 16 or 24 (lossless only).
pub bit_depth: u32,
/// In Hz (e.g. 44100), not kHz.
pub sample_rate: u32,
pub track_names: Vec<String>,
pub track_count: u32,
pub release_count: u32,
pub audio_file_count: u32,
pub total_audio_size: u64,
pub has_cover_art: bool,
pub has_cue_sheet: bool,
pub has_rip_log: bool,
/// Drives the primary sort key of [`Ord`]. `-1` means the indexer did
/// not report the value.
pub seeders: i32,
/// Torznab `peers - seeders`. `-1` means the indexer did not report it.
pub leechers: i32,
pub parsed_successfully: bool,
pub parse_errors: Vec<String>,
}
impl Ord for ParsedTorrent {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.seeders
.cmp(&other.seeders)
.then(self.format.quality_rank().cmp(&other.format.quality_rank()))
}
}
impl PartialOrd for ParsedTorrent {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl ParsedTorrent {
pub(super) fn record_error(&mut self, msg: impl Into<String>) {
self.parse_errors.push(msg.into());
}
pub(super) fn set_quality_if_missing(&mut self, bit_depth: u32, sample_rate: u32) {
if self.bit_depth == 0 {
self.bit_depth = bit_depth;
}
if self.sample_rate == 0 {
self.sample_rate = sample_rate;
}
}
}