spreadsheet_ods/
metadata.rs

1//! Document metadata.
2
3use crate::xlink::{XLinkActuate, XLinkShow, XLinkType};
4use chrono::{Duration, NaiveDateTime};
5use get_size::GetSize;
6use get_size_derive::GetSize;
7
8/// Metadata
9#[derive(Debug, Clone, Default)]
10pub struct Metadata {
11    /// The <meta:generator> element contains a string that identifies the OpenDocument producer
12    /// that was used to create or last modify the document. This string should match the definition for
13    /// user-agents in the HTTP protocol as specified in section 14.43 of RFC2616. The generator string
14    /// should allow OpenDocument consumers to distinguish between all released versions of a
15    /// producer.
16    /// Note: Release versions of a producer could be distinguished based on build
17    /// identifiers or patch-level information.
18    /// If an OpenDocument producer that creates a document cannot provide an identifier string, the
19    /// producer shall not export this element. If a producer stores a modified document created by
20    /// another producer cannot provide a unique identifier, it shall not export the original identifier
21    /// belonging to the producer that created the document.
22    pub generator: String,
23    /// The dc:title element specifies the title of a document
24    pub title: String,
25    /// The dc:description element contains a description of a document.
26    pub description: String,
27    /// The dc:subject element specifies the subject of a document
28    pub subject: String,
29    /// The meta:keyword element contains a keyword pertaining to a document.
30    pub keyword: String,
31    /// The meta:initial-creator element specifies the name of the initial creator of a document
32    pub initial_creator: String,
33    /// The dc:creator element specifies the name of the person who last modified a
34    /// document, who created an annotation, who authored a change .
35    pub creator: String,
36    /// The meta:printed-by element specifies the name of the last person who printed a
37    /// document.
38    pub printed_by: String,
39    /// The meta:creation-date element specifies the date and time when a document was
40    /// created.
41    pub creation_date: Option<NaiveDateTime>,
42    /// The dc:date element specifies the date and time when the document was last
43    /// modified, when an annotation was created, when a change was made.
44    pub date: Option<NaiveDateTime>,
45    /// The meta:print-date element specifies the date and time when a document was last
46    /// printed.
47    pub print_date: Option<NaiveDateTime>,
48    /// The dc:language element specifies the default language of a document
49    pub language: String,
50    /// The meta:editing-cycles element specifies the number of times a document has been
51    /// edited. When a document is created, this value is set to 1. Each time a document is saved, the
52    /// editing-cycles number is incremented by 1.
53    pub editing_cycles: u32,
54    /// The meta:editing-duration element specifies the total time spent editing a document.
55    pub editing_duration: Duration,
56    /// The <meta:template> element specifies an IRI for the document template that was used to
57    /// create a document. The IRI is specified using the xlink:href attribute.
58    pub template: MetaTemplate,
59    /// The meta:auto-reload element specifies whether a document is reloaded or replaced by
60    /// another document after a specified period of time has elapsed.
61    pub auto_reload: MetaAutoReload,
62    /// The meta:hyperlink-behaviour element specifies the default behavior for hyperlinks in a
63    /// document.
64    pub hyperlink_behaviour: MetaHyperlinkBehaviour,
65    /// The meta:document-statistic element represents statistics about a document.
66    pub document_statistics: MetaDocumentStatistics,
67    /// The <meta:user-defined> element specifies any additional user-defined metadata for a
68    /// document.
69    pub user_defined: Vec<MetaUserDefined>,
70}
71
72impl GetSize for Metadata {
73    fn get_heap_size(&self) -> usize {
74        self.generator.get_heap_size()
75            + self.title.get_heap_size()
76            + self.description.get_heap_size()
77            + self.subject.get_heap_size()
78            + self.keyword.get_heap_size()
79            + self.initial_creator.get_heap_size()
80            + self.creator.get_heap_size()
81            + self.printed_by.get_heap_size()
82            + self.language.get_heap_size()
83            + self.editing_cycles.get_heap_size()
84            + self.template.get_heap_size()
85            + self.auto_reload.get_heap_size()
86            + self.hyperlink_behaviour.get_heap_size()
87            + self.document_statistics.get_heap_size()
88            + self.user_defined.get_heap_size()
89    }
90}
91
92/// Specifies an IRI for the document template that was used to
93/// create a document.
94#[derive(Debug, Default, Clone)]
95pub struct MetaTemplate {
96    /// The meta:date attribute specifies the date and time when a template was last modified, prior to
97    /// being used to create the current document.
98    pub date: Option<NaiveDateTime>,
99    /// See XLink
100    pub actuate: Option<XLinkActuate>,
101    /// See XLink
102    pub href: Option<String>,
103    /// See XLink
104    pub title: Option<String>,
105    /// See XLink
106    pub link_type: Option<XLinkType>,
107}
108
109impl GetSize for MetaTemplate {
110    fn get_heap_size(&self) -> usize {
111        self.actuate.get_heap_size()
112            + self.href.get_heap_size()
113            + self.title.get_heap_size()
114            + self.link_type.get_heap_size()
115    }
116}
117
118impl MetaTemplate {
119    /// Everything is None.
120    pub fn is_empty(&self) -> bool {
121        self.date.is_none()
122            && self.actuate.is_none()
123            && self.href.is_none()
124            && self.title.is_none()
125            && self.link_type.is_none()
126    }
127}
128
129/// Specifies whether a document is reloaded or replaced by
130/// another document after a specified period of time has elapsed.
131#[derive(Debug, Default, Clone)]
132pub struct MetaAutoReload {
133    /// The meta:delay attribute specifies a reload delay.
134    pub delay: Option<Duration>,
135    /// See XLink
136    pub actuate: Option<XLinkActuate>,
137    /// See XLink
138    pub href: Option<String>,
139    /// See XLink
140    pub show: Option<XLinkShow>,
141    /// See XLink
142    pub link_type: Option<XLinkType>,
143}
144
145impl GetSize for MetaAutoReload {
146    fn get_heap_size(&self) -> usize {
147        self.actuate.get_heap_size()
148            + self.href.get_heap_size()
149            + self.show.get_heap_size()
150            + self.link_type.get_heap_size()
151    }
152}
153
154impl MetaAutoReload {
155    /// Everything is None.
156    pub fn is_empty(&self) -> bool {
157        self.delay.is_none()
158            && self.actuate.is_none()
159            && self.href.is_none()
160            && self.show.is_none()
161            && self.link_type.is_none()
162    }
163}
164
165/// Specifies the default behavior for hyperlinks in a document.
166#[derive(Debug, Default, Clone, GetSize)]
167pub struct MetaHyperlinkBehaviour {
168    /// The office:target-frame-name attribute specifies the name of a target frame.
169    /// The defined values for the office:target-frame-name attribute are:
170    /// • _blank: The referenced document is displayed in a new frame.
171    /// • _parent: The referenced document is displayed in the parent frame of the current frame.
172    /// • _self: The referenced document replaces the content of the current frame.
173    /// • _top: The referenced document is displayed in the topmost frame, that is the frame that
174    /// contains the current frame as a child or descendant but is not contained within another frame.
175    /// • A frame name: The referenced document is displayed in the named frame. If the named frame
176    /// does not exist, a new frame with that name is created.
177    /// The office:target-frame-name attribute may be used together with an xlink:show 19.917
178    /// attribute. In that case, if the value of the attribute is _blank, the xlink:show attribute value
179    /// should be new. If the value of the attribute is any of the other value options, the value of the
180    /// xlink:show attribute should be replace.
181    pub target_frame_name: Option<String>,
182    /// See XLink
183    pub show: Option<XLinkShow>,
184}
185
186impl MetaHyperlinkBehaviour {
187    /// Everything is None.
188    pub fn is_empty(&self) -> bool {
189        self.target_frame_name.is_none() && self.show.is_none()
190    }
191}
192
193/// Represents statistics about a document.
194#[derive(Debug, Default, Clone, GetSize)]
195pub struct MetaDocumentStatistics {
196    ///
197    pub cell_count: u32,
198    ///
199    pub object_count: u32,
200    ///
201    pub ole_object_count: u32,
202    ///
203    pub table_count: u32,
204}
205
206/// Specifies any additional user-defined metadata for a document.
207#[derive(Debug, Clone, GetSize)]
208pub struct MetaUserDefined {
209    /// Name
210    pub name: String,
211    /// Value
212    pub value: MetaValue,
213}
214
215impl Default for MetaUserDefined {
216    fn default() -> Self {
217        Self {
218            name: "".to_string(),
219            value: MetaValue::String("".to_string()),
220        }
221    }
222}
223
224/// Value for user defined metadata.
225#[derive(Debug, Clone)]
226pub enum MetaValue {
227    ///
228    Boolean(bool),
229    ///
230    Datetime(NaiveDateTime),
231    ///
232    Float(f64),
233    ///
234    TimeDuration(Duration),
235    ///
236    String(String),
237}
238
239impl GetSize for MetaValue {
240    fn get_heap_size(&self) -> usize {
241        match self {
242            MetaValue::Boolean(_) => 0,
243            MetaValue::Datetime(_) => 0,
244            MetaValue::Float(_) => 0,
245            MetaValue::TimeDuration(_) => 0,
246            MetaValue::String(v) => v.get_heap_size(),
247        }
248    }
249}