Files
expenses/server/src/routes/dtos/direction.rs
T
2026-09-29 22:35:03 +02:00

65 lines
1.7 KiB
Rust

use serde::{Deserialize, Serialize};
use crate::domain;
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Direction {
In,
Out,
}
impl From<Direction> for domain::Direction {
fn from(direction: Direction) -> Self {
match direction {
Direction::In => Self::In,
Direction::Out => Self::Out,
}
}
}
impl From<domain::Direction> for Direction {
fn from(direction: domain::Direction) -> Self {
match direction {
domain::Direction::In => Self::In,
domain::Direction::Out => Self::Out,
}
}
}
#[cfg(test)]
mod tests {
use super::Direction;
use crate::domain;
#[test]
fn serializes_direction() {
assert_eq!(serde_json::to_value(Direction::In).unwrap(), "in");
assert_eq!(serde_json::to_value(Direction::Out).unwrap(), "out");
}
#[test]
fn deserializes_direction() {
let incoming: Direction = serde_json::from_value("in".into()).unwrap();
assert!(matches!(incoming, Direction::In));
let outgoing: Direction = serde_json::from_value("out".into()).unwrap();
assert!(matches!(outgoing, Direction::Out));
}
#[test]
fn converts_between_dto_and_domain() {
let domain_in: domain::Direction = Direction::In.into();
assert_eq!(domain_in, domain::Direction::In);
let domain_out: domain::Direction = Direction::Out.into();
assert_eq!(domain_out, domain::Direction::Out);
let dto_in: Direction = domain::Direction::In.into();
assert!(matches!(dto_in, Direction::In));
let dto_out: Direction = domain::Direction::Out.into();
assert!(matches!(dto_out, Direction::Out));
}
}