Implemented a domain layer, because the API DTO's where getting bloated.

This commit is contained in:
2026-09-26 17:22:10 +02:00
parent 04debfba7b
commit 4737bd5c81
24 changed files with 1103 additions and 538 deletions
+96
View File
@@ -0,0 +1,96 @@
use super::DecodeError;
use crate::domain::{Direction, RecurrenceUnit, WeekendAdjustment};
pub fn encode_direction(direction: Direction) -> &'static str {
match direction {
Direction::In => "in",
Direction::Out => "out",
}
}
pub fn decode_direction(value: String) -> Result<Direction, DecodeError> {
match value.as_str() {
"in" => Ok(Direction::In),
"out" => Ok(Direction::Out),
_ => Err(DecodeError::Direction(value)),
}
}
pub fn encode_recurrence_unit(unit: RecurrenceUnit) -> &'static str {
match unit {
RecurrenceUnit::Day => "day",
RecurrenceUnit::Week => "week",
RecurrenceUnit::Month => "month",
RecurrenceUnit::Year => "year",
}
}
pub fn decode_recurrence_unit(value: String) -> Result<RecurrenceUnit, DecodeError> {
match value.as_str() {
"day" => Ok(RecurrenceUnit::Day),
"week" => Ok(RecurrenceUnit::Week),
"month" => Ok(RecurrenceUnit::Month),
"year" => Ok(RecurrenceUnit::Year),
_ => Err(DecodeError::RecurrenceUnit(value)),
}
}
pub fn encode_weekend_adjustment(adjustment: WeekendAdjustment) -> &'static str {
match adjustment {
WeekendAdjustment::PreviousBusinessDay => "previous_business_day",
WeekendAdjustment::NextBusinessDay => "next_business_day",
}
}
pub fn decode_weekend_adjustment(value: String) -> Result<WeekendAdjustment, DecodeError> {
match value.as_str() {
"previous_business_day" => Ok(WeekendAdjustment::PreviousBusinessDay),
"next_business_day" => Ok(WeekendAdjustment::NextBusinessDay),
_ => Err(DecodeError::WeekendAdjustment(value)),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trips_all_variants() {
for direction in [Direction::In, Direction::Out] {
let encoded = encode_direction(direction).to_owned();
assert_eq!(decode_direction(encoded).unwrap(), direction);
}
for unit in [
RecurrenceUnit::Day,
RecurrenceUnit::Week,
RecurrenceUnit::Month,
RecurrenceUnit::Year,
] {
let encoded = encode_recurrence_unit(unit).to_owned();
assert_eq!(decode_recurrence_unit(encoded).unwrap(), unit);
}
for adjustment in [
WeekendAdjustment::PreviousBusinessDay,
WeekendAdjustment::NextBusinessDay,
] {
let encoded = encode_weekend_adjustment(adjustment).to_owned();
assert_eq!(decode_weekend_adjustment(encoded).unwrap(), adjustment);
}
}
#[test]
fn rejects_unknown_values() {
assert!(matches!(
decode_direction("sideways".to_owned()),
Err(DecodeError::Direction(value)) if value == "sideways"
));
assert!(matches!(
decode_recurrence_unit("hour".to_owned()),
Err(DecodeError::RecurrenceUnit(value)) if value == "hour"
));
assert!(matches!(
decode_weekend_adjustment("never".to_owned()),
Err(DecodeError::WeekendAdjustment(value)) if value == "never"
));
}
}