From 71ec8d5aadf9b9586cf4dd2313292b4a5c36710f Mon Sep 17 00:00:00 2001 From: lov3b Date: Mon, 6 Mar 2023 07:47:52 +0100 Subject: [PATCH] Use traits from the standard library instead of own methods --- src/downloader.rs | 48 +++++++++++++++++++++++------------------------ src/playlist.rs | 7 ++++--- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/downloader.rs b/src/downloader.rs index 69dc52b..989e4a4 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -1,5 +1,5 @@ use std::{ - fmt, + error, fmt, fs::File, io::{self, Read, Write}, }; @@ -30,8 +30,24 @@ impl DualWriter { Ok(()) } +} - pub fn get_string(self) -> Result { +impl TryFrom> for DualWriter { + type Error = io::Error; + + fn try_from(file_name: Option<&str>) -> Result { + Ok(if let Some(file_name) = file_name { + Self::File(File::create(&file_name)?) + } else { + Self::Buffer(Vec::::new()) + }) + } +} + +impl TryInto for DualWriter { + type Error = String; + + fn try_into(self) -> Result { Ok(match self { Self::Buffer(buffer) => { String::from_utf8(buffer).or(Err("Failed to decode buffer".to_owned()))? @@ -47,32 +63,16 @@ impl DualWriter { } }) } - - pub fn new(file_name: Option<&str>) -> Result { - Ok(if let Some(file_name) = file_name { - Self::File(File::create(&file_name)?) - } else { - Self::Buffer(Vec::::new()) - }) - } } pub async fn download_with_progress( link: &str, file_name: Option<&str>, -) -> Result { - let mut dual_writer = DualWriter::new(file_name).or(Err("Failed to create file".to_owned()))?; +) -> Result> { + let mut dual_writer: DualWriter = file_name.try_into()?; - let client = Client::builder() - .gzip(true) - .deflate(true) - .build() - .or(Err("Failed to create client".to_owned()))?; - let resp = client - .get(link) - .send() - .await - .or(Err("Failed to connect server".to_owned()))?; + let client = Client::builder().gzip(true).deflate(true).build()?; + let resp = client.get(link).send().await?; let content_length = if let Some(got_content_length) = resp.content_length() { got_content_length } else { @@ -90,9 +90,7 @@ pub async fn download_with_progress( while let Some(item) = stream.next().await { let bytes = item.unwrap(); downloaded = cmp::min(downloaded + (bytes.len() as u64), content_length); - dual_writer - .write(bytes) - .or(Err("Failed to write to file".to_owned()))?; + dual_writer.write(bytes)?; progress_bar.set_position(downloaded); } diff --git a/src/playlist.rs b/src/playlist.rs index 5aecba4..4fa04bb 100644 --- a/src/playlist.rs +++ b/src/playlist.rs @@ -1,6 +1,6 @@ use std::{fs, ops::Deref, path::PathBuf, rc::Rc}; -use crate::{download_with_progress, downloader::DualWriter, MAX_TRIES}; +use crate::{download_with_progress, MAX_TRIES}; pub struct Playlist { pub content: String, @@ -79,8 +79,9 @@ impl Playlist { let downloaded = download_with_progress(&url, None) .await - .and_then(DualWriter::get_string); - if let Ok(content) = downloaded { + .map(TryInto::try_into); + + if let Ok(Ok(content)) = downloaded { break Ok(content); } else if counter > MAX_TRIES { break Err("Failed to download playlist".to_owned());