wp_data_fmt/
formatter.rs

1use wp_model_core::model::{DataField, DataRecord, Value, types::value::ObjectValue};
2
3use crate::{FormatType, SqlFormat};
4
5pub trait DataFormat {
6    type Output;
7
8    fn format_null(&self) -> Self::Output;
9    fn format_bool(&self, value: &bool) -> Self::Output;
10    fn format_string(&self, value: &str) -> Self::Output;
11    fn format_i64(&self, value: &i64) -> Self::Output;
12    fn format_f64(&self, value: &f64) -> Self::Output;
13    fn format_ip(&self, value: &std::net::IpAddr) -> Self::Output;
14    fn format_datetime(&self, value: &chrono::NaiveDateTime) -> Self::Output;
15
16    fn format_object(&self, value: &ObjectValue) -> Self::Output;
17    fn format_array(&self, value: &[DataField]) -> Self::Output;
18
19    fn fmt_value(&self, value: &Value) -> Self::Output {
20        match value {
21            Value::Null => self.format_null(),
22            Value::Bool(v) => self.format_bool(v),
23            Value::Chars(v) => self.format_string(v),
24            Value::Digit(v) => self.format_i64(v),
25            Value::Float(v) => self.format_f64(v),
26            Value::IpAddr(v) => self.format_ip(v),
27            Value::Time(v) => self.format_datetime(v),
28            Value::Obj(v) => self.format_object(v),
29            Value::Array(v) => self.format_array(v),
30            _ => self.format_string(&value.to_string()),
31        }
32    }
33
34    fn format_field(&self, field: &DataField) -> Self::Output;
35    fn format_record(&self, record: &DataRecord) -> Self::Output;
36}
37
38pub trait StaticDataFormatter {
39    type Output;
40
41    fn stdfmt_null() -> Self::Output;
42    fn stdfmt_bool(value: &bool) -> Self::Output;
43    fn stdfmt_string(value: &str) -> Self::Output;
44    fn stdfmt_i64(value: &i64) -> Self::Output;
45    fn stdfmt_f64(value: &f64) -> Self::Output;
46    fn stdfmt_ip_addr(value: &std::net::IpAddr) -> Self::Output;
47    fn stdfmt_datetime(value: &chrono::NaiveDateTime) -> Self::Output;
48
49    fn stdfmt_object(value: &ObjectValue) -> Self::Output;
50    fn stdfmt_array(value: &[DataField]) -> Self::Output;
51
52    fn stdfmt_value(value: &Value) -> Self::Output {
53        match value {
54            Value::Null => Self::stdfmt_null(),
55            Value::Bool(v) => Self::stdfmt_bool(v),
56            Value::Chars(v) => Self::stdfmt_string(v),
57            Value::Digit(v) => Self::stdfmt_i64(v),
58            Value::Float(v) => Self::stdfmt_f64(v),
59            Value::IpAddr(v) => Self::stdfmt_ip_addr(v),
60            Value::Time(v) => Self::stdfmt_datetime(v),
61            Value::Obj(v) => Self::stdfmt_object(v),
62            Value::Array(v) => Self::stdfmt_array(v),
63            _ => Self::stdfmt_string(&value.to_string()),
64        }
65    }
66
67    fn stdfmt_field(field: &DataField) -> Self::Output;
68    fn stdfmt_record(record: &DataRecord) -> Self::Output;
69}
70
71trait AsDataFormatter {
72    fn as_formatter(&self) -> &dyn DataFormat<Output = String>;
73}
74impl AsDataFormatter for FormatType {
75    fn as_formatter(&self) -> &dyn DataFormat<Output = String> {
76        match self {
77            FormatType::Csv(f) => f,
78            FormatType::Json(f) => f,
79            FormatType::Kv(f) => f,
80            FormatType::Sql(f) => f,
81            FormatType::Raw(f) => f,
82            FormatType::ProtoText(f) => f,
83        }
84    }
85}
86
87impl AsDataFormatter for SqlFormat {
88    fn as_formatter(&self) -> &dyn DataFormat<Output = String> {
89        match self {
90            SqlFormat::Json(f) => f,
91            SqlFormat::Kv(f) => f,
92            SqlFormat::Raw(f) => f,
93            SqlFormat::ProtoText(f) => f,
94        }
95    }
96}
97impl DataFormat for FormatType {
98    type Output = String;
99    fn format_null(&self) -> Self::Output {
100        self.as_formatter().format_null()
101    }
102    fn format_bool(&self, value: &bool) -> Self::Output {
103        self.as_formatter().format_bool(value)
104    }
105    fn format_string(&self, value: &str) -> Self::Output {
106        self.as_formatter().format_string(value)
107    }
108    fn format_i64(&self, value: &i64) -> Self::Output {
109        self.as_formatter().format_i64(value)
110    }
111    fn format_f64(&self, value: &f64) -> Self::Output {
112        self.as_formatter().format_f64(value)
113    }
114    fn format_ip(&self, value: &std::net::IpAddr) -> Self::Output {
115        self.as_formatter().format_ip(value)
116    }
117    fn format_datetime(&self, value: &chrono::NaiveDateTime) -> Self::Output {
118        self.as_formatter().format_datetime(value)
119    }
120    fn format_object(&self, value: &ObjectValue) -> Self::Output {
121        self.as_formatter().format_object(value)
122    }
123    fn format_array(&self, value: &[DataField]) -> Self::Output {
124        self.as_formatter().format_array(value)
125    }
126    fn format_field(&self, field: &DataField) -> Self::Output {
127        self.as_formatter().format_field(field)
128    }
129    fn format_record(&self, record: &DataRecord) -> Self::Output {
130        self.as_formatter().format_record(record)
131    }
132}