1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::{any::type_name, collections::BTreeSet, fmt::Formatter};
use serde::{
de::{MapAccess, Visitor},
Deserialize, Deserializer, Serialize,
};
use xcell_errors::{
for_3rd::{read_map_next_extra, read_map_next_value, DataType},
XResult,
};
use crate::{utils::syntax_error, XCellTyped, XCellValue};
mod der;
#[derive(Debug, Clone, Serialize)]
pub struct StringDescription {
patterns: BTreeSet<String>,
pub default: String,
}
impl From<StringDescription> for XCellTyped {
fn from(value: StringDescription) -> Self {
Self::String(Box::new(value))
}
}
impl StringDescription {
pub fn matches_type(&self, s: &str) -> bool {
for pattern in &self.patterns {
if s.eq_ignore_ascii_case(pattern) {
return true;
}
}
false
}
pub fn add_pattern(&mut self, s: impl Into<String>) {
self.patterns.insert(s.into());
}
pub fn mut_pattern(&mut self) -> &mut BTreeSet<String> {
&mut self.patterns
}
pub fn parse_cell(&self, cell: &DataType) -> XResult<XCellValue> {
self.parse_value(cell).map(XCellValue::String)
}
pub fn parse_value(&self, cell: &DataType) -> XResult<String> {
match cell {
DataType::Int(v) => Ok(v.to_string()),
DataType::Float(v) => Ok(v.to_string()),
DataType::String(v) => Ok(v.to_string()),
DataType::Bool(v) => Ok(v.to_string()),
DataType::DateTime(v) => Ok(v.to_string()),
DataType::Empty => Ok(self.default.clone()),
DataType::Error(e) => syntax_error(format!("未知错误 {e}")),
}
}
}