mirror of
https://github.com/Dangoware/dmp-core.git
synced 2025-04-19 09:42:53 -05:00
Cleaned up many warnings
This commit is contained in:
parent
f79bf8d477
commit
0fa97c27d0
5 changed files with 58 additions and 90 deletions
|
@ -1,7 +1,7 @@
|
|||
use std::{
|
||||
fs::{self, File, OpenOptions},
|
||||
io::{Error, Read, Write},
|
||||
path::PathBuf,
|
||||
fs::{File, OpenOptions, self},
|
||||
io::{Error, Write, Read},
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
@ -41,7 +41,7 @@ impl ConfigLibrary {
|
|||
pub fn open(&self) -> Result<File, Error> {
|
||||
match File::open(self.path.as_path()) {
|
||||
Ok(ok) => Ok(ok),
|
||||
Err(e) => Err(e)
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -62,18 +62,17 @@ impl ConfigLibraries {
|
|||
pub fn get_default(&self) -> Result<&ConfigLibrary, ConfigError> {
|
||||
for library in &self.libraries {
|
||||
if library.uuid == self.default_library {
|
||||
return Ok(library)
|
||||
return Ok(library);
|
||||
}
|
||||
}
|
||||
Err(ConfigError::NoDefaultLibrary)
|
||||
}
|
||||
|
||||
pub fn get_library(&self, uuid: &Uuid) -> Result<ConfigLibrary, ConfigError> {
|
||||
|
||||
for library in &self.libraries {
|
||||
// dbg!(&library.uuid, &uuid);
|
||||
if &library.uuid == uuid {
|
||||
return Ok(library.to_owned())
|
||||
return Ok(library.to_owned());
|
||||
}
|
||||
}
|
||||
Err(ConfigError::NoConfigLibrary(*uuid))
|
||||
|
@ -82,7 +81,7 @@ impl ConfigLibraries {
|
|||
pub fn uuid_exists(&self, uuid: &Uuid) -> bool {
|
||||
for library in &self.libraries {
|
||||
if &library.uuid == uuid {
|
||||
return true
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
|
@ -91,7 +90,7 @@ impl ConfigLibraries {
|
|||
|
||||
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
|
||||
pub struct ConfigConnections {
|
||||
pub listenbrainz_token: Option<String>
|
||||
pub listenbrainz_token: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
|
||||
|
@ -122,7 +121,12 @@ impl Config {
|
|||
pub fn write_file(&self) -> Result<(), Error> {
|
||||
let mut writer = self.path.clone();
|
||||
writer.set_extension("tmp");
|
||||
let mut file = OpenOptions::new().create(true).truncate(true).read(true).write(true).open(&writer)?;
|
||||
let mut file = OpenOptions::new()
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.read(true)
|
||||
.write(true)
|
||||
.open(&writer)?;
|
||||
let config = to_string_pretty(self)?;
|
||||
// dbg!(&config);
|
||||
|
||||
|
@ -136,15 +140,20 @@ impl Config {
|
|||
Some(path) => {
|
||||
let mut writer = path.clone();
|
||||
writer.set_extension("tmp");
|
||||
let mut file = OpenOptions::new().create(true).truncate(true).read(true).write(true).open(&writer)?;
|
||||
let mut file = OpenOptions::new()
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.read(true)
|
||||
.write(true)
|
||||
.open(&writer)?;
|
||||
let config = to_string_pretty(self)?;
|
||||
// dbg!(&config);
|
||||
|
||||
file.write_all(config.as_bytes())?;
|
||||
fs::rename(writer, self.path.as_path())?;
|
||||
Ok(())
|
||||
},
|
||||
None => Err(ConfigError::NoBackupLibrary.into())
|
||||
}
|
||||
None => Err(ConfigError::NoBackupLibrary.into()),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -175,17 +184,23 @@ pub enum ConfigError {
|
|||
BadPlaylist,
|
||||
#[error("No backup Config folder present")]
|
||||
NoBackupLibrary,
|
||||
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use std::{path::PathBuf, sync::{Arc, RwLock}};
|
||||
use super::{Config, ConfigLibrary};
|
||||
use crate::music_storage::library::MusicLibrary;
|
||||
use super::{Config, ConfigLibraries, ConfigLibrary};
|
||||
use std::{
|
||||
path::PathBuf,
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
|
||||
pub fn new_config_lib() -> (Config, MusicLibrary) {
|
||||
let lib = ConfigLibrary::new(PathBuf::from("test-config/library"), String::from("library"), None);
|
||||
let lib = ConfigLibrary::new(
|
||||
PathBuf::from("test-config/library"),
|
||||
String::from("library"),
|
||||
None,
|
||||
);
|
||||
let mut config = Config {
|
||||
path: PathBuf::from("test-config/config_test.json"),
|
||||
..Default::default()
|
||||
|
@ -194,7 +209,11 @@ pub mod tests {
|
|||
config.push_library(lib);
|
||||
config.write_file().unwrap();
|
||||
|
||||
let mut lib = MusicLibrary::init(Arc::new(RwLock::from(config.clone())), dbg!(config.libraries.default_library)).unwrap();
|
||||
let mut lib = MusicLibrary::init(
|
||||
Arc::new(RwLock::from(config.clone())),
|
||||
dbg!(config.libraries.default_library),
|
||||
)
|
||||
.unwrap();
|
||||
lib.scan_folder("test-config/music/").unwrap();
|
||||
lib.save(config.clone()).unwrap();
|
||||
|
||||
|
@ -206,20 +225,22 @@ pub mod tests {
|
|||
|
||||
// dbg!(&config);
|
||||
|
||||
let mut lib = MusicLibrary::init(Arc::new(RwLock::from(config.clone())), config.libraries.get_default().unwrap().uuid).unwrap();
|
||||
|
||||
let mut lib = MusicLibrary::init(
|
||||
Arc::new(RwLock::from(config.clone())),
|
||||
config.libraries.get_default().unwrap().uuid,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
lib.scan_folder("test-config/music/").unwrap();
|
||||
|
||||
lib.save(config.clone()).unwrap();
|
||||
|
||||
|
||||
(config, lib)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test3() {
|
||||
let (config, lib) = read_config_lib();
|
||||
let (config, _) = read_config_lib();
|
||||
|
||||
_ = config.write_file();
|
||||
|
||||
|
|
|
@ -12,7 +12,6 @@ use std::error::Error;
|
|||
use uuid::Uuid;
|
||||
|
||||
use crate::music_player::player::Player;
|
||||
use crate::music_storage::library::URI;
|
||||
use crate::{
|
||||
config::config::Config, music_controller::queue::Queue, music_storage::library::MusicLibrary,
|
||||
};
|
||||
|
@ -21,7 +20,7 @@ pub struct Controller<P: Player> {
|
|||
pub queue: Queue,
|
||||
pub config: Arc<RwLock<Config>>,
|
||||
pub library: MusicLibrary,
|
||||
pub player: P,
|
||||
pub player: Box<P>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -55,19 +54,12 @@ impl<T: Send, U: Send> MailMan<T, U> {
|
|||
}
|
||||
}
|
||||
|
||||
enum PlayerCmd {
|
||||
Test(URI),
|
||||
}
|
||||
|
||||
enum PlayerRes {
|
||||
Test,
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
impl<P> Controller<P> {
|
||||
impl<P: Player> Controller<P> {
|
||||
pub fn start<T>(config_path: T) -> Result<Self, Box<dyn Error>>
|
||||
where
|
||||
std::path::PathBuf: std::convert::From<T>,
|
||||
P: Player,
|
||||
{
|
||||
let config_path = PathBuf::from(config_path);
|
||||
|
||||
|
@ -77,13 +69,11 @@ impl<P> Controller<P> {
|
|||
let config_ = Arc::new(RwLock::from(config));
|
||||
let library = MusicLibrary::init(config_.clone(), uuid)?;
|
||||
|
||||
let (player_mail, in_thread) = MailMan::<PlayerCmd, PlayerRes>::double();
|
||||
|
||||
Ok(Controller {
|
||||
queue: Queue::new(),
|
||||
queue: Queue::default(),
|
||||
config: config_.clone(),
|
||||
library,
|
||||
player: P::new(),
|
||||
player: Box::new(P::new()),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -94,27 +84,4 @@ impl<P> Controller<P> {
|
|||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_super {
|
||||
use std::{thread::sleep, time::Duration};
|
||||
|
||||
use super::Controller;
|
||||
|
||||
#[test]
|
||||
fn play_test() {
|
||||
let mut a = match Controller::start("test-config/config_test.json".to_string()) {
|
||||
Ok(c) => c,
|
||||
Err(e) => panic!("{e}"),
|
||||
};
|
||||
sleep(Duration::from_millis(500));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_() {
|
||||
let c = Controller::start(
|
||||
"F:\\Dangoware\\Dango Music Player\\dmp-core\\test-config\\config_test.json",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
sleep(Duration::from_secs(60));
|
||||
}
|
||||
}
|
||||
mod test_super {}
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
use crate::music_storage::library::{MusicLibrary, Song, URI};
|
||||
use std::{
|
||||
error::Error,
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
use crate::music_storage::library::Song;
|
||||
use std::error::Error;
|
||||
use uuid::Uuid;
|
||||
|
||||
use thiserror::Error;
|
||||
|
@ -53,7 +50,7 @@ impl From<Song> for QueueItem {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Queue {
|
||||
pub items: Vec<QueueItem>,
|
||||
pub played: Vec<QueueItem>,
|
||||
|
@ -81,16 +78,6 @@ impl Queue {
|
|||
);
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
//TODO: Make the queue take settings from config/state if applicable
|
||||
Queue {
|
||||
items: Vec::new(),
|
||||
played: Vec::new(),
|
||||
loop_: false,
|
||||
shuffle: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_items(&mut self, tracks: Vec<QueueItem>) {
|
||||
let mut tracks = tracks;
|
||||
self.items.clear();
|
||||
|
@ -131,7 +118,7 @@ impl Queue {
|
|||
let empty = self.items.is_empty();
|
||||
|
||||
self.items.insert(
|
||||
(if empty { 0 } else { 1 }),
|
||||
if empty { 0 } else { 1 },
|
||||
QueueItem {
|
||||
item,
|
||||
state: if (self.items.get(1).is_none()
|
||||
|
|
|
@ -642,7 +642,7 @@ impl MusicLibrary {
|
|||
name,
|
||||
uuid,
|
||||
library: Vec::new(),
|
||||
playlists: PlaylistFolder::new(),
|
||||
playlists: PlaylistFolder::default(),
|
||||
backup_songs: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use std::default;
|
||||
use std::error::Error;
|
||||
use std::{
|
||||
fs::File,
|
||||
|
@ -26,6 +27,7 @@ pub enum SortOrder {
|
|||
|
||||
nest! {
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]*
|
||||
#[derive(Default)]
|
||||
pub struct PlaylistFolder {
|
||||
name: String,
|
||||
items: Vec<
|
||||
|
@ -37,15 +39,6 @@ nest! {
|
|||
}
|
||||
}
|
||||
|
||||
impl PlaylistFolder {
|
||||
pub fn new() -> Self {
|
||||
PlaylistFolder {
|
||||
name: String::new(),
|
||||
items: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct Playlist {
|
||||
uuid: Uuid,
|
||||
|
@ -233,7 +226,7 @@ impl Playlist {
|
|||
#[cfg(target_family = "windows")]
|
||||
{
|
||||
playlist.title = path
|
||||
.split("\\")
|
||||
.split('\\')
|
||||
.last()
|
||||
.unwrap_or_default()
|
||||
.strip_suffix(".m3u8")
|
||||
|
@ -339,7 +332,7 @@ impl Default for Playlist {
|
|||
#[cfg(test)]
|
||||
mod test_super {
|
||||
use super::*;
|
||||
use crate::{config::config::tests::read_config_lib, music_storage::playlist};
|
||||
use crate::config::config::tests::read_config_lib;
|
||||
|
||||
#[test]
|
||||
fn list_to_m3u8() {
|
||||
|
@ -360,7 +353,7 @@ mod test_super {
|
|||
let playlist =
|
||||
Playlist::from_m3u8(".\\test-config\\playlists\\playlist.m3u8", arc).unwrap();
|
||||
|
||||
playlist.to_file(".\\test-config\\playlists\\playlist");
|
||||
_ = playlist.to_file(".\\test-config\\playlists\\playlist");
|
||||
dbg!(playlist)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue