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