Skip to main content

easyofd_core/versions/
file_list.rs

1//! 文件列表。
2
3use super::File;
4
5/// 对应 Java: org.ofdrw.core.basicStructure.FileList
6///
7/// 文件列表容器,包含一个版本中所有文件的信息。
8#[derive(Debug, Clone)]
9pub struct FileList {
10    /// 文件列表。
11    pub files: Vec<File>,
12}
13
14impl FileList {
15    /// 创建空的文件列表。
16    #[must_use]
17    pub fn new() -> Self {
18        Self { files: Vec::new() }
19    }
20
21    /// 添加一个文件并返回自身(链式调用)。
22    #[must_use]
23    pub fn with(mut self, file: File) -> Self {
24        self.files.push(file);
25        self
26    }
27
28    /// 添加一个文件。
29    pub fn push(&mut self, file: File) {
30        self.files.push(file);
31    }
32
33    /// 序列化为 XML 字符串。
34    #[must_use]
35    pub fn to_xml_string(&self) -> String {
36        let inner: String = self.files.iter().map(File::to_xml_string).collect();
37        format!("<FileList>{inner}</FileList>")
38    }
39}
40
41impl Default for FileList {
42    fn default() -> Self {
43        Self::new()
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_file_list_new() {
53        let fl = FileList::new();
54        assert!(fl.files.is_empty());
55        let fl2 = FileList::default();
56        assert!(fl2.files.is_empty());
57    }
58
59    #[test]
60    fn test_file_list_with_and_xml() {
61        let fl = FileList::new()
62            .with(File::new("a.xml", 100))
63            .with(File::new("b.xml", 200));
64        assert_eq!(fl.files.len(), 2);
65        let xml = fl.to_xml_string();
66        assert!(xml.contains("<FileList>"));
67        assert!(xml.contains("a.xml"));
68        assert!(xml.contains("b.xml"));
69    }
70}