phlow_sdk/
id.rs

1use serde::Serialize;
2use std::fmt::Display;
3use valu3::{traits::ToValueBehavior, value::Value};
4
5#[derive(Debug, Clone, PartialEq, Serialize, Eq, Hash)]
6pub struct ID(Option<String>);
7
8impl ID {
9    pub fn new() -> Self {
10        Self(None)
11    }
12
13    pub fn is_some(&self) -> bool {
14        self.0.is_some()
15    }
16}
17
18impl ToValueBehavior for ID {
19    fn to_value(&self) -> Value {
20        self.0.to_value()
21    }
22}
23
24impl From<String> for ID {
25    fn from(id: String) -> Self {
26        Self(Some(id))
27    }
28}
29
30impl From<&Value> for ID {
31    fn from(value: &Value) -> Self {
32        Self::from(value.to_string())
33    }
34}
35
36impl From<Value> for ID {
37    fn from(value: Value) -> Self {
38        Self::from(value.to_string())
39    }
40}
41
42impl From<&String> for ID {
43    fn from(id: &String) -> Self {
44        Self::from(id.to_string())
45    }
46}
47
48impl From<&str> for ID {
49    fn from(id: &str) -> Self {
50        Self::from(id.to_string())
51    }
52}
53
54impl Display for ID {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        write!(
57            f,
58            "{}",
59            if self.0.is_some() {
60                match self.0.as_ref() {
61                    Some(id) => id,
62                    None => "unknown",
63                }
64            } else {
65                "unknown"
66            }
67        )
68    }
69}
70
71impl Default for ID {
72    fn default() -> Self {
73        Self::new()
74    }
75}