Implemented thiserror.
This commit is contained in:
@@ -28,13 +28,6 @@ impl TryFrom<String> for Direction {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[error("invalid direction value: {0}")]
|
||||
pub struct DirectionConversionError(pub String);
|
||||
|
||||
impl std::error::Error for DirectionConversionError {}
|
||||
|
||||
impl std::fmt::Display for DirectionConversionError {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(formatter, "invalid direction value: {}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,37 +146,24 @@ impl TryFrom<String> for WeekendAdjustment {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum RecurringTransactionConversionError {
|
||||
#[error("invalid amount_cents value: {0}")]
|
||||
AmountCents(i64),
|
||||
Direction(DirectionConversionError),
|
||||
|
||||
#[error(transparent)]
|
||||
Direction(#[from] DirectionConversionError),
|
||||
|
||||
#[error("invalid recurrence_every value: {0}")]
|
||||
RecurrenceEvery(i32),
|
||||
|
||||
#[error("invalid recurrence_unit value: {0}")]
|
||||
RecurrenceUnit(String),
|
||||
|
||||
#[error("invalid weekend_adjustment value: {0}")]
|
||||
WeekendAdjustment(String),
|
||||
}
|
||||
|
||||
impl std::error::Error for RecurringTransactionConversionError {}
|
||||
|
||||
impl std::fmt::Display for RecurringTransactionConversionError {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::AmountCents(value) => {
|
||||
write!(formatter, "invalid amount_cents value: {value}")
|
||||
}
|
||||
Self::Direction(error) => error.fmt(formatter),
|
||||
Self::RecurrenceEvery(value) => {
|
||||
write!(formatter, "invalid recurrence_every value: {value}")
|
||||
}
|
||||
Self::RecurrenceUnit(value) => {
|
||||
write!(formatter, "invalid recurrence_unit value: {value}")
|
||||
}
|
||||
Self::WeekendAdjustment(value) => {
|
||||
write!(formatter, "invalid weekend_adjustment value: {value}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::{TimeZone, Utc};
|
||||
|
||||
@@ -32,23 +32,13 @@ impl TryFrom<TransactionRow> for TransactionResponse {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum TransactionConversionError {
|
||||
#[error("invalid amount_cents value: {0}")]
|
||||
AmountCents(i64),
|
||||
Direction(DirectionConversionError),
|
||||
}
|
||||
|
||||
impl std::error::Error for TransactionConversionError {}
|
||||
|
||||
impl std::fmt::Display for TransactionConversionError {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::AmountCents(value) => {
|
||||
write!(formatter, "invalid amount_cents value: {value}")
|
||||
}
|
||||
Self::Direction(error) => error.fmt(formatter),
|
||||
}
|
||||
}
|
||||
#[error(transparent)]
|
||||
Direction(#[from] DirectionConversionError),
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -82,31 +82,18 @@ impl TryFrom<u32> for PositiveRecurrenceInterval {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ScheduledTransactionValidationError {
|
||||
#[error("amount is too large")]
|
||||
AmountOutOfRange,
|
||||
|
||||
#[error("recurrence every must be between 1 and {}: {}", i32::MAX, .0)]
|
||||
RecurrenceEvery(u32),
|
||||
|
||||
#[error("end must not be before start")]
|
||||
EndBeforeStart,
|
||||
}
|
||||
|
||||
impl std::error::Error for ScheduledTransactionValidationError {}
|
||||
|
||||
impl std::fmt::Display for ScheduledTransactionValidationError {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::AmountOutOfRange => write!(formatter, "amount is too large"),
|
||||
Self::RecurrenceEvery(value) => {
|
||||
write!(
|
||||
formatter,
|
||||
"recurrence every must be between 1 and {}: {value}",
|
||||
i32::MAX
|
||||
)
|
||||
}
|
||||
Self::EndBeforeStart => write!(formatter, "end must not be before start"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::{TimeZone, Utc};
|
||||
|
||||
Reference in New Issue
Block a user