100 lines
3.2 KiB
Rust
100 lines
3.2 KiB
Rust
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();
|
|
let decoded = decode_direction(encoded.clone()).unwrap();
|
|
assert_eq!(decoded, direction);
|
|
}
|
|
for unit in [
|
|
RecurrenceUnit::Day,
|
|
RecurrenceUnit::Week,
|
|
RecurrenceUnit::Month,
|
|
RecurrenceUnit::Year,
|
|
] {
|
|
let encoded = encode_recurrence_unit(unit).to_owned();
|
|
let decoded = decode_recurrence_unit(encoded.clone()).unwrap();
|
|
assert_eq!(decoded, unit);
|
|
}
|
|
for adjustment in [
|
|
WeekendAdjustment::PreviousBusinessDay,
|
|
WeekendAdjustment::NextBusinessDay,
|
|
] {
|
|
let encoded = encode_weekend_adjustment(adjustment).to_owned();
|
|
let decoded = decode_weekend_adjustment(encoded.clone()).unwrap();
|
|
assert_eq!(decoded, 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"
|
|
));
|
|
}
|
|
}
|