Skip to main content

wp_data_fmt/
formatter.rs

1use wp_model_core::model::{DataRecord, FieldStorage, Value, types::value::ObjectValue};
2
3use crate::{FormatType, SqlFormat};
4
5// ============================================================================
6// 新设计:简化统一的 Formatter trait
7// ============================================================================
8
9/// 核心 trait:值格式化器
10///
11/// 只需实现一个方法来格式化任意 Value。这是最核心、最简单的接口。
12pub trait ValueFormatter {
13    type Output;
14
15    /// 格式化任意值
16    fn format_value(&self, value: &Value) -> Self::Output;
17}
18
19/// 扩展 trait:记录格式化器
20///
21/// 在 ValueFormatter 基础上,提供格式化字段和记录的能力。
22/// 大部分实现都可以使用默认实现。
23pub trait RecordFormatter: ValueFormatter {
24    /// 格式化单个字段
25    ///
26    /// 默认实现:只格式化字段的值,忽略字段名
27    fn fmt_field(&self, field: &FieldStorage) -> Self::Output {
28        self.format_value(field.get_value())
29    }
30
31    /// 格式化整条记录
32    ///
33    /// 这个方法需要具体实现,因为不同格式对记录的表示方式差异很大:
34    /// - JSON: `{"name":"Alice","age":30}`
35    /// - CSV: `Alice,30`
36    /// - KV: `name: "Alice", age: 30`
37    fn fmt_record(&self, record: &DataRecord) -> Self::Output;
38}
39
40// ============================================================================
41// 旧设计:保持向后兼容(标记为 deprecated)
42// ============================================================================
43
44/// 旧的 DataFormat trait
45///
46/// **已弃用**: 请使用 `ValueFormatter` 和 `RecordFormatter` 替代。
47///
48/// 这个 trait 将在下一个主要版本中移除。
49#[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; // ← 改为 FieldStorage
66
67    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), // v 现在是 &Vec<FieldStorage>
78            _ => 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/// 旧的 StaticDataFormatter trait
87///
88/// **已弃用**: 请使用 `ValueFormatter` 替代(使用实例而不是静态方法)。
89///
90/// 这个 trait 将在下一个主要版本中移除。
91#[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; // ← 改为 FieldStorage
108
109    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), // v 现在是 &Vec<FieldStorage>
120            _ => 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
197// ============================================================================
198// 新 trait 实现:FormatType 和 SqlFormat
199// ============================================================================
200
201impl 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}