Skip to main content

easydoc_core/metadata/
document.rs

1/// DOCX 文件的文档级元数据。
2///
3/// 对应 OOXML `docProps/core.xml` 中的 Dublin Core 元数据。
4/// 无直接 Java 对应(Java `EasyExcel` 的 `ReadSheet`/`WriteSheet` 仅描述表格级元数据)。
5#[derive(Debug, Clone, Default, PartialEq, Eq)]
6pub struct DocumentMeta {
7    /// Document title (maps to `dc:title`).
8    pub title: Option<String>,
9    /// Author name (maps to `dc:creator`).
10    pub author: Option<String>,
11    /// Subject / description (maps to `dc:description`).
12    pub subject: Option<String>,
13    /// Keywords, comma or semicolon separated (maps to `cp:keywords`).
14    pub keywords: Option<String>,
15    /// Page width in twips (default: A4 portrait = 11906).
16    pub page_width: Option<u32>,
17    /// Page height in twips (default: A4 portrait = 16838).
18    pub page_height: Option<u32>,
19    /// Page orientation: `true` = landscape.
20    pub landscape: bool,
21}
22
23impl DocumentMeta {
24    /// 创建默认文档元数据。
25    #[must_use]
26    pub fn new() -> Self {
27        Self::default()
28    }
29
30    /// 设置文档标题。
31    #[must_use]
32    pub fn title(mut self, title: impl Into<String>) -> Self {
33        self.title = Some(title.into());
34        self
35    }
36
37    /// 设置作者名称。
38    #[must_use]
39    pub fn author(mut self, author: impl Into<String>) -> Self {
40        self.author = Some(author.into());
41        self
42    }
43
44    /// 设置主题。
45    #[must_use]
46    pub fn subject(mut self, subject: impl Into<String>) -> Self {
47        self.subject = Some(subject.into());
48        self
49    }
50
51    /// 设置关键词。
52    #[must_use]
53    pub fn keywords(mut self, keywords: impl Into<String>) -> Self {
54        self.keywords = Some(keywords.into());
55        self
56    }
57
58    /// 设置横向页面方向。
59    #[must_use]
60    pub fn landscape(mut self, landscape: bool) -> Self {
61        self.landscape = landscape;
62        self
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn document_meta_builder_chain() {
72        let meta = DocumentMeta::new()
73            .title("Test Document")
74            .author("Author Name")
75            .subject("Test Subject")
76            .keywords("rust,docx")
77            .landscape(true);
78        assert_eq!(meta.title.as_deref(), Some("Test Document"));
79        assert_eq!(meta.author.as_deref(), Some("Author Name"));
80        assert_eq!(meta.subject.as_deref(), Some("Test Subject"));
81        assert_eq!(meta.keywords.as_deref(), Some("rust,docx"));
82        assert!(meta.landscape);
83    }
84
85    #[test]
86    fn document_meta_default() {
87        let meta = DocumentMeta::default();
88        assert!(meta.title.is_none());
89        assert!(meta.author.is_none());
90        assert!(meta.subject.is_none());
91        assert!(meta.keywords.is_none());
92        assert!(meta.page_width.is_none());
93        assert!(meta.page_height.is_none());
94        assert!(!meta.landscape);
95    }
96
97    #[test]
98    fn document_meta_clone_eq() {
99        let meta = DocumentMeta::new().title("Test");
100        let meta2 = meta.clone();
101        assert_eq!(meta, meta2);
102    }
103
104    #[test]
105    fn document_meta_debug() {
106        let meta = DocumentMeta::new().title("Test");
107        let dbg = format!("{meta:?}");
108        assert!(dbg.contains("Test"));
109    }
110
111    #[test]
112    fn document_meta_page_dimensions() {
113        let mut meta = DocumentMeta::new();
114        meta.page_width = Some(11906);
115        meta.page_height = Some(16838);
116        assert_eq!(meta.page_width, Some(11906));
117        assert_eq!(meta.page_height, Some(16838));
118    }
119}