yufmath 0.1.1

A Rust CAS Lib.
Documentation
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! # 笔记本单元格
//!
//! 定义笔记本中的单元格数据结构和相关操作。

use crate::core::Expression;
use crate::formatter::{FormatType};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::SystemTime;
use uuid::Uuid;

/// 单元格唯一标识符
pub type CellId = Uuid;

/// 单元格类型
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum CellType {
    /// 代码单元格 - 包含可执行的数学表达式
    Code,
    /// 文本单元格 - 包含纯文本内容
    Text,
    /// Markdown 单元格 - 包含 Markdown 格式的文档
    Markdown,
    /// 输出单元格 - 显示计算结果(只读)
    Output,
}

impl CellType {
    /// 获取单元格类型的显示名称
    pub fn display_name(&self) -> &'static str {
        match self {
            CellType::Code => "代码",
            CellType::Text => "文本",
            CellType::Markdown => "Markdown",
            CellType::Output => "输出",
        }
    }
    
    /// 检查单元格类型是否可执行
    pub fn is_executable(&self) -> bool {
        matches!(self, CellType::Code)
    }
    
    /// 检查单元格类型是否可编辑
    pub fn is_editable(&self) -> bool {
        !matches!(self, CellType::Output)
    }
}

/// 单元格内容
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum CellContent {
    /// 原始文本内容
    Text(String),
    /// 解析后的表达式(仅用于代码单元格)
    Expression(Expression),
    /// 执行结果(仅用于输出单元格)
    Result {
        value: String,
        format: FormatType,
        execution_time: Option<std::time::Duration>,
    },
}

impl CellContent {
    /// 获取内容的文本表示
    pub fn as_text(&self) -> String {
        match self {
            CellContent::Text(text) => text.clone(),
            CellContent::Expression(expr) => format!("{:?}", expr),
            CellContent::Result { value, .. } => value.clone(),
        }
    }
    
    /// 检查内容是否为空
    pub fn is_empty(&self) -> bool {
        match self {
            CellContent::Text(text) => text.trim().is_empty(),
            CellContent::Expression(_) => false,
            CellContent::Result { value, .. } => value.trim().is_empty(),
        }
    }
    
    /// 获取内容长度
    pub fn len(&self) -> usize {
        self.as_text().len()
    }
}

/// 单元格元数据
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CellMetadata {
    /// 创建时间
    pub created_at: SystemTime,
    /// 最后修改时间
    pub modified_at: SystemTime,
    /// 最后执行时间
    pub last_executed: Option<SystemTime>,
    /// 执行次数
    pub execution_count: u64,
    /// 是否已修改(需要重新执行)
    pub is_dirty: bool,
    /// 自定义标签
    pub tags: Vec<String>,
    /// 自定义属性
    pub properties: HashMap<String, String>,
}

impl Default for CellMetadata {
    fn default() -> Self {
        let now = SystemTime::now();
        Self {
            created_at: now,
            modified_at: now,
            last_executed: None,
            execution_count: 0,
            is_dirty: false,
            tags: Vec::new(),
            properties: HashMap::new(),
        }
    }
}

impl CellMetadata {
    /// 标记单元格为已修改
    pub fn mark_dirty(&mut self) {
        self.is_dirty = true;
        self.modified_at = SystemTime::now();
    }
    
    /// 标记单元格为已执行
    pub fn mark_executed(&mut self) {
        self.is_dirty = false;
        self.last_executed = Some(SystemTime::now());
        self.execution_count += 1;
    }
    
    /// 添加标签
    pub fn add_tag(&mut self, tag: String) {
        if !self.tags.contains(&tag) {
            self.tags.push(tag);
            self.mark_dirty();
        }
    }
    
    /// 移除标签
    pub fn remove_tag(&mut self, tag: &str) {
        if let Some(pos) = self.tags.iter().position(|t| t == tag) {
            self.tags.remove(pos);
            self.mark_dirty();
        }
    }
    
    /// 设置属性
    pub fn set_property(&mut self, key: String, value: String) {
        self.properties.insert(key, value);
        self.mark_dirty();
    }
    
    /// 获取属性
    pub fn get_property(&self, key: &str) -> Option<&String> {
        self.properties.get(key)
    }
}

/// 笔记本单元格
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NotebookCell {
    /// 单元格唯一标识符
    pub id: CellId,
    /// 单元格类型
    pub cell_type: CellType,
    /// 单元格内容
    pub content: CellContent,
    /// 单元格元数据
    pub metadata: CellMetadata,
    /// 输出单元格(如果有)
    pub output: Option<Box<NotebookCell>>,
}

impl NotebookCell {
    /// 创建新的代码单元格
    pub fn new_code(content: String) -> Self {
        Self {
            id: Uuid::new_v4(),
            cell_type: CellType::Code,
            content: CellContent::Text(content),
            metadata: CellMetadata::default(),
            output: None,
        }
    }
    
    /// 创建新的文本单元格
    pub fn new_text(content: String) -> Self {
        Self {
            id: Uuid::new_v4(),
            cell_type: CellType::Text,
            content: CellContent::Text(content),
            metadata: CellMetadata::default(),
            output: None,
        }
    }
    
    /// 创建新的 Markdown 单元格
    pub fn new_markdown(content: String) -> Self {
        Self {
            id: Uuid::new_v4(),
            cell_type: CellType::Markdown,
            content: CellContent::Text(content),
            metadata: CellMetadata::default(),
            output: None,
        }
    }
    
    /// 创建输出单元格
    pub fn new_output(value: String, format: FormatType, execution_time: Option<std::time::Duration>) -> Self {
        Self {
            id: Uuid::new_v4(),
            cell_type: CellType::Output,
            content: CellContent::Result { value, format, execution_time },
            metadata: CellMetadata::default(),
            output: None,
        }
    }
    
