Skip to main content

appletheia_application/outbox/
outbox_relay_instance_id.rs

1use std::{fmt, fmt::Display};
2
3use super::OutboxRelayInstanceError;
4
5#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
6pub struct OutboxRelayInstanceId(String);
7
8impl OutboxRelayInstanceId {
9    pub fn new(value: String) -> Result<Self, OutboxRelayInstanceError> {
10        if value.is_empty() {
11            Err(OutboxRelayInstanceError::EmptyInstanceId)
12        } else {
13            Ok(Self(value))
14        }
15    }
16
17    pub fn value(&self) -> &str {
18        &self.0
19    }
20}
21
22impl Display for OutboxRelayInstanceId {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        write!(f, "{}", self.value())
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn new_rejects_empty_value() {
34        let err = OutboxRelayInstanceId::new(String::new()).expect_err("expected error");
35        matches!(err, OutboxRelayInstanceError::EmptyInstanceId);
36    }
37
38    #[test]
39    fn new_accepts_non_empty_value() {
40        let id = OutboxRelayInstanceId::new("instance-1".to_string()).unwrap();
41        assert_eq!(id.value(), "instance-1");
42    }
43}