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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use crate::config::ProjectConfig;
use super::*;
mod display;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct XTable {
pub path: PathBuf,
pub name: String,
pub config: TableConfig,
pub data: XTableKind,
pub enumeration_linked: bool,
pub output_generated: bool,
pub sum_excel: u64,
pub sum_config: u64,
}
impl XTable {
pub fn is_enumerate(&self) -> bool {
matches!(self.data, XTableKind::Enumerate(_))
}
}
impl XTable {
pub fn load_file(excel: &Path, global: &ProjectConfig) -> XResult<Self> {
let mut xcell = Self {
path: excel.canonicalize()?,
name: "".to_string(),
data: Default::default(),
config: Default::default(),
sum_excel: 0,
sum_config: 0,
enumeration_linked: false,
output_generated: false,
};
xcell.try_set_name()?;
xcell.try_load_header(global)?;
xcell.try_load_config(global)?;
if xcell.check_sum_change() {
match xcell.reload_data() {
Ok(_) => {}
Err(e) => {
log::error!("{e}")
}
}
}
Ok(xcell)
}
fn try_set_name(&mut self) -> XResult<()> {
let _: Option<()> = try {
let file = self.path.file_prefix()?.to_str()?;
if !file.is_empty() {
self.name = file.to_string();
return Ok(());
}
};
Err(XError::table_error(format!("配置表文件名不合法: {}", self.path.display())))
}
fn try_load_header(&mut self, global: &ProjectConfig) -> XResult<()> {
let err: XResult<()> = try {
let table = find_first_table(&self.path)?;
self.data.read_table_headers(&table, global);
};
err.map_err(|e| e.with_path(&self.path))
}
fn try_load_config(&mut self, global: &ProjectConfig) -> XResult<()> {
let file = self.path.with_extension("toml");
let file = if file.exists() { Some(file.as_path()) } else { None };
self.config = TableConfig::load_file(file, Some(global))?;
Ok(())
}
pub fn reload_data(&mut self) -> XResult<()> {
let table = find_first_table(&self.path)?;
self.data.read_table_data(&table, &self.path, &self.config.typing);
Ok(())
}
pub fn id(&self) -> u64 {
xx_hash(self)
}
pub fn check_sum_change(&mut self) -> bool {
self.check_excel_change() || self.check_config_change()
}
pub fn check_excel_change(&mut self) -> bool {
let sum = match xx_file(&self.path) {
Ok(o) => o,
Err(_) => return false,
};
let changed = self.sum_excel != sum;
if changed {
self.sum_excel = sum;
}
changed
}
pub fn check_config_change(&mut self) -> bool {
let sum = 0;
let changed = self.sum_excel != sum;
if changed {
self.sum_excel = sum;
}
changed
}
}
impl XTable {
pub fn on_config_change(&mut self, global: &ProjectConfig) -> XResult<()> {
self.try_load_header(global)?;
self.try_load_config(global)?;
self.on_excel_change()?;
Ok(())
}
pub fn on_excel_change(&mut self) -> XResult<()> {
self.reload_data()?;
Ok(())
}
}