1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use super::readable::ReadableDate;
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};

#[derive(Debug, Clone, PartialEq)]
pub enum Lang {
    Apple,
    Ringo,
    Pinggo,
    Apfel,
}

impl Default for Lang {
    fn default() -> Self {
        Self::Apple
    }
}

impl Serialize for Lang {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(match *self {
            Self::Apple => "Apple",
            Self::Ringo => "林檎",
            Self::Pinggo => "苹果",
            Self::Apfel => "Apfel",
        })
    }
}

impl<'de> Deserialize<'de> for Lang {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        match String::deserialize(deserializer)?.as_ref() {
            "Apple" | "Apples" | "apple" | "apples" => Ok(Self::Apple),
            "林檎" | "リンゴ" | "りんご" | "Ringo" => Ok(Self::Ringo),
            "苹果" | "蘋果" | "Pinggo" => Ok(Self::Pinggo),
            "Apfel" | "Äpfel" => Ok(Self::Apfel),
            s @ _ => Err(serde::de::Error::custom(format!(
                "key [status] value not supported value {}",
                s
            ))),
        }
    }
}

/// The status of the file. This is reserved for future usage,
#[derive(Debug, Clone, PartialEq)]
pub enum Status {
    /// Mark the file as a draft.
    ///
    /// WIP, not finished yet.
    Draft,

    /// File could be viewed in public scope.
    ///
    /// Finished and could be hosted publicly.
    Public,

    /// File could be viewed in private scope.
    ///
    /// Finished but concern about the privacy.
    Internal,
}

impl Default for Status {
    fn default() -> Self {
        Self::Draft
    }
}

impl Serialize for Status {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(match *self {
            Self::Draft => "Draft",
            Self::Public => "Public",
            Self::Internal => "Internal",
        })
    }
}

impl<'de> Deserialize<'de> for Status {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        match String::deserialize(deserializer)?.as_ref() {
            "Draft" => Ok(Self::Draft),
            "Public" => Ok(Self::Public),
            "Internal" => Ok(Self::Internal),
            s @ _ => Err(serde::de::Error::custom(format!(
                "key [status] value not supported value {}",
                s
            ))),
        }
    }
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Banner {
    /// Create the outdate banner once the duration since last update is larger than
    /// this value in day.
    #[serde(rename = "Outdate", default)]
    pub outdate: i64,
}

fn default_template() -> String {
    "Article".into()
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Render {
    /// Pick the template for current source file.
    #[serde(rename = "Template", default = "default_template")]
    pub template: String,

    /// The output file path.
    #[serde(rename = "Target", default)]
    pub target: Option<std::path::PathBuf>,
}

impl Default for Render {
    fn default() -> Self {
        Self {
            template: "Article".to_owned(),
            target: None,
        }
    }
}

/// The frontmatter in a Markdown file.
///
/// **Note**, mercurius only parses the frontmatter, it would never modify the parsing result
/// itself. Functionalities below is implemented by another binary package named [quatrain].
///
/// [quatrain]: https://github.com/equt/quatrain
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct FrontMatter {
    /// The title of the current file.
    ///
    /// ## HTML Rendering
    ///
    /// 1. Title would be used in the `<title>` tag.
    /// 2. It would also be prepended as an `<h1>` at the front of the article.
    #[serde(rename = "Title")]
    pub title: String,

    /// The create date of the file.
    ///
    /// This should be the exact date the file was created, even empty.
    #[serde(rename = "Create Date")]
    pub create_date: ReadableDate,

    /// The last modified date of the file.
    ///
    /// > Formatting or _rewriting_ should not be taken into account.
    ///
    /// ## HTML Render
    ///
    /// 1. This field is now used to calculate the [_Outdate Info_].
    ///
    /// [_Outdate Info_]: struct.Banner.html#structfield.outdate
    #[serde(rename = "Last Update", default)]
    pub last_update: Option<ReadableDate>,

    /// Main language of the file.
    ///
    /// This language should be the one used most in this file. Use the word `Apple` to
    /// refer that language, e.g., `林檎`.
    #[serde(rename = "Language", default)]
    pub lang: Lang,

    /// The description of the file.
    ///
    /// Currently this field will not be used in any place. This is reserved for future usage,
    /// e.g., the `<meta>` tag.
    #[serde(rename = "Description", default)]
    pub description: Option<String>,

    /// Tagging the file.
    ///
    /// Currently this field will not be used in any place. This is reserved for future usage,
    /// e.g., filtering by tags.
    #[serde(rename = "Tags")]
    pub tags: Option<Vec<String>>,

    /// The status of the file.
    ///
    /// Currently this field will not be used in any place. This is reserved for future usage,
    /// e.g., conditional rendering or publishing.
    #[serde(rename = "Status", default)]
    pub status: Status,

    /// Control the rendering behavior.
    #[serde(rename = "Render", default)]
    pub render: Render,

    /// The banner of the file.
    #[serde(rename = "Banner", default)]
    pub banner: Banner,
}

#[cfg(test)]
mod test {
    use super::*;
    use serde_yaml;

    #[test]
    fn deserialize() {
        let fm: FrontMatter = serde_yaml::from_str(
            r"
Title: Foo

Create Date: Jan 12, 2001
Last Update: Feb 02, 2002
",
        )
        .unwrap();
        assert_eq!(
            fm,
            FrontMatter {
                title: "Foo".to_owned(),
                create_date: ReadableDate::new(2001, 1, 12),
                last_update: Some(ReadableDate::new(2002, 2, 02)),
                status: Status::Draft,
                ..Default::default()
            }
        )
    }
}