Skip to main content

appletheia_application/outbox/command/
command_outbox_id.rs

1use std::{fmt, fmt::Display};
2
3use uuid::{Uuid, Version};
4
5use super::CommandOutboxIdError;
6
7#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
8pub struct CommandOutboxId(Uuid);
9
10impl CommandOutboxId {
11    pub fn new() -> Self {
12        Self(Uuid::now_v7())
13    }
14
15    pub fn value(&self) -> Uuid {
16        self.0
17    }
18}
19
20impl Default for CommandOutboxId {
21    fn default() -> Self {
22        Self::new()
23    }
24}
25
26impl TryFrom<Uuid> for CommandOutboxId {
27    type Error = CommandOutboxIdError;
28
29    fn try_from(value: Uuid) -> Result<Self, Self::Error> {
30        match value.get_version() {
31            Some(Version::SortRand) => Ok(Self(value)),
32            _ => Err(CommandOutboxIdError::NotUuidV7(value)),
33        }
34    }
35}
36
37impl From<CommandOutboxId> for Uuid {
38    fn from(value: CommandOutboxId) -> Self {
39        value.value()
40    }
41}
42
43impl Display for CommandOutboxId {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        write!(f, "{}", self.value())
46    }
47}