From 685320336b887e2e53461a666074f9509e0c014f Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:20:04 +0200 Subject: [PATCH] impl From traits for error type for easy usage --- src/dl/yt_dlp.rs | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/src/dl/yt_dlp.rs b/src/dl/yt_dlp.rs index bb06780..4b2909e 100644 --- a/src/dl/yt_dlp.rs +++ b/src/dl/yt_dlp.rs @@ -1,6 +1,6 @@ +use core::fmt; use serde::Deserialize; use serde_json; -use core::fmt; use std::str::Utf8Error; use tokio::process::Command; @@ -29,7 +29,26 @@ pub struct YtDlpInfo { pub enum YtDlpError { CommandError(std::io::Error), UtfError(Utf8Error), - ErrorMessage(String) + ErrorMessage(String), + JsonError, +} + +impl From for YtDlpError { + fn from(value: std::io::Error) -> Self { + Self::CommandError(value) + } +} + +impl From for YtDlpError { + fn from(value: Utf8Error) -> Self { + Self::UtfError(value) + } +} + +impl From for YtDlpError { + fn from(_value: serde_json::Error) -> Self { + Self::JsonError + } } impl fmt::Display for YtDlpError { @@ -38,7 +57,8 @@ impl fmt::Display for YtDlpError { match self { YTE::CommandError(e) => write!(f, "Command::new - {}", e), YTE::UtfError(_) => write!(f, "Error while decoding UTF8"), - YTE::ErrorMessage(msg) => write!(f, "yt-dlp error - {}", msg) + YTE::ErrorMessage(msg) => write!(f, "yt-dlp error - {}", msg), + YTE::JsonError => write!(f, "json parsing error"), } } } @@ -46,22 +66,19 @@ impl fmt::Display for YtDlpError { pub struct YtDlp {} impl YtDlp { - pub async fn load_info(url: &str) -> Result<(), YtDlpError> { - let output = match Command::new("python") + pub async fn load_info(url: &str) -> Result { + let output = Command::new("python") .args(["-m", "yt_dlp", url, "-j"]) .output() - .await - { - Ok(output) => output, - Err(e) => return Err(YtDlpError::CommandError(e)), - }; + .await?; if output.stdout.is_empty() && !output.stderr.is_empty() { - return match std::str::from_utf8(&output.stderr) { - Ok(message) => Err(YtDlpError::ErrorMessage(message.to_string())), - Err(utf8_error) => Err(YtDlpError::UtfError(utf8_error)) - }; + let message = std::str::from_utf8(&output.stderr)?; + return Err(YtDlpError::ErrorMessage(message.to_string())); } - Ok(()) + + let info: YtDlpInfo = serde_json::from_slice(&output.stdout)?; + + Ok(info) } }