refactor: add Ord/PartialOrd/PartialEq for PeerTubeVideoStreamingPlaylistsFiles struct

This commit is contained in:
VC
2023-10-16 21:22:47 +02:00
parent d83985394c
commit 62641f526f

View File

@@ -1,5 +1,5 @@
use serde::Deserialize; use serde::Deserialize;
use std::{boxed::Box, error::Error}; use std::{boxed::Box, cmp::Ordering, error::Error};
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct PeerTubeVideos { pub struct PeerTubeVideos {
@@ -22,7 +22,7 @@ pub struct PeerTubeVideoStreamingPlaylists {
pub files: Vec<PeerTubeVideoStreamingPlaylistsFiles>, pub files: Vec<PeerTubeVideoStreamingPlaylistsFiles>,
} }
#[derive(Debug, Deserialize)] #[derive(Eq, Debug, Deserialize)]
pub struct PeerTubeVideoStreamingPlaylistsFiles { pub struct PeerTubeVideoStreamingPlaylistsFiles {
pub id: u64, pub id: u64,
pub resolution: PeerTubeVideoStreamingPlaylistsFilesResolution, pub resolution: PeerTubeVideoStreamingPlaylistsFilesResolution,
@@ -30,7 +30,25 @@ pub struct PeerTubeVideoStreamingPlaylistsFiles {
pub file_download_url: String, pub file_download_url: String,
} }
#[derive(Debug, Deserialize)] impl Ord for PeerTubeVideoStreamingPlaylistsFiles {
fn cmp(&self, other: &Self) -> Ordering {
self.resolution.id.cmp(&other.resolution.id)
}
}
impl PartialOrd for PeerTubeVideoStreamingPlaylistsFiles {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for PeerTubeVideoStreamingPlaylistsFiles {
fn eq(&self, other: &Self) -> bool {
self.resolution.id == other.resolution.id
}
}
#[derive(Eq, Debug, Deserialize, PartialEq)]
pub struct PeerTubeVideoStreamingPlaylistsFilesResolution { pub struct PeerTubeVideoStreamingPlaylistsFilesResolution {
pub id: u16, pub id: u16,
} }
@@ -49,17 +67,7 @@ pub async fn get_latest_video(u: &str) -> Result<PeerTubeVideo, Box<dyn Error>>
/// This returns the direct download URL for with the maximum resolution /// This returns the direct download URL for with the maximum resolution
pub fn get_max_resolution_dl(p: &[PeerTubeVideoStreamingPlaylists]) -> String { pub fn get_max_resolution_dl(p: &[PeerTubeVideoStreamingPlaylists]) -> String {
let mut res = 0; p[0].files.iter().max().unwrap().file_download_url.clone()
let mut dl_url = String::new();
for i in p[0].files.iter() {
if i.resolution.id > res {
res = i.resolution.id;
dl_url = i.file_download_url.clone();
}
}
dl_url
} }
/// This gets all the crispy details about one particular video /// This gets all the crispy details about one particular video