Skip to main content

wp_data_fmt/
formatter.rs

1use wp_model_core::model::data::record::RecordItem;
2use wp_model_core::model::{DataRecord, FieldStorage, Value, types::value::ObjectValue};
3
4use crate::{FormatType, SqlFormat};
5
6// ============================================================================
7// 新设计:简化统一的 Formatter trait
8// ============================================================================
9
10/// 核心 trait:值格式化器
11///
12/// 只需实现一个方法来格式化任意 Value。这是最核心、最简单的接口。
13pub trait ValueFormatter {
14    type Output;
15
16    /// 格式化任意值
17    fn format_value(&self, value: &Value) -> Self::Output;
18}
19
20/// 扩展 trait:记录格式化器
21///
22/// 在 ValueFormatter 基础上,提供格式化字段和记录的能力。
23/// 大部分实现都可以使用默认实现。
24pub trait RecordFormatter: ValueFormatter {
25    /// 格式化单个字段
26    ///
27    /// 默认实现:只格式化字段的值,忽略字段名
28    fn fmt_field(&self, field: &FieldStorage) -> Self::Output {
29        self.format_value(field.get_value())
30    }
31
32    /// 格式化整条记录
33    ///
34    /// 这个方法需要具体实现,因为不同格式对记录的表示方式差异很大:
35    /// - JSON: `{"name":"Alice","age":30}`
36    /// - CSV: `Alice,30`
37    /// - KV: `name: "Alice", age: 30`
38    fn fmt_record(&self, record: &DataRecord) -> Self::Output;
39}
40
41// ============================================================================
42// 旧设计:保持向后兼容(标记为 deprecated)
43// ============================================================================
44
45/// 旧的 DataFormat trait
46///
47/// **已弃用**: 请使用 `ValueFormatter` 和 `RecordFormatter` 替代。
48///
49/// 这个 trait 将在下一个主要版本中移除。
50#[deprecated(
51    since = "0.2.0",
52    note = "Use ValueFormatter and RecordFormatter instead. This trait will be removed in the next major version."
53)]
54pub trait DataFormat {
55    type Output;
56
57    fn format_null(&self) -> Self::Output;
58    fn format_bool(&self, value: &bool) -> Self::Output;
59    fn format_string(&self, value: &str) -> Self::Output;
60    fn format_i64(&self, value: &i64) -> Self::Output;
61    fn format_f64(&self, value: &f64) -> Self::Output;
62    fn format_ip(&self, value: &std::net::IpAddr) -> Self::Output;
63    fn format_datetime(&self, value: &chrono::NaiveDateTime) -> Self::Output;
64
65    fn format_object(&self, value: &ObjectValue) -> Self::Output;
66    fn format_array(&self, value: &[FieldStorage]) -> Self::Output; // ← 改为 FieldStorage
67
68    fn fmt_value(&self, value: &Value) -> Self::Output {
69        match value {
70            Value::Null => self.format_null(),
71            Value::Bool(v) => self.format_bool(v),
72            Value::Chars(v) => self.format_string(v),
73            Value::Digit(v) => self.format_i64(v),
74            Value::Float(v) => self.format_f64(v),
75            Value::IpAddr(v) => self.format_ip(v),
76            Value::Time(v) => self.format_datetime(v),
77            Value::Obj(v) => self.format_object(v),
78            Value::Array(v) => self.format_array(v), // v 现在是 &Vec<FieldStorage>
79            _ => self.format_string(&value.to_string()),
80        }
81    }
82
83    fn format_field(&self, field: &FieldStorage) -> Self::Output;
84    fn format_record(&self, record: &DataRecord) -> Self::Output;
85}
86
87/// 旧的 StaticDataFormatter trait
88///
89/// **已弃用**: 请使用 `ValueFormatter` 替代(使用实例而不是静态方法)。
90///
91/// 这个 trait 将在下一个主要版本中移除。
92#[deprecated(
93    since = "0.2.0",
94    note = "Use ValueFormatter instead with instance methods. This trait will be removed in the next major version."
95)]
96pub trait StaticDataFormatter {
97    type Output;
98
99    fn stdfmt_null() -> Self::Output;
100    fn stdfmt_bool(value: &bool) -> Self::Output;
101    fn stdfmt_string(value: &str) -> Self::Output;
102    fn stdfmt_i64(value: &i64) -> Self::Output;
103    fn stdfmt_f64(value: &f64) -> Self::Output;
104    fn stdfmt_ip_addr(value: &std::net::IpAddr) -> Self::Output;
105    fn stdfmt_datetime(value: &chrono::NaiveDateTime) -> Self::Output;
106
107    fn stdfmt_object(value: &ObjectValue) -> Self::Output;
108    fn stdfmt_array(value: &[FieldStorage]) -> Self::Output; // ← 改为 FieldStorage
109
110    fn stdfmt_value(value: &Value) -> Self::Output {
111        match value {
112            Value::Null => Self::stdfmt_null(),
113            Value::Bool(v) => Self::stdfmt_bool(v),
114            Value::Chars(v) => Self::stdfmt_string(v),
115            Value::Digit(v) => Self::stdfmt_i64(v),
116            Value::Float(v) => Self::stdfmt_f64(v),
117            Value::IpAddr(v) => Self::stdfmt_ip_addr(v),
118            Value::Time(v) => Self::stdfmt_datetime(v),
119            Value::Obj(v) => Self::stdfmt_object(v),
120            Value::Array(v) => Self::stdfmt_array(v), // v 现在是 &Vec<FieldStorage>
121            _ => Self::stdfmt_string(&value.to_string()),
122        }
123    }
124
125    fn stdfmt_field(field: &FieldStorage) -> Self::Output;
126    fn stdfmt_record(record: &DataRecord) -> Self::Output;
127}
128
129#[allow(deprecated)]
130trait AsDataFormatter {
131    fn as_formatter(&self) -> &dyn DataFormat<Output = String>;
132}
133
134#[allow(deprecated)]
135impl AsDataFormatter for FormatType {
136    fn as_formatter(&self) -> &dyn DataFormat<Output = String> {
137        match self {
138            FormatType::Csv(f) => f,
139            FormatType::Json(f) => f,
140            FormatType::Kv(f) => f,
141            FormatType::Sql(f) => f,
142            FormatType::Raw(f) => f,
143            FormatType::ProtoText(f) => f,
144        }
145    }
146}
147
148#[allow(deprecated)]
149impl AsDataFormatter for SqlFormat {
150    fn as_formatter(&self) -> &dyn DataFormat<Output = String> {
151        match self {
152            SqlFormat::Json(f) => f,
153            SqlFormat::Kv(f) => f,
154            SqlFormat::Raw(f) => f,
155            SqlFormat::ProtoText(f) => f,
156        }
157    }
158}
159
160#[allow(deprecated)]
161impl DataFormat for FormatType {
162    type Output = String;
163    fn format_null(&self) -> Self::Output {
164        self.as_formatter().format_null()
165    }
166    fn format_bool(&self, value: &bool) -> Self::Output {
167        self.as_formatter().format_bool(value)
168    }
169    fn format_string(&self, value: &str) -> Self::Output {
170        self.as_formatter().format_string(value)
171    }
172    fn format_i64(&self, value: &i64) -> Self::Output {
173        self.as_formatter().format_i64(value)
174    }
175    fn format_f64(&self, value: &f64) -> Self::Output {
176        self.as_formatter().format_f64(value)
177    }
178    fn format_ip(&self, value: &std::net::IpAddr) -> Self::Output {
179        self.as_formatter().format_ip(value)
180    }
181    fn format_datetime(&self, value: &chrono::NaiveDateTime) -> Self::Output {
182        self.as_formatter().format_datetime(value)
183    }
184    fn format_object(&self, value: &ObjectValue) -> Self::Output {
185        self.as_formatter().format_object(value)
186    }
187    fn format_array(&self, value: &[FieldStorage]) -> Self::Output {
188        self.as_formatter().format_array(value)
189    }
190    fn format_field(&self, field: &FieldStorage) -> Self::Output {
191        self.as_formatter().format_field(field)
192    }
193    fn format_record(&self, record: &DataRecord) -> Self::Output {
194        self.as_formatter().format_record(record)
195    }
196}
197
198// ============================================================================
199// 新 trait 实现:FormatType 和 SqlFormat
200// ============================================================================
201
202impl ValueFormatter for FormatType {
203    type Output = String;
204
205    fn format_value(&self, value: &Value) -> String {
206        match self {
207            FormatType::Csv(f) => f.format_value(value),
208            FormatType::Json(f) => f.format_value(value),
209            FormatType::Kv(f) => f.format_value(value),
210            FormatType::Sql(f) => f.format_value(value),
211            FormatType::Raw(f) => f.format_value(value),
212            FormatType::ProtoText(f) => f.format_value(value),
213        }
214    }
215}
216
217impl RecordFormatter for FormatType {
218    fn fmt_field(&self, field: &FieldStorage) -> String {
219        match self {
220            FormatType::Csv(f) => f.fmt_field(field),
221            FormatType::Json(f) => f.fmt_field(field),
222            FormatType::Kv(f) => f.fmt_field(field),
223            FormatType::Sql(f) => f.fmt_field(field),
224            FormatType::Raw(f) => f.fmt_field(field),
225            FormatType::ProtoText(f) => f.fmt_field(field),
226        }
227    }
228
229    fn fmt_record(&self, record: &DataRecord) -> String {
230        match self {
231            FormatType::Csv(f) => f.fmt_record(record),
232            FormatType::Json(f) => f.fmt_record(record),
233            FormatType::Kv(f) => f.fmt_record(record),
234            FormatType::Sql(f) => f.fmt_record(record),
235            FormatType::Raw(f) => f.fmt_record(record),
236            FormatType::ProtoText(f) => f.fmt_record(record),
237        }
238    }
239}
240
241impl ValueFormatter for SqlFormat {
242    type Output = String;
243
244    fn format_value(&self, value: &Value) -> String {
245        match self {
246            SqlFormat::Json(f) => f.format_value(value),
247            SqlFormat::Kv(f) => f.format_value(value),
248            SqlFormat::Raw(f) => f.format_value(value),
249            SqlFormat::ProtoText(f) => f.format_value(value),
250        }
251    }
252}
253
254impl RecordFormatter for SqlFormat {
255    fn fmt_field(&self, field: &FieldStorage) -> String {
256        match self {
257            SqlFormat::Json(f) => f.fmt_field(field),
258            SqlFormat::Kv(f) => f.fmt_field(field),
259            SqlFormat::Raw(f) => f.fmt_field(field),
260            SqlFormat::ProtoText(f) => f.fmt_field(field),
261        }
262    }
263
264    fn fmt_record(&self, record: &DataRecord) -> String {
265        match self {
266            SqlFormat::Json(f) => f.fmt_record(record),
267            SqlFormat::Kv(f) => f.fmt_record(record),
268            SqlFormat::Raw(f) => f.fmt_record(record),
269            SqlFormat::ProtoText(f) => f.fmt_record(record),
270        }
271    }
272}