dtz_identifier/
execution_id.rs1use std::fmt::Display;
2use uuid::Uuid;
3
4static PREFIX: &str = "execution-";
5
6#[derive(Debug, Clone, PartialEq, Copy)]
7pub struct ExecutionId {
8 pub id: Uuid,
9}
10
11impl Default for ExecutionId {
12 fn default() -> Self {
13 Self { id: Uuid::now_v7() }
14 }
15}
16
17impl Display for ExecutionId {
18 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 f.write_str(&format!("{PREFIX}{}", self.id))
20 }
21}
22
23impl<'de> serde::Deserialize<'de> for ExecutionId {
24 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
25 where
26 D: serde::Deserializer<'de>,
27 {
28 struct ExecutionIdVisitor;
29
30 impl serde::de::Visitor<'_> for ExecutionIdVisitor {
31 type Value = ExecutionId;
32
33 fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
34 formatter.write_str("a string starting with '")?;
35 formatter.write_str(PREFIX)?;
36 formatter.write_str("' followed by a UUID")
37 }
38
39 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
40 where
41 E: serde::de::Error,
42 {
43 if let Some(uuid_str) = value.strip_prefix(PREFIX) {
44 let uuid = Uuid::parse_str(uuid_str).map_err(E::custom)?;
45 Ok(ExecutionId { id: uuid })
46 } else {
47 Err(E::custom("invalid format"))
48 }
49 }
50 }
51
52 deserializer.deserialize_str(ExecutionIdVisitor)
53 }
54}
55
56impl serde::Serialize for ExecutionId {
57 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
58 where
59 S: serde::Serializer,
60 {
61 serializer.serialize_str(&self.to_string())
62 }
63}
64
65impl From<Uuid> for ExecutionId {
66 fn from(id: Uuid) -> Self {
67 ExecutionId { id }
68 }
69}