mail_headers_ng/header_components/
file_meta.rs

1
2use chrono::DateTime;
3use chrono::Utc;
4
5use std::mem::replace;
6
7#[cfg(feature="serde")]
8use serde::{Serialize, Deserialize};
9
10/// A struct representing common file metadata.
11///
12/// This is used by e.g. attachments, when attaching
13/// a file (or embedding an image). Through it's usage
14/// is optional.
15///
16/// # Stability Note
17///
18/// This is likely to move to an different place at
19/// some point, potentially in a different `mail-*`
20/// crate.
21#[derive(Debug, Clone, Eq, PartialEq, Hash, Default)]
22#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
23pub struct FileMeta {
24    /// The file name.
25    ///
26    /// Note that this utility is limited to utf-8 file names.
27    /// This is normally used when downloading a attachment to
28    /// choose the default file name.
29    #[cfg_attr(feature="serde", serde(default))]
30    pub file_name: Option<String>,
31
32    /// The creation date of the file (in utc).
33    #[cfg_attr(feature="serde", serde(default))]
34    #[cfg_attr(feature="serde", serde(with = "super::utils::serde::opt_date_time"))]
35    pub creation_date: Option<DateTime<Utc>>,
36
37    /// The last modification date of the file (in utc).
38    #[cfg_attr(feature="serde", serde(default))]
39    #[cfg_attr(feature="serde", serde(with = "super::utils::serde::opt_date_time"))]
40    pub modification_date: Option<DateTime<Utc>>,
41
42    /// The date time the file was read, i.e. placed in the mail (in utc).
43    #[cfg_attr(feature="serde", serde(default))]
44    #[cfg_attr(feature="serde", serde(with = "super::utils::serde::opt_date_time"))]
45    pub read_date: Option<DateTime<Utc>>,
46
47    /// The size the file should have.
48    ///
49    /// Note that normally mail explicitly opts to NOT specify the size
50    /// of a mime-multi part body (e.g. an attachments) and you can never
51    /// rely on it to e.g. skip ahead. But it has some uses wrt. thinks
52    /// like external headers.
53    #[cfg_attr(feature="serde", serde(default))]
54    pub size: Option<usize>
55}
56
57macro_rules! impl_replace_none {
58    ($self_:expr, $other:expr, [$($field:ident),*]) => ({
59        let &mut FileMeta {$(
60            ref mut $field
61        ),*} = $self_;
62
63        $(
64            if $field.is_none() {
65                replace($field, $other.$field.clone());
66            }
67        )*
68    })
69}
70
71impl FileMeta {
72
73    /// Replaces all fields which are `None` with the value of the field in `other_meta`.
74    pub fn replace_empty_fields_with(&mut self, other_meta: &Self) {
75        impl_replace_none! {
76            self, other_meta,
77            [file_name, creation_date, modification_date, read_date, size]
78        }
79    }
80}