This commit is contained in:
Agatha Lovelace 2022-09-23 20:27:10 +02:00
parent 21e2d06dd7
commit 429755f1e1
Signed by: sorceress
GPG Key ID: 11BBCFC65FC9F401
3 changed files with 14 additions and 19 deletions

View File

@ -23,6 +23,7 @@ pub struct Source {
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(default)]
pub struct Config { pub struct Config {
pub cache_expire_days: usize, pub cache_expire_days: usize,
pub crossfade: bool, pub crossfade: bool,
@ -31,10 +32,11 @@ pub struct Config {
pub volume: f32, pub volume: f32,
pub sources: Vec<Source>, pub sources: Vec<Source>,
} }
impl Config { impl Config {
pub fn read_config() -> Result<Self> { pub fn read_config() -> Result<Self> {
let file = config_dir() 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"))?; .ok_or(miette!("Configuration file not found"))?;
let contents = std::fs::read_to_string(file).into_diagnostic()?; 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 contents = toml::to_string(config).into_diagnostic()?;
let path = config_dir() 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"))?; .ok_or(miette!("Configuration file not found"))?;
File::create(path) File::create(path)
@ -55,6 +57,7 @@ impl Config {
} }
} }
// TODO: Initialize sources to empty list instead
impl Default for Config { impl Default for Config {
fn default() -> Self { fn default() -> Self {
Config { Config {

View File

@ -65,7 +65,7 @@ pub async fn index_source(source: Source, mode: IndexMode, db: &DatabaseConnecti
.filter(|e| { .filter(|e| {
mime_guess::from_path(e.path()) mime_guess::from_path(e.path())
.first() .first()
.and_then(|v| Some(v.type_() == mime::AUDIO)) .map(|v| v.type_() == mime::AUDIO)
.unwrap_or(false) .unwrap_or(false)
}) })
{ {
@ -97,20 +97,12 @@ pub async fn index_source(source: Source, mode: IndexMode, db: &DatabaseConnecti
.to_string()), .to_string()),
source_id: Set(source.id.into()), source_id: Set(source.id.into()),
hash: Set(hash.try_into().into_diagnostic()?), hash: Set(hash.try_into().into_diagnostic()?),
artist: Set(tags artist: Set(tags.and_then(|t| t.artist()).map(|t| t.to_string())),
.and_then(|t| t.artist()) name: Set(tags.and_then(|t| t.title()).map(|t| t.to_string())),
.and_then(|t| Some(t.to_string()))), album: Set(tags.and_then(|t| t.album()).map(|t| t.to_string())),
name: Set(tags genres: Set(tags.and_then(|t| t.genre()).map(|t| t.to_string())),
.and_then(|t| t.title()) track: Set(tags.and_then(|t| t.track()).map(|t| t as i32)),
.and_then(|t| Some(t.to_string()))), year: Set(tags.and_then(|t| t.year()).map(|t| t as i32)),
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))),
duration: Set(properties duration: Set(properties
.duration() .duration()
.as_millis() .as_millis()

View File

@ -2,12 +2,12 @@ use miette::{miette, Result};
use std::path::PathBuf; use std::path::PathBuf;
pub fn config_dir() -> Option<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)] #[allow(dead_code)]
pub fn cache_dir() -> Option<PathBuf> { 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 /// If no files have been created in the config directory, the app is running for the first time