1use wp_model_core::model::{DataRecord, FieldStorage, Value, types::value::ObjectValue};
2
3use crate::{FormatType, SqlFormat};
4
5pub trait ValueFormatter {
13 type Output;
14
15 fn format_value(&self, value: &Value) -> Self::Output;
17}
18
19pub trait RecordFormatter: ValueFormatter {
24 fn fmt_field(&self, field: &FieldStorage) -> Self::Output {
28 self.format_value(field.get_value())
29 }
30
31 fn fmt_record(&self, record: &DataRecord) -> Self::Output;
38}
39
40#[deprecated(
50 since = "0.2.0",
51 note = "Use ValueFormatter and RecordFormatter instead. This trait will be removed in the next major version."
52)]
53pub trait DataFormat {
54 type Output;
55
56 fn format_null(&self) -> Self::Output;
57 fn format_bool(&self, value: &bool) -> Self::Output;
58 fn format_string(&self, value: &str) -> Self::Output;
59 fn format_i64(&self, value: &i64) -> Self::Output;
60 fn format_f64(&self, value: &f64) -> Self::Output;
61 fn format_ip(&self, value: &std::net::IpAddr) -> Self::Output;
62 fn format_datetime(&self, value: &chrono::NaiveDateTime) -> Self::Output;
63
64 fn format_object(&self, value: &ObjectValue) -> Self::Output;
65 fn format_array(&self, value: &[FieldStorage]) -> Self::Output; fn fmt_value(&self, value: &Value) -> Self::Output {
68 match value {
69 Value::Null => self.format_null(),
70 Value::Bool(v) => self.format_bool(v),
71 Value::Chars(v) => self.format_string(v),
72 Value::Digit(v) => self.format_i64(v),
73 Value::Float(v) => self.format_f64(v),
74 Value::IpAddr(v) => self.format_ip(v),
75 Value::Time(v) => self.format_datetime(v),
76 Value::Obj(v) => self.format_object(v),
77 Value::Array(v) => self.format_array(v), _ => self.format_string(&value.to_string()),
79 }
80 }
81
82 fn format_field(&self, field: &FieldStorage) -> Self::Output;
83 fn format_record(&self, record: &DataRecord) -> Self::Output;
84}
85
86#[deprecated(
92 since = "0.2.0",
93 note = "Use ValueFormatter instead with instance methods. This trait will be removed in the next major version."
94)]
95pub trait StaticDataFormatter {
96 type Output;
97
98 fn stdfmt_null() -> Self::Output;
99 fn stdfmt_bool(value: &bool) -> Self::Output;
100 fn stdfmt_string(value: &str) -> Self::Output;
101 fn stdfmt_i64(value: &i64) -> Self::Output;
102 fn stdfmt_f64(value: &f64) -> Self::Output;
103 fn stdfmt_ip_addr(value: &std::net::IpAddr) -> Self::Output;
104 fn stdfmt_datetime(value: &chrono::NaiveDateTime) -> Self::Output;
105
106 fn stdfmt_object(value: &ObjectValue) -> Self::Output;
107 fn stdfmt_array(value: &[FieldStorage]) -> Self::Output; fn stdfmt_value(value: &Value) -> Self::Output {
110 match value {
111 Value::Null => Self::stdfmt_null(),
112 Value::Bool(v) => Self::stdfmt_bool(v),
113 Value::Chars(v) => Self::stdfmt_string(v),
114 Value::Digit(v) => Self::stdfmt_i64(v),
115 Value::Float(v) => Self::stdfmt_f64(v),
116 Value::IpAddr(v) => Self::stdfmt_ip_addr(v),
117 Value::Time(v) => Self::stdfmt_datetime(v),
118 Value::Obj(v) => Self::stdfmt_object(v),
119 Value::Array(v) => Self::stdfmt_array(v), _ => Self::stdfmt_string(&value.to_string()),
121 }
122 }
123
124 fn stdfmt_field(field: &FieldStorage) -> Self::Output;
125 fn stdfmt_record(record: &DataRecord) -> Self::Output;
126}
127
128#[allow(deprecated)]
129trait AsDataFormatter {
130 fn as_formatter(&self) -> &dyn DataFormat<Output = String>;
131}
132
133#[allow(deprecated)]
134impl AsDataFormatter for FormatType {
135 fn as_formatter(&self) -> &dyn DataFormat<Output = String> {
136 match self {
137 FormatType::Csv(f) => f,
138 FormatType::Json(f) => f,
139 FormatType::Kv(f) => f,
140 FormatType::Sql(f) => f,
141 FormatType::Raw(f) => f,
142 FormatType::ProtoText(f) => f,
143 }
144 }
145}
146
147#[allow(deprecated)]
148impl AsDataFormatter for SqlFormat {
149 fn as_formatter(&self) -> &dyn DataFormat<Output = String> {
150 match self {
151 SqlFormat::Json(f) => f,
152 SqlFormat::Kv(f) => f,
153 SqlFormat::Raw(f) => f,
154 SqlFormat::ProtoText(f) => f,
155 }
156 }
157}
158
159#[allow(deprecated)]
160impl DataFormat for FormatType {
161 type Output = String;
162 fn format_null(&self) -> Self::Output {
163 self.as_formatter().format_null()
164 }
165 fn format_bool(&self, value: &bool) -> Self::Output {
166 self.as_formatter().format_bool(value)
167 }
168 fn format_string(&self, value: &str) -> Self::Output {
169 self.as_formatter().format_string(value)
170 }
171 fn format_i64(&self, value: &i64) -> Self::Output {
172 self.as_formatter().format_i64(value)
173 }
174 fn format_f64(&self, value: &f64) -> Self::Output {
175 self.as_formatter().format_f64(value)
176 }
177 fn format_ip(&self, value: &std::net::IpAddr) -> Self::Output {
178 self.as_formatter().format_ip(value)
179 }
180 fn format_datetime(&self, value: &chrono::NaiveDateTime) -> Self::Output {
181 self.as_formatter().format_datetime(value)
182 }
183 fn format_object(&self, value: &ObjectValue) -> Self::Output {
184 self.as_formatter().format_object(value)
185 }
186 fn format_array(&self, value: &[FieldStorage]) -> Self::Output {
187 self.as_formatter().format_array(value)
188 }
189 fn format_field(&self, field: &FieldStorage) -> Self::Output {
190 self.as_formatter().format_field(field)
191 }
192 fn format_record(&self, record: &DataRecord) -> Self::Output {
193 self.as_formatter().format_record(record)
194 }
195}
196
197impl ValueFormatter for FormatType {
202 type Output = String;
203
204 fn format_value(&self, value: &Value) -> String {
205 match self {
206 FormatType::Csv(f) => f.format_value(value),
207 FormatType::Json(f) => f.format_value(value),
208 FormatType::Kv(f) => f.format_value(value),
209 FormatType::Sql(f) => f.format_value(value),
210 FormatType::Raw(f) => f.format_value(value),
211 FormatType::ProtoText(f) => f.format_value(value),
212 }
213 }
214}
215
216impl RecordFormatter for FormatType {
217 fn fmt_field(&self, field: &FieldStorage) -> String {
218 match self {
219 FormatType::Csv(f) => f.fmt_field(field),
220 FormatType::Json(f) => f.fmt_field(field),
221 FormatType::Kv(f) => f.fmt_field(field),
222 FormatType::Sql(f) => f.fmt_field(field),
223 FormatType::Raw(f) => f.fmt_field(field),
224 FormatType::ProtoText(f) => f.fmt_field(field),
225 }
226 }
227
228 fn fmt_record(&self, record: &DataRecord) -> String {
229 match self {
230 FormatType::Csv(f) => f.fmt_record(record),
231 FormatType::Json(f) => f.fmt_record(record),
232 FormatType::Kv(f) => f.fmt_record(record),
233 FormatType::Sql(f) => f.fmt_record(record),
234 FormatType::Raw(f) => f.fmt_record(record),
235 FormatType::ProtoText(f) => f.fmt_record(record),
236 }
237 }
238}
239
240impl ValueFormatter for SqlFormat {
241 type Output = String;
242
243 fn format_value(&self, value: &Value) -> String {
244 match self {
245 SqlFormat::Json(f) => f.format_value(value),
246 SqlFormat::Kv(f) => f.format_value(value),
247 SqlFormat::Raw(f) => f.format_value(value),
248 SqlFormat::ProtoText(f) => f.format_value(value),
249 }
250 }
251}
252
253impl RecordFormatter for SqlFormat {
254 fn fmt_field(&self, field: &FieldStorage) -> String {
255 match self {
256 SqlFormat::Json(f) => f.fmt_field(field),
257 SqlFormat::Kv(f) => f.fmt_field(field),
258 SqlFormat::Raw(f) => f.fmt_field(field),
259 SqlFormat::ProtoText(f) => f.fmt_field(field),
260 }
261 }
262
263 fn fmt_record(&self, record: &DataRecord) -> String {
264 match self {
265 SqlFormat::Json(f) => f.fmt_record(record),
266 SqlFormat::Kv(f) => f.fmt_record(record),
267 SqlFormat::Raw(f) => f.fmt_record(record),
268 SqlFormat::ProtoText(f) => f.fmt_record(record),
269 }
270 }
271}