Renamed AmountCents to just Cents.

This commit is contained in:
2026-09-25 16:32:49 +02:00
parent e7062b31e4
commit cce06eb025
8 changed files with 83 additions and 84 deletions
-65
View File
@@ -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
));
}
}
+65
View File
@@ -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 -4
View File
@@ -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),