Skip to main content

easyofd_core/versions/
doc_version.rs

1//! 文档版本。
2
3use super::FileList;
4
5/// 对应 Java: org.ofdrw.core.basicStructure.DocVersion
6///
7/// 描述文档的一个版本,包含文档根路径和文件列表。
8#[derive(Debug, Clone)]
9pub struct DocVersion {
10    /// 文档根路径(如 "Doc_0")。
11    pub doc_root: String,
12    /// 该版本的文件列表。
13    pub file_list: Option<FileList>,
14}
15
16impl DocVersion {
17    /// 创建指定文档根路径的文档版本。
18    #[must_use]
19    pub fn new(doc_root: impl Into<String>) -> Self {
20        Self {
21            doc_root: doc_root.into(),
22            file_list: None,
23        }
24    }
25
26    /// 设置文件列表。
27    #[must_use]
28    pub fn with_file_list(mut self, file_list: FileList) -> Self {
29        self.file_list = Some(file_list);
30        self
31    }
32
33    /// 序列化为 XML 字符串。
34    #[must_use]
35    pub fn to_xml_string(&self) -> String {
36        let inner = match &self.file_list {
37            Some(fl) => fl.to_xml_string(),
38            None => String::new(),
39        };
40        format!(
41            "<DocVersion DocRoot=\"{}\">{inner}</DocVersion>",
42            self.doc_root
43        )
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    use crate::versions::File;
51
52    #[test]
53    fn test_doc_version_new() {
54        let dv = DocVersion::new("Doc_0");
55        assert_eq!(dv.doc_root, "Doc_0");
56        assert!(dv.file_list.is_none());
57    }
58
59    #[test]
60    fn test_doc_version_with_file_list_and_xml() {
61        let fl = FileList::new().with(File::new("OFD.xml", 1024));
62        let dv = DocVersion::new("Doc_0").with_file_list(fl);
63        let xml = dv.to_xml_string();
64        assert!(xml.contains("DocRoot=\"Doc_0\""));
65        assert!(xml.contains("OFD.xml"));
66        assert!(xml.contains("1024"));
67    }
68}