Use thiserror crate

This commit is contained in:
Anas Elgarhy 2023-02-15 01:39:07 +02:00
parent 538ad72f2d
commit 9ffcf036f2
No known key found for this signature in database
GPG Key ID: 0501802A1D496528
3 changed files with 16 additions and 20 deletions

1
Cargo.lock generated
View File

@ -227,6 +227,7 @@ dependencies = [
"serde",
"temp-file",
"test-context",
"thiserror",
"typed-builder",
]

View File

@ -15,6 +15,7 @@ temp-file = "0.1.7"
typed-builder = "0.12.0"
log = { version = "0.4.17", optional = true }
pretty_env_logger = { version = "0.4.0", optional = true }
thiserror = "1.0.38"
[dependencies.clap]
version = "4.1.4"

View File

@ -3,13 +3,14 @@ pub mod player_settings;
pub mod query;
use crate::cmus::query::CmusQueryResponse;
#[cfg(feature = "debug")]
use log::{debug, info};
use std::collections::HashMap;
use std::fmt::{Debug, Display};
use std::fmt::Debug;
use std::num::ParseIntError;
use std::str::FromStr;
use thiserror::Error;
use typed_builder::TypedBuilder;
#[cfg(feature = "debug")]
use log::{debug, info};
#[derive(Debug, PartialEq, Default)]
pub struct TrackMetadata {
@ -33,35 +34,28 @@ pub struct Track {
pub position: u32,
}
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Error)]
pub enum CmusError {
#[error("Cmus running error: {0}")]
CmusRunningError(String),
#[error("Unknown status")]
UnknownStatus,
#[error("No status")]
NoStatus,
#[error("Empty path")]
EmptyPath,
#[error("Duration error: {0}")]
DurationError(String),
#[error("Position error: {0}")]
PositionError(String),
#[error("Unknown error: {0}")]
UnknownError(String),
#[error("Unknown AAA mode: {0}")]
UnknownAAAMode(String),
#[error("Unknown shuffle mode: {0}")]
UnknownShuffleMode(String),
}
impl Display for CmusError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CmusError::CmusRunningError(s) => write!(f, "Cmus running error: {}", s),
CmusError::UnknownStatus => write!(f, "Unknown status"),
CmusError::NoStatus => write!(f, "No status"),
CmusError::EmptyPath => write!(f, "Empty path"),
CmusError::DurationError(s) => write!(f, "Duration error: {}", s),
CmusError::PositionError(s) => write!(f, "Position error: {}", s),
CmusError::UnknownError(s) => write!(f, "Unknown error: {}", s),
CmusError::UnknownAAAMode(s) => write!(f, "Unknown AAA mode: {}", s),
CmusError::UnknownShuffleMode(s) => write!(f, "Unknown shuffle mode: {}", s),
}
}
}
impl FromStr for TrackStatus {
type Err = CmusError;