From b2e7367795e1df6f3e91d944656d95b5040a57df Mon Sep 17 00:00:00 2001 From: G2-Games Date: Fri, 8 Dec 2023 00:37:19 -0600 Subject: [PATCH] Ran `cargo fmt` --- src/music_controller/controller.rs | 10 ++---- src/music_player.rs | 57 +++++++++++++++++++++--------- src/music_storage/music_db.rs | 26 ++++++++------ src/music_storage/utils.rs | 11 +++--- 4 files changed, 64 insertions(+), 40 deletions(-) diff --git a/src/music_controller/controller.rs b/src/music_controller/controller.rs index 6f4881a..333db0b 100644 --- a/src/music_controller/controller.rs +++ b/src/music_controller/controller.rs @@ -18,10 +18,7 @@ impl MusicController { Err(error) => return Err(error), }; - let controller = MusicController { - config, - library, - }; + let controller = MusicController { config, library }; Ok(controller) } @@ -34,10 +31,7 @@ impl MusicController { Err(error) => return Err(error), }; - let controller = MusicController { - config, - library, - }; + let controller = MusicController { config, library }; Ok(controller) } diff --git a/src/music_player.rs b/src/music_player.rs index 647e470..0c9b2d6 100644 --- a/src/music_player.rs +++ b/src/music_player.rs @@ -1,6 +1,6 @@ // Crate things //use crate::music_controller::config::Config; -use crate::music_storage::music_db::{URI, Tag}; +use crate::music_storage::music_db::{Tag, URI}; use crossbeam_channel::bounded; use std::error::Error; use std::sync::{Arc, RwLock}; @@ -48,11 +48,11 @@ impl TryInto for PlayerState { fn try_into(self) -> Result> { match self { Self::VoidPending => Ok(gst::State::VoidPending), - Self::Playing => Ok(gst::State::Playing), + Self::Playing => Ok(gst::State::Playing), Self::Paused => Ok(gst::State::Paused), Self::Ready => Ok(gst::State::Ready), Self::Null => Ok(gst::State::Null), - state => Err(format!("Invalid gst::State: {:?}", state).into()) + state => Err(format!("Invalid gst::State: {:?}", state).into()), } } @@ -177,10 +177,22 @@ impl Player { .expect("Failed to get GStreamer message bus") .add_watch(move |_bus, msg| { match msg.view() { - gst::MessageView::Eos(_) => {}, - gst::MessageView::StreamStart(_) => {}, - gst::MessageView::Error(e) => - println!("song {}", e.error()), + gst::MessageView::Eos(_) => {} + gst::MessageView::StreamStart(_) => println!("Stream start"), + gst::MessageView::Error(e) => { + println!("ERROR: {}", e.error()); + playbin_bus_ctrl + .write() + .unwrap() + .set_state(gst::State::Ready) + .unwrap(); + + playbin_bus_ctrl + .write() + .unwrap() + .set_state(gst::State::Playing) + .unwrap(); + }, gst::MessageView::Tag(tag) => { if let Some(title) = tag.tags().get::() { println!(" Title: {}", title.get()); @@ -193,10 +205,18 @@ impl Player { let percent = buffering.percent(); if percent < 100 { *buffer_bus_ctrl.write().unwrap() = Some(percent as u8); - playbin_bus_ctrl.write().unwrap().set_state(gst::State::Paused).unwrap(); + playbin_bus_ctrl + .write() + .unwrap() + .set_state(gst::State::Paused) + .unwrap(); } else if *paused_bus_ctrl.read().unwrap() == false { *buffer_bus_ctrl.write().unwrap() = None; - playbin_bus_ctrl.write().unwrap().set_state(gst::State::Playing).unwrap(); + playbin_bus_ctrl + .write() + .unwrap() + .set_state(gst::State::Playing) + .unwrap(); } } _ => (), @@ -239,7 +259,10 @@ impl Player { self.source = Some(source.clone()); match source { URI::Cue { start, end, .. } => { - self.playbin.write().unwrap().set_property("uri", source.as_uri()); + self.playbin + .write() + .unwrap() + .set_property("uri", source.as_uri()); // Set the start and end positions of the CUE file *self.start.write().unwrap() = Some(Duration::from_std(*start).unwrap()); @@ -258,11 +281,15 @@ impl Player { panic!("Couldn't seek to beginning of cue track in reasonable time (>20ms)"); } _ => { - self.playbin.write().unwrap().set_property("uri", source.as_uri()); + self.playbin + .write() + .unwrap() + .set_property("uri", source.as_uri()); self.play().unwrap(); - while uri.get::<&str>().unwrap_or("") == self.property("current-uri").get::<&str>().unwrap_or("") + while uri.get::<&str>().unwrap_or("") + == self.property("current-uri").get::<&str>().unwrap_or("") || self.position().is_none() { std::thread::sleep(std::time::Duration::from_millis(10)); @@ -392,13 +419,11 @@ impl Player { pub fn state(&mut self) -> PlayerState { match *self.buffer.read().unwrap() { None => self.playbin.read().unwrap().current_state().into(), - Some(value) => { - PlayerState::Buffering(value) - } + Some(value) => PlayerState::Buffering(value), } } - pub fn property(&self, property: &str) -> glib::Value { + pub fn property(&self, property: &str) -> glib::Value { self.playbin.read().unwrap().property_value(property) } diff --git a/src/music_storage/music_db.rs b/src/music_storage/music_db.rs index ec56547..41d2e22 100644 --- a/src/music_storage/music_db.rs +++ b/src/music_storage/music_db.rs @@ -9,12 +9,12 @@ use std::ops::ControlFlow::{Break, Continue}; // Files use file_format::{FileFormat, Kind}; +use glib::filename_to_uri; use lofty::{AudioFile, ItemKey, ItemValue, ParseOptions, Probe, TagType, TaggedFileExt}; use rcue::parser::parse_from_file; use std::fs; use std::path::{Path, PathBuf}; use walkdir::WalkDir; -use glib::filename_to_uri; // Time use chrono::{serde::ts_milliseconds_option, DateTime, Utc}; @@ -102,7 +102,7 @@ impl ToString for Field { Self::Rating(rating) => rating.to_string(), Self::Format(format) => match format.short_name() { Some(name) => name.to_string(), - None => format.to_string() + None => format.to_string(), }, Self::Duration(duration) => duration.as_millis().to_string(), Self::PlayTime(time) => time.as_millis().to_string(), @@ -153,14 +153,14 @@ impl Song { pub fn get_field(&self, target_field: &str) -> Option { let lower_target = target_field.to_lowercase(); match lower_target.as_str() { - "location" => Some(Field::Location(self.location.clone())), - "plays" => Some(Field::Plays(self.plays)), - "skips" => Some(Field::Skips(self.skips)), + "location" => Some(Field::Location(self.location.clone())), + "plays" => Some(Field::Plays(self.plays)), + "skips" => Some(Field::Skips(self.skips)), "favorited" => Some(Field::Favorited(self.favorited)), - "rating" => self.rating.map(Field::Rating), - "duration" => Some(Field::Duration(self.duration)), + "rating" => self.rating.map(Field::Rating), + "duration" => Some(Field::Duration(self.duration)), "play_time" => Some(Field::PlayTime(self.play_time)), - "format" => self.format.map(Field::Format), + "format" => self.format.map(Field::Format), _ => todo!(), // Other field types are not yet supported } } @@ -228,8 +228,12 @@ impl URI { pub fn as_uri(&self) -> String { let path_str = match self { - URI::Local(location) => filename_to_uri(location, None).expect("couldn't convert path to URI").to_string(), - URI::Cue { location, .. } => filename_to_uri(location, None).expect("couldn't convert path to URI").to_string(), + URI::Local(location) => filename_to_uri(location, None) + .expect("couldn't convert path to URI") + .to_string(), + URI::Cue { location, .. } => filename_to_uri(location, None) + .expect("couldn't convert path to URI") + .to_string(), URI::Remote(_, location) => location.clone(), }; path_str.to_string() @@ -492,7 +496,7 @@ impl MusicLibrary { None => blank_tag, }, } - }, + } Err(_) => blank_tag, }; diff --git a/src/music_storage/utils.rs b/src/music_storage/utils.rs index 5c4865a..03e9740 100644 --- a/src/music_storage/utils.rs +++ b/src/music_storage/utils.rs @@ -1,12 +1,12 @@ +use file_format::{FileFormat, Kind}; use std::io::{BufReader, BufWriter}; use std::path::{Path, PathBuf}; use std::{error::Error, fs}; use walkdir::WalkDir; -use file_format::{FileFormat, Kind}; use snap; -use super::music_db::{Song, AlbumArt, URI}; +use super::music_db::{AlbumArt, Song, URI}; use unidecode::unidecode; pub(super) fn normalize(input_string: &str) -> String { @@ -76,8 +76,9 @@ pub fn find_images(song_path: &Path) -> Result, Box> { .into_iter() .filter_map(|e| e.ok()) { - if target_file.depth() >= 3 { // Don't recurse very deep - break + if target_file.depth() >= 3 { + // Don't recurse very deep + break; } let path = target_file.path(); @@ -87,7 +88,7 @@ pub fn find_images(song_path: &Path) -> Result, Box> { let format = FileFormat::from_file(path)?.kind(); if format != Kind::Image { - break + break; } let image_uri = URI::Local(path.to_path_buf().canonicalize().unwrap());