From ab8ecf81214e7d5590506b7074a68ae42f18a331 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 23 Aug 2022 12:26:53 +0200 Subject: [PATCH] transaction: use proper 'ParseError' type --- Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 1 + src/error.rs | 11 +++++++++++ src/lib.rs | 3 +++ src/transaction.rs | 14 ++++++++------ 5 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 src/error.rs diff --git a/Cargo.lock b/Cargo.lock index 4c0d8c9..92ce977 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -296,6 +296,7 @@ dependencies = [ "fpdec", "serde", "serde_with", + "thiserror", ] [[package]] @@ -395,6 +396,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "1.0.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5f6586b7f764adc0231f4c79be7b920e766bb2f3e51b3661cdb263828f19994" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12bafc5b54507e0149cdf1b145a5d80ab80a90bcd9275df43d4fff68460f6c21" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "time" version = "0.3.13" diff --git a/Cargo.toml b/Cargo.toml index 9f5c338..2653096 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ csv = "1.1" fpdec = "0.5" serde = { version = "1.0", features = ["derive"] } serde_with = "2.0" +thiserror = "1.0" diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..59594be --- /dev/null +++ b/src/error.rs @@ -0,0 +1,11 @@ +//! Error types for this crate. +use thiserror::Error; + +/// Any kind of error that can happen when deserializing a [crate::Transaction] value. +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Error)] +pub enum ParseError { + #[error("amount not provided")] + MissingAmount, + #[error("unknown transaction type '{0}'")] + UnknownTx(String), +} diff --git a/src/lib.rs b/src/lib.rs index f82bd39..f5d3a10 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,8 @@ pub mod core; pub use crate::core::*; +pub mod error; +pub use crate::error::*; + pub mod transaction; pub use crate::transaction::*; diff --git a/src/transaction.rs b/src/transaction.rs index 6ada41c..faebc54 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -1,5 +1,8 @@ //! Define all supported transactions. -use crate::core::{ClientId, TxAmount, TxId}; +use crate::{ + core::{ClientId, TxAmount, TxId}, + ParseError, +}; use serde::Deserialize; @@ -40,8 +43,7 @@ struct TransactionRecord<'a> { } impl TryFrom> for Transaction { - // FIXME: use an actual error type. - type Error = String; + type Error = ParseError; fn try_from(value: TransactionRecord<'_>) -> Result { let TransactionRecord { @@ -53,17 +55,17 @@ impl TryFrom> for Transaction { let transaction = match type_ { "deposit" => { - let amount = amount.ok_or("Missing amount for transaction")?; + let amount = amount.ok_or(ParseError::MissingAmount)?; Transaction::Deposit(Deposit { client, tx, amount }) } "withdrawal" => { - let amount = amount.ok_or("Missing amount for transaction")?; + let amount = amount.ok_or(ParseError::MissingAmount)?; Transaction::Withdrawal(Withdrawal { client, tx, amount }) } "dispute" => Transaction::Dispute(Dispute { client, tx }), "resolve" => Transaction::Resolve(Resolve { client, tx }), "chargeback" => Transaction::Chargeback(Chargeback { client, tx }), - _ => return Err(format!("Unkown transaction type '{}'", type_)), + _ => return Err(ParseError::UnknownTx(type_.into())), }; Ok(transaction) }