diff --git a/Cargo.lock b/Cargo.lock index efbd80f..5f1d7e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -149,7 +149,7 @@ checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.48", ] [[package]] @@ -160,7 +160,7 @@ checksum = "a66537f1bb974b254c98ed142ff995236e81b9d0fe4db0575f46612cb15eb0f9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.48", ] [[package]] @@ -413,7 +413,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.48", ] [[package]] @@ -599,6 +599,7 @@ dependencies = [ "sea-orm-migration", "sea-query", "symphonia", + "thiserror", "tokio", "tracing", "tracing-core", @@ -957,7 +958,7 @@ checksum = "ce243b1bfa62ffc028f1cc3b6034ec63d649f3031bc8a4fbbb004e1ac17d1f68" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.48", ] [[package]] @@ -1120,7 +1121,7 @@ checksum = "49e7bc1560b95a3c4a25d03de42fe76ca718ab92d1a22a55b9b4cf67b3ae635c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.48", ] [[package]] @@ -1310,7 +1311,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.48", ] [[package]] @@ -1367,7 +1368,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.48", ] [[package]] @@ -1512,9 +1513,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.69" +version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" dependencies = [ "unicode-ident", ] @@ -1541,9 +1542,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.33" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -1800,7 +1801,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.48", ] [[package]] @@ -1858,7 +1859,7 @@ dependencies = [ "proc-macro2", "quote", "sea-bae", - "syn 2.0.38", + "syn 2.0.48", "unicode-ident", ] @@ -1922,7 +1923,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.48", "thiserror", ] @@ -1995,7 +1996,7 @@ checksum = "1e48d1f918009ce3145511378cf68d613e3b3d9137d67272562080d68a2b32d5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.48", ] [[package]] @@ -2569,9 +2570,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.38" +version = "2.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" dependencies = [ "proc-macro2", "quote", @@ -2620,22 +2621,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.50" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2" +checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.50" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" +checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.48", ] [[package]] @@ -2719,7 +2720,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.48", ] [[package]] @@ -2762,7 +2763,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.48", ] [[package]] @@ -2945,7 +2946,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.48", "wasm-bindgen-shared", ] @@ -2967,7 +2968,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.48", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/Cargo.toml b/Cargo.toml index f3da986..67c8bd9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ sea-orm = { version = "0.12.4", features = ["sqlx-sqlite", "runtime-tokio-native sea-orm-migration = "0.12.4" sea-query = "0.30.2" symphonia = { version = "0.5.3", features = ["flac", "mp3", "vorbis", "ogg", "wav"] } +thiserror = "1.0.56" tokio = { version = "1.33.0", features = ["full"] } tracing = { version = "0.1.40" } tracing-core = "0.1.32" diff --git a/src/backend/config.rs b/src/backend/config.rs index 54ba754..b1933fb 100644 --- a/src/backend/config.rs +++ b/src/backend/config.rs @@ -1,11 +1,12 @@ use std::{fmt::Display, fs::File, io::Write}; use super::{ + error::EleanorError, kdl_utils::{KdlDocumentExt, KdlNodeExt}, utils::config_dir, }; use kdl::{KdlDocument, KdlNode}; -use miette::{miette, IntoDiagnostic, Result}; +use miette::miette; #[derive(Debug)] pub struct Source { @@ -36,12 +37,12 @@ impl Default for Config { } impl Config { - pub fn read_config() -> Result { + pub fn read_config() -> Result { let file = config_dir() .map(|v| v.join("settings.kdl")) .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)?; let kdl_doc: KdlDocument = contents.parse()?; let playback = kdl_doc.get_children_or("playback", KdlDocument::new()); @@ -71,7 +72,7 @@ impl Config { sources .iter() .map(Source::try_from_node) - .collect::>()? + .collect::>()? }; Ok(Self { @@ -83,7 +84,7 @@ impl Config { }) } - pub fn write_config(config: &Config) -> Result<()> { + pub fn write_config(config: &Config) -> Result<(), EleanorError> { let contents = config.to_string(); let path = config_dir() @@ -92,7 +93,7 @@ impl Config { File::create(path) .and_then(|mut v| v.write_all(contents.as_bytes())) - .into_diagnostic() + .map_err(Into::into) } } @@ -133,7 +134,7 @@ impl Display for Config { } impl Source { - fn try_from_node(node: &KdlNode) -> Result { + fn try_from_node(node: &KdlNode) -> Result { let name = node.name().value().to_owned(); let id = node @@ -144,8 +145,7 @@ impl Source { .value() .as_i64() .ok_or(miette!("Source id must be a number"))? - .try_into() - .into_diagnostic()?; + .try_into()?; let path = node .get("path") diff --git a/src/backend/error.rs b/src/backend/error.rs new file mode 100644 index 0000000..16ced2d --- /dev/null +++ b/src/backend/error.rs @@ -0,0 +1,42 @@ +use std::{fmt::Debug, num::TryFromIntError, sync::PoisonError}; + +use kdl::KdlError; +use miette::Diagnostic; +use thiserror::Error; +use tokio::task::JoinError; + +#[derive(Error, Debug, Diagnostic)] +pub enum EleanorError { + #[error("Couldn't unlock mutex")] + LockFailed, + #[error("Failed to convert types")] + CastError, + #[error("Failed to convert from integer: {0}")] + TryFromIntError(#[from] TryFromIntError), + #[error("Failed to create probe: {0}")] + SymponiaError(#[from] symphonia::core::errors::Error), + #[error("Database error: {0}")] + DatabaseError(#[from] sea_orm::DbErr), + #[error("An IO error occured: {0}")] + IoError(#[from] std::io::Error), + #[error("Couldn't join thread: {0}")] + JoinError(#[from] JoinError), + #[error("{0}")] + MietteError(Box), + #[error("Error processing KDL: {0}")] + KdlError(#[from] KdlError), +} + +impl From> for EleanorError { + fn from(_err: PoisonError) -> Self { + Self::LockFailed + } +} + +impl From for EleanorError { + fn from(err: miette::Error) -> Self { + let error: Box = err.into(); + + Self::MietteError(error) + } +} diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 31b5d05..2c87e86 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -4,34 +4,38 @@ mod migrator; #[allow(clippy::pedantic)] pub mod model; // pub mod playback; +pub mod error; mod kdl_utils; pub mod logging; pub mod utils; use std::fs::{create_dir_all, File}; -use miette::{miette, IntoDiagnostic, Result}; +use miette::miette; use migrator::Migrator; use sea_orm_migration::prelude::*; use tracing::info; use crate::backend::config::Config; -use self::utils::{cache_dir, config_dir}; +use self::{ + error::EleanorError, + utils::{cache_dir, config_dir}, +}; /// Create the necessary files on first run -pub fn create_app_data() -> Result<()> { +pub fn create_app_data() -> Result<(), EleanorError> { let config_path = config_dir().ok_or(miette!("Configuration directory does not exist"))?; // Create Eleanor's config directory - create_dir_all(&config_path).into_diagnostic()?; + create_dir_all(&config_path)?; let cache_path = cache_dir().ok_or(miette!("Configuration directory does not exist"))?; // Create Eleanor's cache directory - create_dir_all(cache_path).into_diagnostic()?; + create_dir_all(cache_path)?; - File::create(config_path.join("eleanor.db")).into_diagnostic()?; + File::create(config_path.join("eleanor.db"))?; Config::write_config(&Config::default())?; info!("Created configuration file"); @@ -39,8 +43,8 @@ pub fn create_app_data() -> Result<()> { } /// Run unapplied migrations -pub async fn prepare_db(db: &sea_orm::DatabaseConnection) -> Result<()> { - Migrator::up(db, None).await.into_diagnostic()?; +pub async fn prepare_db(db: &sea_orm::DatabaseConnection) -> Result<(), EleanorError> { + Migrator::up(db, None).await?; info!("Applied migrations"); diff --git a/src/main.rs b/src/main.rs index 37d0b21..f3e668b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ mod gui; #[tokio::main] async fn main() { if let Err(e) = startup().await { - eprintln!("{:?}", e); + eprintln!("{e:?}"); } }