From 429755f1e156663178323e54d91ebb452b04fc4a Mon Sep 17 00:00:00 2001 From: Agatha Lovelace Date: Fri, 23 Sep 2022 20:27:10 +0200 Subject: [PATCH] Refactor --- src/backend/config.rs | 7 +++++-- src/backend/fetching.rs | 22 +++++++--------------- src/backend/utils.rs | 4 ++-- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/backend/config.rs b/src/backend/config.rs index d415c4c..91c8bd1 100644 --- a/src/backend/config.rs +++ b/src/backend/config.rs @@ -23,6 +23,7 @@ pub struct Source { } #[derive(Serialize, Deserialize, Debug)] +#[serde(default)] pub struct Config { pub cache_expire_days: usize, pub crossfade: bool, @@ -31,10 +32,11 @@ pub struct Config { pub volume: f32, pub sources: Vec, } + impl Config { pub fn read_config() -> Result { let file = config_dir() - .and_then(|v| Some(v.join("settings.toml"))) + .map(|v| v.join("settings.toml")) .ok_or(miette!("Configuration file not found"))?; let contents = std::fs::read_to_string(file).into_diagnostic()?; @@ -46,7 +48,7 @@ impl Config { let contents = toml::to_string(config).into_diagnostic()?; let path = config_dir() - .and_then(|v| Some(v.join("settings.toml"))) + .map(|v| v.join("settings.toml")) .ok_or(miette!("Configuration file not found"))?; File::create(path) @@ -55,6 +57,7 @@ impl Config { } } +// TODO: Initialize sources to empty list instead impl Default for Config { fn default() -> Self { Config { diff --git a/src/backend/fetching.rs b/src/backend/fetching.rs index 5ce55d8..0852caf 100644 --- a/src/backend/fetching.rs +++ b/src/backend/fetching.rs @@ -65,7 +65,7 @@ pub async fn index_source(source: Source, mode: IndexMode, db: &DatabaseConnecti .filter(|e| { mime_guess::from_path(e.path()) .first() - .and_then(|v| Some(v.type_() == mime::AUDIO)) + .map(|v| v.type_() == mime::AUDIO) .unwrap_or(false) }) { @@ -97,20 +97,12 @@ pub async fn index_source(source: Source, mode: IndexMode, db: &DatabaseConnecti .to_string()), source_id: Set(source.id.into()), hash: Set(hash.try_into().into_diagnostic()?), - artist: Set(tags - .and_then(|t| t.artist()) - .and_then(|t| Some(t.to_string()))), - name: Set(tags - .and_then(|t| t.title()) - .and_then(|t| Some(t.to_string()))), - album: Set(tags - .and_then(|t| t.album()) - .and_then(|t| Some(t.to_string()))), - genres: Set(tags - .and_then(|t| t.genre()) - .and_then(|t| Some(t.to_string()))), - track: Set(tags.and_then(|t| t.track()).and_then(|t| Some(t as i32))), - year: Set(tags.and_then(|t| t.year()).and_then(|t| Some(t as i32))), + artist: Set(tags.and_then(|t| t.artist()).map(|t| t.to_string())), + name: Set(tags.and_then(|t| t.title()).map(|t| t.to_string())), + album: Set(tags.and_then(|t| t.album()).map(|t| t.to_string())), + genres: Set(tags.and_then(|t| t.genre()).map(|t| t.to_string())), + track: Set(tags.and_then(|t| t.track()).map(|t| t as i32)), + year: Set(tags.and_then(|t| t.year()).map(|t| t as i32)), duration: Set(properties .duration() .as_millis() diff --git a/src/backend/utils.rs b/src/backend/utils.rs index 0bdafbe..f73d205 100644 --- a/src/backend/utils.rs +++ b/src/backend/utils.rs @@ -2,12 +2,12 @@ use miette::{miette, Result}; use std::path::PathBuf; pub fn config_dir() -> Option { - dirs::config_dir().and_then(|v| Some(v.join("eleanor"))) + dirs::config_dir().map(|v| v.join("eleanor")) } #[allow(dead_code)] pub fn cache_dir() -> Option { - dirs::cache_dir().and_then(|v| Some(v.join("eleanor"))) + dirs::cache_dir().map(|v| v.join("eleanor")) } /// If no files have been created in the config directory, the app is running for the first time