    /// 设置单元格内容
    pub fn set_content(&mut self, content: CellContent) {
        self.content = content;
        self.metadata.mark_dirty();
    }
    
    /// 设置文本内容
    pub fn set_text(&mut self, text: String) {
        self.content = CellContent::Text(text);
        self.metadata.mark_dirty();
    }
    
    /// 获取文本内容
    pub fn get_text(&self) -> String {
        self.content.as_text()
    }
    
    /// 检查单元格是否可执行
    pub fn is_executable(&self) -> bool {
        self.cell_type.is_executable()
    }
    
    /// 检查单元格是否可编辑
    pub fn is_editable(&self) -> bool {
        self.cell_type.is_editable()
    }
    
    /// 检查单元格是否需要重新执行
    pub fn needs_execution(&self) -> bool {
        self.metadata.is_dirty && self.is_executable()
    }
    
    /// 设置输出结果
    pub fn set_output(&mut self, output: NotebookCell) {
        self.output = Some(Box::new(output));
        self.metadata.mark_executed();
    }
    
    /// 清除输出结果
    pub fn clear_output(&mut self) {
        self.output = None;
    }
    
    /// 获取输出结果
    pub fn get_output(&self) -> Option<&NotebookCell> {
        self.output.as_ref().map(|b| b.as_ref())
    }
    
    /// 转换单元格类型
    pub fn convert_to(&mut self, new_type: CellType) {
        if self.cell_type != new_type {
            self.cell_type = new_type;
            self.metadata.mark_dirty();
            
            // 清除输出(如果不再是代码单元格)
            if !self.is_executable() {
                self.clear_output();
            }
        }
    }
    
    /// 复制单元格
    pub fn duplicate(&self) -> Self {
        let mut new_cell = self.clone();
        new_cell.id = Uuid::new_v4();
        new_cell.metadata = CellMetadata::default();
        new_cell.clear_output();
        new_cell
    }
    
    /// 获取单元格摘要信息
    pub fn summary(&self) -> String {
        let content_preview = {
            let text = self.get_text();
            if text.len() > 50 {
                format!("{}...", &text[..47])
            } else {
                text
            }
        };
        
        format!(
            "{} [{}] - {}",
            self.cell_type.display_name(),
            if self.metadata.is_dirty { "已修改" } else { "已保存" },
            content_preview
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_cell_type_properties() {
        assert_eq!(CellType::Code.display_name(), "代码");
        assert!(CellType::Code.is_executable());
        assert!(CellType::Code.is_editable());
        
        assert_eq!(CellType::Output.display_name(), "输出");
        assert!(!CellType::Output.is_executable());
        assert!(!CellType::Output.is_editable());
    }
    
    #[test]
    fn test_cell_content() {
        let content = CellContent::Text("2 + 3".to_string());
        assert_eq!(content.as_text(), "2 + 3");
        assert!(!content.is_empty());
        assert_eq!(content.len(), 5);
        
        let empty_content = CellContent::Text("   ".to_string());
        assert!(empty_content.is_empty());
    }
    
    #[test]
    fn test_cell_metadata() {
        let mut metadata = CellMetadata::default();
        assert!(!metadata.is_dirty);
        assert_eq!(metadata.execution_count, 0);
        
        metadata.mark_dirty();
        assert!(metadata.is_dirty);
        
        metadata.mark_executed();
        assert!(!metadata.is_dirty);
        assert_eq!(metadata.execution_count, 1);
        assert!(metadata.last_executed.is_some());
    }
    
    #[test]
    fn test_notebook_cell_creation() {
        let cell = NotebookCell::new_code("x + y".to_string());
        assert!(cell.is_executable());
        assert!(cell.is_editable());
        assert_eq!(cell.get_text(), "x + y");
        
        let text_cell = NotebookCell::new_text("这是一个文本单元格".to_string());
        assert!(!text_cell.is_executable());
        assert!(text_cell.is_editable());
    }
    
    #[test]
    fn test_cell_output() {
        let mut cell = NotebookCell::new_code("2 + 3".to_string());
        assert!(cell.get_output().is_none());
        
        let output = NotebookCell::new_output(
            "5".to_string(), 
            FormatType::Standard, 
            Some(std::time::Duration::from_millis(10))
        );
        
        cell.set_output(output);
        assert!(cell.get_output().is_some());
        assert_eq!(cell.get_output().unwrap().get_text(), "5");
    }
    
    #[test]
    fn test_cell_conversion() {
        let mut cell = NotebookCell::new_code("# 标题".to_string());
        assert_eq!(cell.cell_type, CellType::Code);
        
        cell.convert_to(CellType::Markdown);
        assert_eq!(cell.cell_type, CellType::Markdown);
        assert!(cell.metadata.is_dirty);
    }
    
    #[test]
    fn test_cell_duplication() {
        let original = NotebookCell::new_code("原始内容".to_string());
        let duplicate = original.duplicate();
        
        assert_ne!(original.id, duplicate.id);
        assert_eq!(original.get_text(), duplicate.get_text());
        assert_eq!(duplicate.metadata.execution_count, 0);
    }
    
    #[test]
    fn test_cell_tags_and_properties() {
        let mut metadata = CellMetadata::default();
        
        metadata.add_tag("重要".to_string());
        metadata.add_tag("数学".to_string());
        assert_eq!(metadata.tags.len(), 2);
        
        metadata.remove_tag("重要");
        assert_eq!(metadata.tags.len(), 1);
        assert_eq!(metadata.tags[0], "数学");
        
        metadata.set_property("难度".to_string(), "中等".to_string());
        assert_eq!(metadata.get_property("难度"), Some(&"中等".to_string()));
    }
}