datasynth_core/models/
graph_properties.rs1use chrono::NaiveDate;
8use rust_decimal::Decimal;
9use std::collections::HashMap;
10
11#[derive(Debug, Clone, PartialEq)]
16pub enum GraphPropertyValue {
17 String(String),
18 Int(i64),
19 Float(f64),
20 Decimal(Decimal),
21 Bool(bool),
22 Date(NaiveDate),
23 StringList(Vec<String>),
24}
25
26impl GraphPropertyValue {
27 pub fn to_string_value(&self) -> String {
29 match self {
30 Self::String(s) => s.clone(),
31 Self::Int(i) => i.to_string(),
32 Self::Float(f) => format!("{f:.6}"),
33 Self::Decimal(d) => d.to_string(),
34 Self::Bool(b) => b.to_string(),
35 Self::Date(d) => d.to_string(),
36 Self::StringList(v) => v.join(";"),
37 }
38 }
39
40 pub fn as_str(&self) -> Option<&str> {
42 match self {
43 Self::String(s) => Some(s),
44 _ => None,
45 }
46 }
47
48 pub fn as_bool(&self) -> Option<bool> {
50 match self {
51 Self::Bool(b) => Some(*b),
52 _ => None,
53 }
54 }
55
56 pub fn as_decimal(&self) -> Option<Decimal> {
58 match self {
59 Self::Decimal(d) => Some(*d),
60 _ => None,
61 }
62 }
63
64 pub fn as_int(&self) -> Option<i64> {
66 match self {
67 Self::Int(i) => Some(*i),
68 _ => None,
69 }
70 }
71
72 pub fn as_float(&self) -> Option<f64> {
74 match self {
75 Self::Float(f) => Some(*f),
76 _ => None,
77 }
78 }
79
80 pub fn as_date(&self) -> Option<NaiveDate> {
82 match self {
83 Self::Date(d) => Some(*d),
84 _ => None,
85 }
86 }
87}
88
89pub trait ToNodeProperties {
94 fn node_type_name(&self) -> &'static str;
96
97 fn node_type_code(&self) -> u16;
99
100 fn to_node_properties(&self) -> HashMap<String, GraphPropertyValue>;
102}
103
104#[cfg(test)]
105#[allow(clippy::unwrap_used, clippy::approx_constant)]
106mod tests {
107 use super::*;
108
109 #[test]
110 fn test_graph_property_value_to_string() {
111 assert_eq!(GraphPropertyValue::Bool(true).to_string_value(), "true");
112 assert_eq!(GraphPropertyValue::Bool(false).to_string_value(), "false");
113 assert_eq!(GraphPropertyValue::Int(42).to_string_value(), "42");
114 assert_eq!(GraphPropertyValue::Int(-7).to_string_value(), "-7");
115 assert_eq!(
116 GraphPropertyValue::String("hello".into()).to_string_value(),
117 "hello"
118 );
119 assert_eq!(
120 GraphPropertyValue::Float(3.14).to_string_value(),
121 "3.140000"
122 );
123 assert_eq!(
124 GraphPropertyValue::Decimal(Decimal::new(1234, 2)).to_string_value(),
125 "12.34"
126 );
127 assert_eq!(
128 GraphPropertyValue::Date(NaiveDate::from_ymd_opt(2024, 1, 15).unwrap())
129 .to_string_value(),
130 "2024-01-15"
131 );
132 assert_eq!(
133 GraphPropertyValue::StringList(vec!["a".into(), "b".into(), "c".into()])
134 .to_string_value(),
135 "a;b;c"
136 );
137 }
138
139 #[test]
140 fn test_accessor_methods() {
141 assert_eq!(
142 GraphPropertyValue::String("test".into()).as_str(),
143 Some("test")
144 );
145 assert_eq!(GraphPropertyValue::Int(42).as_str(), None);
146 assert_eq!(GraphPropertyValue::Bool(true).as_bool(), Some(true));
147 assert_eq!(GraphPropertyValue::String("x".into()).as_bool(), None);
148 assert_eq!(
149 GraphPropertyValue::Decimal(Decimal::new(100, 0)).as_decimal(),
150 Some(Decimal::new(100, 0))
151 );
152 assert_eq!(GraphPropertyValue::Bool(true).as_decimal(), None);
153 assert_eq!(GraphPropertyValue::Int(99).as_int(), Some(99));
154 assert_eq!(GraphPropertyValue::Float(1.5).as_float(), Some(1.5));
155 let d = NaiveDate::from_ymd_opt(2024, 6, 1).unwrap();
156 assert_eq!(GraphPropertyValue::Date(d).as_date(), Some(d));
157 }
158
159 #[test]
160 fn test_empty_string_list() {
161 assert_eq!(GraphPropertyValue::StringList(vec![]).to_string_value(), "");
162 }
163}