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
59
60
61
62
use crate::{utils::syntax_error, IntegerKind, XCellTyped, XCellValue};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use xcell_errors::{
for_3rd::{BigInt, DataType},
XResult,
};
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct EnumerateDescription {
pub integer: IntegerKind,
pub typing: String,
pub default: String,
pub mapping: BTreeMap<String, BigInt>,
}
impl From<EnumerateDescription> for XCellTyped {
fn from(value: EnumerateDescription) -> Self {
Self::Enumerate(Box::new(value))
}
}
impl EnumerateDescription {
pub fn new<S>(typing: S) -> Self
where
S: Into<String>,
{
Self { integer: Default::default(), typing: typing.into(), default: "".to_string(), mapping: Default::default() }
}
pub fn parse_cell(&self, cell: &DataType) -> XResult<XCellValue> {
self.parse_value(cell).map(XCellValue::Enumerate)
}
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}")),
}
}
}
impl XCellTyped {
pub fn as_enumerate(&self) -> Option<&EnumerateDescription> {
match self {
XCellTyped::Enumerate(e) => Some(e),
_ => None,
}
}
pub fn mut_enumerate(&mut self) -> Option<&mut EnumerateDescription> {
match self {
XCellTyped::Enumerate(e) => Some(e),
_ => None,
}
}
pub fn is_enumerate(&self) -> bool {
self.as_enumerate().is_some()
}
}