Refactor
This commit is contained in:
parent
21e2d06dd7
commit
429755f1e1
|
@ -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<Source>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn read_config() -> Result<Self> {
|
||||
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 {
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -2,12 +2,12 @@ use miette::{miette, Result};
|
|||
use std::path::PathBuf;
|
||||
|
||||
pub fn config_dir() -> Option<PathBuf> {
|
||||
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<PathBuf> {
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue