fix: stream ordering, downloader progress bar

This commit is contained in:
ThetaDev 2022-11-04 19:45:58 +01:00
parent 6e2ba71f33
commit a706eb172e
6 changed files with 43 additions and 45 deletions

View file

@ -6,6 +6,7 @@ mod paginator;
pub mod richtext;
pub use convert::FromYtItem;
pub use ordering::QualityOrd;
pub use paginator::Paginator;
use serde_with::serde_as;
@ -191,7 +192,7 @@ pub struct VideoStream {
}
/// Audio stream
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[non_exhaustive]
pub struct AudioStream {
/// Audio stream URL

View file

@ -4,30 +4,26 @@ use crate::model::AudioCodec;
use super::{AudioStream, VideoStream};
impl PartialOrd for VideoStream {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(
match (self.width * self.height).cmp(&(other.width * other.height)) {
pub trait QualityOrd {
fn quality_cmp(&self, other: &Self) -> Ordering;
}
impl QualityOrd for VideoStream {
fn quality_cmp(&self, other: &Self) -> Ordering {
match (self.width * self.height).cmp(&(other.width * other.height)) {
Ordering::Less => Ordering::Less,
Ordering::Greater => Ordering::Greater,
Ordering::Equal => match self.codec.cmp(&other.codec) {
Ordering::Less => Ordering::Less,
Ordering::Greater => Ordering::Greater,
Ordering::Equal => match self.codec.cmp(&other.codec) {
Ordering::Less => Ordering::Less,
Ordering::Greater => Ordering::Greater,
Ordering::Equal => self.average_bitrate.cmp(&other.average_bitrate),
},
Ordering::Equal => self.average_bitrate.cmp(&other.average_bitrate),
},
)
}
}
}
impl Ord for VideoStream {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap()
}
}
impl PartialOrd for AudioStream {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
impl QualityOrd for AudioStream {
fn quality_cmp(&self, other: &Self) -> Ordering {
fn cmp_bitrate(s: &AudioStream) -> u32 {
match s.codec {
// Opus is more efficient
@ -36,12 +32,6 @@ impl PartialOrd for AudioStream {
}
}
Some(cmp_bitrate(self).cmp(&cmp_bitrate(other)))
}
}
impl Ord for AudioStream {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap()
cmp_bitrate(self).cmp(&cmp_bitrate(other))
}
}