minor changes, cargo fmt

main
aprzn 2 years ago
parent 364fc5d082
commit 894347e69e

1
.gitignore vendored

@ -1 +1,2 @@
/target /target
mockup.png

@ -46,7 +46,9 @@ pub enum SongRequestError {
} }
impl From<reqwest::Error> for SongRequestError { impl From<reqwest::Error> for SongRequestError {
fn from(_: reqwest::Error) -> Self {Self::ConnectionFailure} fn from(_: reqwest::Error) -> Self {
Self::ConnectionFailure
}
} }
impl Song { impl Song {

@ -5,12 +5,12 @@ mod music;
use eframe; use eframe;
use eframe::egui; use eframe::egui;
use reqwest::blocking as req;
use std::boxed::Box; use std::boxed::Box;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use thiserror::Error; use thiserror::Error;
use reqwest::blocking as req;
struct PipeDash { struct PipeDash {
msg_queue: VecDeque<Message>, msg_queue: VecDeque<Message>,
@ -109,33 +109,37 @@ impl From<Color> for eframe::epaint::Color32 {
impl Song { impl Song {
pub fn try_new(gd_song: gd::Song) -> Result<Self, SongError> { pub fn try_new(gd_song: gd::Song) -> Result<Self, SongError> {
match &gd_song { if let gd::Song::Newgrounds { id } = &gd_song {
gd::Song::Newgrounds { id } => {
let song_response = gd_song.get_response(); let song_response = gd_song.get_response();
let song_path = gd::gd_path().join(format!("{}.mp3", id)); let song_path = gd::gd_path().join(format!("{}.mp3", id));
File::open(&song_path) File::open(&song_path)
.or_else(|_| song_response .or_else(|_| {
.clone() song_response.clone().map_err(|e| e.into()).and_then(
.map_err(|e| e.into()) |response: gd::SongResponse| -> Result<File, SongError> {
.and_then(|response: gd::SongResponse| -> Result<File, SongError> { let song_blob = response
let song_blob = response.download_link() .download_link()
.ok_or(SongError::MissingLink) .ok_or(SongError::MissingLink)
.and_then(|link| Ok(req::get(link)?.bytes()?))?; .and_then(|link| Ok(req::get(link)?.bytes()?))?;
let mut file = File::open(&song_path)?; let mut file = File::open(&song_path)?;
file.write(&song_blob); file.write(&song_blob);
Ok(file) Ok(file)
}) },
) )
})
.and_then(|file| { .and_then(|file| {
let name = song_response let name = song_response
.ok() .ok()
.and_then(|response| response.name()) .and_then(|response| response.name())
.unwrap_or("".into()); .unwrap_or("".into());
let decoder = rodio::Decoder::new_mp3(file)?; let decoder = rodio::Decoder::new_mp3(file)?;
Ok(Song {name, id: *id, decoder}) Ok(Song {
name,
id: *id,
decoder,
}) })
} })
_ => Err(SongError::NotNewgrounds), } else {
Err(SongError::NotNewgrounds)
} }
} }
} }

Loading…
Cancel
Save