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
use std::{any::type_name, collections::BTreeSet, fmt::Formatter};
use serde::{
de::{MapAccess, Visitor},
Deserialize, Deserializer, Serialize,
};
use serde_types::OneOrMany;
use xcell_errors::{
for_3rd::{read_map_next_extra, read_map_next_value, DataType},
XResult,
};
use crate::utils::{syntax_error, type_mismatch};
use super::*;
mod der;
#[derive(Debug, Clone, Serialize)]
pub struct BooleanDescription {
pub accept: BTreeSet<String>,
pub reject: BTreeSet<String>,
pub default: bool,
}
impl From<BooleanDescription> for XCellTyped {
fn from(value: BooleanDescription) -> Self {
Self::Boolean(Box::new(value))
}
}
impl BooleanDescription {
pub fn new(default: bool) -> BooleanDescription {
Self { default, ..Self::default() }
}
pub fn parse_cell(&self, cell: &DataType) -> XResult<XCellValue> {
self.parse_value(cell).map(XCellValue::Boolean)
}
fn parse_value(&self, cell: &DataType) -> XResult<bool> {
match cell {
DataType::String(s) => {
if self.accept.contains(s.to_ascii_lowercase().trim()) {
Ok(true)
}
else if self.reject.contains(s.to_ascii_lowercase().trim()) {
Ok(false)
}
else {
syntax_error(format!("{} 无法解析为 bool 类型", s))
}
}
DataType::Bool(v) => Ok(*v),
DataType::Empty => Ok(self.default),
DataType::Error(e) => syntax_error(format!("未知错误 {e}")),
_ => type_mismatch("Boolean", cell),
}
}
}