Renamed AmountCents to just Cents.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
pub mod amount_cents;
|
||||
pub mod cents;
|
||||
pub mod direction;
|
||||
pub mod scheduled_transaction;
|
||||
pub mod transaction;
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct AmountCents(u64);
|
||||
|
||||
impl AmountCents {
|
||||
pub fn get(&self) -> u64 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u64> for AmountCents {
|
||||
fn from(value: u64) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<i64> for AmountCents {
|
||||
type Error = AmountCentsConversionError;
|
||||
|
||||
fn try_from(value: i64) -> Result<Self, Self::Error> {
|
||||
u64::try_from(value)
|
||||
.map(Self)
|
||||
.map_err(|_| AmountCentsConversionError(value.into()))
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<AmountCents> for i64 {
|
||||
type Error = AmountCentsConversionError;
|
||||
|
||||
fn try_from(value: AmountCents) -> Result<Self, Self::Error> {
|
||||
i64::try_from(value.get()).map_err(|_| AmountCentsConversionError(value.get().into()))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[error("invalid amount_cents value: {0}")]
|
||||
pub struct AmountCentsConversionError(pub i128);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{AmountCents, AmountCentsConversionError};
|
||||
|
||||
#[test]
|
||||
fn converts_non_negative_database_values() {
|
||||
assert_eq!(AmountCents::try_from(4250_i64).unwrap().get(), 4250);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_negative_database_values() {
|
||||
assert!(matches!(
|
||||
AmountCents::try_from(-1_i64),
|
||||
Err(AmountCentsConversionError(-1_i128))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_values_too_large_for_the_database() {
|
||||
assert!(matches!(
|
||||
i64::try_from(AmountCents::from(i64::MAX as u64 + 1)),
|
||||
Err(AmountCentsConversionError(value)) if value == i64::MAX as i128 + 1
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct Cents(u64);
|
||||
|
||||
impl Cents {
|
||||
pub fn get(&self) -> u64 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u64> for Cents {
|
||||
fn from(value: u64) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<i64> for Cents {
|
||||
type Error = CentsConversionError;
|
||||
|
||||
fn try_from(value: i64) -> Result<Self, Self::Error> {
|
||||
u64::try_from(value)
|
||||
.map(Self)
|
||||
.map_err(|_| CentsConversionError(value.into()))
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Cents> for i64 {
|
||||
type Error = CentsConversionError;
|
||||
|
||||
fn try_from(value: Cents) -> Result<Self, Self::Error> {
|
||||
i64::try_from(value.get()).map_err(|_| CentsConversionError(value.get().into()))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[error("invalid cents value: {0}")]
|
||||
pub struct CentsConversionError(pub i128);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Cents, CentsConversionError};
|
||||
|
||||
#[test]
|
||||
fn converts_non_negative_database_values() {
|
||||
assert_eq!(Cents::try_from(4250_i64).unwrap().get(), 4250);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_negative_database_values() {
|
||||
assert!(matches!(
|
||||
Cents::try_from(-1_i64),
|
||||
Err(CentsConversionError(-1_i128))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_values_too_large_for_the_database() {
|
||||
assert!(matches!(
|
||||
i64::try_from(Cents::from(i64::MAX as u64 + 1)),
|
||||
Err(CentsConversionError(value)) if value == i64::MAX as i128 + 1
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ use uuid::Uuid;
|
||||
use crate::{
|
||||
database::dtos::recurring_transaction::RecurringTransactionRow,
|
||||
routes::dtos::{
|
||||
amount_cents::{AmountCents, AmountCentsConversionError},
|
||||
cents::{Cents, CentsConversionError},
|
||||
direction::{Direction, DirectionConversionError},
|
||||
},
|
||||
};
|
||||
@@ -39,7 +39,7 @@ impl TryFrom<RecurringTransactionRow> for RecurringTransaction {
|
||||
id: row.id,
|
||||
data: RecurringTransactionData {
|
||||
name: row.name,
|
||||
amount_cents: AmountCents::try_from(row.amount_cents)?,
|
||||
amount_cents: Cents::try_from(row.amount_cents)?,
|
||||
direction: Direction::try_from(row.direction)
|
||||
.map_err(RecurringTransactionConversionError::Direction)?,
|
||||
start: row.starts_at,
|
||||
@@ -69,7 +69,7 @@ pub enum ScheduledTransactionData {
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct RecurringTransactionData {
|
||||
pub name: String,
|
||||
pub amount_cents: AmountCents,
|
||||
pub amount_cents: Cents,
|
||||
pub direction: Direction,
|
||||
pub start: DateTime<Utc>,
|
||||
pub end: Option<DateTime<Utc>>,
|
||||
@@ -150,7 +150,7 @@ impl TryFrom<String> for WeekendAdjustment {
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum RecurringTransactionConversionError {
|
||||
#[error(transparent)]
|
||||
AmountCents(#[from] AmountCentsConversionError),
|
||||
Cents(#[from] CentsConversionError),
|
||||
|
||||
#[error(transparent)]
|
||||
Direction(#[from] DirectionConversionError),
|
||||
|
||||
@@ -4,7 +4,7 @@ use uuid::Uuid;
|
||||
|
||||
use crate::database::dtos::transaction::TransactionRow;
|
||||
use crate::routes::dtos::{
|
||||
amount_cents::{AmountCents, AmountCentsConversionError},
|
||||
cents::{Cents, CentsConversionError},
|
||||
direction::{Direction, DirectionConversionError},
|
||||
};
|
||||
|
||||
@@ -19,7 +19,7 @@ pub struct Transaction {
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TransactionData {
|
||||
pub description: String,
|
||||
pub amount_cents: AmountCents,
|
||||
pub amount_cents: Cents,
|
||||
pub direction: Direction,
|
||||
pub date: DateTime<Utc>,
|
||||
}
|
||||
@@ -32,7 +32,7 @@ impl TryFrom<TransactionRow> for Transaction {
|
||||
id: row.id,
|
||||
data: TransactionData {
|
||||
description: row.description,
|
||||
amount_cents: AmountCents::try_from(row.amount_cents)?,
|
||||
amount_cents: Cents::try_from(row.amount_cents)?,
|
||||
direction: Direction::try_from(row.direction)
|
||||
.map_err(TransactionConversionError::Direction)?,
|
||||
date: row.date,
|
||||
@@ -44,7 +44,7 @@ impl TryFrom<TransactionRow> for Transaction {
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum TransactionConversionError {
|
||||
#[error(transparent)]
|
||||
AmountCents(#[from] AmountCentsConversionError),
|
||||
Cents(#[from] CentsConversionError),
|
||||
|
||||
#[error(transparent)]
|
||||
Direction(#[from] DirectionConversionError),
|
||||
|
||||
@@ -76,8 +76,8 @@ async fn put_scheduled_transaction(
|
||||
Json(transaction): Json<ScheduledTransactionData>,
|
||||
) -> Result<(StatusCode, Json<ScheduledTransaction>), AppError> {
|
||||
let ScheduledTransactionData::Recurring(transaction) = transaction;
|
||||
let transaction = ValidatedRecurringTransaction::try_from(transaction)?;
|
||||
|
||||
let transaction = ValidatedRecurringTransaction::try_from(transaction)?;
|
||||
let transaction = RecurringTransactionRow {
|
||||
id,
|
||||
name: transaction.name,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
use crate::routes::dtos::{
|
||||
amount_cents::AmountCentsConversionError,
|
||||
cents::CentsConversionError,
|
||||
direction::Direction,
|
||||
scheduled_transaction::{RecurrenceUnit, RecurringTransactionData, WeekendAdjustment},
|
||||
};
|
||||
@@ -67,7 +67,7 @@ impl TryFrom<u32> for PositiveRecurrenceInterval {
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ScheduledTransactionValidationError {
|
||||
#[error(transparent)]
|
||||
AmountCents(#[from] AmountCentsConversionError),
|
||||
Cents(#[from] CentsConversionError),
|
||||
|
||||
#[error("recurrence every must be between 1 and {}: {}", i32::MAX, .0)]
|
||||
RecurrenceEvery(u32),
|
||||
@@ -85,7 +85,7 @@ mod tests {
|
||||
ValidatedRecurringTransaction,
|
||||
};
|
||||
use crate::routes::dtos::{
|
||||
amount_cents::AmountCentsConversionError,
|
||||
cents::CentsConversionError,
|
||||
direction::Direction,
|
||||
scheduled_transaction::{
|
||||
Recurrence, RecurrenceUnit, RecurringTransactionData, WeekendAdjustment,
|
||||
@@ -127,7 +127,7 @@ mod tests {
|
||||
ValidatedRecurringTransaction::try_from(transaction(i64::MAX as u64 + 1)).unwrap_err();
|
||||
assert!(matches!(
|
||||
error,
|
||||
ScheduledTransactionValidationError::AmountCents(AmountCentsConversionError(value))
|
||||
ScheduledTransactionValidationError::Cents(CentsConversionError(value))
|
||||
if value == i64::MAX as i128 + 1
|
||||
));
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
use crate::routes::dtos::{
|
||||
amount_cents::AmountCentsConversionError, direction::Direction, transaction::TransactionData,
|
||||
cents::CentsConversionError, direction::Direction, transaction::TransactionData,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -28,7 +28,7 @@ impl TryFrom<TransactionData> for ValidatedTransaction {
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum TransactionValidationError {
|
||||
#[error(transparent)]
|
||||
AmountCents(#[from] AmountCentsConversionError),
|
||||
Cents(#[from] CentsConversionError),
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -37,8 +37,7 @@ mod tests {
|
||||
|
||||
use super::{TransactionValidationError, ValidatedTransaction};
|
||||
use crate::routes::dtos::{
|
||||
amount_cents::AmountCentsConversionError, direction::Direction,
|
||||
transaction::TransactionData,
|
||||
cents::CentsConversionError, direction::Direction, transaction::TransactionData,
|
||||
};
|
||||
|
||||
fn transaction(amount_cents: u64) -> TransactionData {
|
||||
@@ -67,7 +66,7 @@ mod tests {
|
||||
);
|
||||
assert!(matches!(
|
||||
error,
|
||||
TransactionValidationError::AmountCents(AmountCentsConversionError(value))
|
||||
TransactionValidationError::Cents(CentsConversionError(value))
|
||||
if value == i64::MAX as i128 + 1
|
||||
));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user