metadata-gen 0.0.8

A powerful Rust library for extracting, validating, and processing metadata in YAML, TOML, and JSON formats from any content or data file.
Documentation
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! Metadata extraction and processing module.
//!
//! This module provides functionality for extracting metadata from various formats
//! (YAML, TOML, JSON) and processing it into a standardized structure.

use crate::error::MetadataError;
use crate::MetadataMap;
use alloc::string::{String, ToString};

mod date;
mod front_matter;
mod process;
mod scan;

#[cfg(all(
    test,
    feature = "std",
    feature = "yaml",
    feature = "toml",
    feature = "json"
))]
use date::standardize_date;
use front_matter::parse_block;
#[cfg(all(
    test,
    feature = "std",
    feature = "yaml",
    feature = "toml",
    feature = "json"
))]
use process::{generate_derived_fields, generate_slug};
pub use process::{
    process_metadata, process_metadata_with, DateOrder, ProcessOptions,
};
use scan::Scan;

/// Represents metadata for a page or content item.
///
/// # Example
///
/// ```
/// use metadata_gen::Metadata;
/// use std::collections::HashMap;
///
/// let mut data = HashMap::new();
/// data.insert("title".to_string(), "My Page".to_string());
/// let metadata = Metadata::new(data);
/// assert_eq!(metadata.get("title"), Some(&"My Page".to_string()));
/// ```
#[derive(Debug, Default, Clone)]
pub struct Metadata {
    /// The underlying key-value store for metadata fields.
    inner: MetadataMap,
}

impl Metadata {
    /// Creates a new `Metadata` instance with the given data.
    ///
    /// # Arguments
    ///
    /// * `data` - A [`MetadataMap`] containing the metadata key-value pairs.
    ///
    /// # Returns
    ///
    /// A new `Metadata` instance.
    pub fn new(data: MetadataMap) -> Self {
        Metadata { inner: data }
    }

    /// Retrieves the value associated with the given key.
    ///
    /// # Arguments
    ///
    /// * `key` - A string slice representing the key to look up.
    ///
    /// # Returns
    ///
    /// An `Option<&String>` containing the value if the key exists, or `None` otherwise.
    pub fn get(&self, key: &str) -> Option<&String> {
        self.inner.get(key)
    }

    /// Inserts a key-value pair into the metadata.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to insert.
    /// * `value` - The value to associate with the key.
    ///
    /// # Returns
    ///
    /// The old value associated with the key, if it existed.
    pub fn insert(
        &mut self,
        key: String,
        value: String,
    ) -> Option<String> {
        self.inner.insert(key, value)
    }

    /// Checks if the metadata contains the given key.
    ///
    /// # Arguments
    ///
    /// * `key` - A string slice representing the key to check for.
    ///
    /// # Returns
    ///
    /// `true` if the key exists, `false` otherwise.
    pub fn contains_key(&self, key: &str) -> bool {
        self.inner.contains_key(key)
    }

    /// Consumes the `Metadata` instance and returns the inner map.
    ///
    /// # Returns
    ///
    /// The inner [`MetadataMap`] containing all metadata key-value pairs.
    pub fn into_inner(self) -> MetadataMap {
        self.inner
    }
}

/// Extracts metadata from the content string.
///
/// This function attempts to extract metadata from YAML, TOML, or JSON formats.
///
/// # Arguments
///
/// * `content` - A string slice containing the content to extract metadata from.
///
/// # Returns
///
/// A `Result` containing the extracted `Metadata` if successful, or a `MetadataError` if extraction fails.
///
/// # Errors
///
/// Returns a `MetadataError::ExtractionError` if no valid front matter is found.
pub fn extract_metadata(
    content: &str,
) -> Result<Metadata, MetadataError> {
    extract_metadata_with_limits(content, &ParseLimits::default())
}

/// Resource limits applied while a front-matter block is parsed.
///
/// Front matter is untrusted input in any pipeline that ingests
/// contributed documents, so every parse runs under a budget. The
/// defaults are the YAML parser's strict preset narrowed to what a
/// document header plausibly needs; raise them with the builder methods
/// for manifests that are genuinely large.
///
/// # Example
///
/// ```
/// use metadata_gen::{extract_metadata_with_limits, ParseLimits};
///
/// let limits = ParseLimits::default().max_block_bytes(4096);
/// let err = extract_metadata_with_limits(
///     &format!("---\ntitle: {}\n---\n", "x".repeat(5000)),
///     &limits,
/// )
/// .unwrap_err();
/// assert!(err.to_string().contains("exceeds"));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct ParseLimits {
    /// Largest front-matter block, in bytes, that will be handed to a parser.
    pub max_block_bytes: usize,
    /// Deepest nesting of mappings and sequences the YAML parser accepts.
    pub max_depth: usize,
}

impl Default for ParseLimits {
    fn default() -> Self {
        Self {
            max_block_bytes: 1024 * 1024,
            max_depth: 64,
        }
    }
}

impl ParseLimits {
    /// Sets the largest block size, in bytes, a parser will be given.
    #[must_use]
    pub const fn max_block_bytes(mut self, bytes: usize) -> Self {
        self.max_block_bytes = bytes;
        self
    }

    /// Sets the deepest nesting the YAML parser accepts.
    #[must_use]
    pub const fn max_depth(mut self, depth: usize) -> Self {
        self.max_depth = depth;
        self
    }
}

/// Extracts metadata like [`extract_metadata`], under explicit [`ParseLimits`].
///
/// # Errors
///
/// Returns [`MetadataError::ExtractionError`] when no front matter is
/// found, the block exceeds `limits.max_block_bytes`, or an opening fence
/// has no closing fence (the message carries the byte offset);
/// [`MetadataError::Parse`] when a parser rejects the block; and
/// [`MetadataError::UnsupportedFormatError`] when the block's format was
/// not compiled in.
///
/// # Example
///
/// ```
/// use metadata_gen::{extract_metadata_with_limits, ParseLimits};
///
/// let doc = "---\ntitle: Limits\n---\n";
/// let metadata = extract_metadata_with_limits(doc, &ParseLimits::default()).unwrap();
/// assert_eq!(metadata.get("title").unwrap(), "Limits");
/// ```
pub fn extract_metadata_with_limits(
    content: &str,
    limits: &ParseLimits,
) -> Result<Metadata, MetadataError> {
    match scan::scan(content) {
        Scan::Found { format, raw, .. } => parse_block(format, raw, limits),
        Scan::Unterminated { format, offset } => {
            Err(MetadataError::ExtractionError {
                message: alloc::format!(
                    "{format} front matter opened at byte {offset} has no closing fence."
                ),
            })
        }
        Scan::Unsupported(format) => {
            Err(MetadataError::UnsupportedFormatError(format.to_string()))
        }
        Scan::None => Err(MetadataError::ExtractionError {
            message: "No valid front matter found.".to_string(),
        }),
    }
}

/// Which front-matter shape a document carries, and where its body starts.
///
/// Returned by [`detect_front_matter`]; the byte offset is the first byte
/// after the closing delimiter, so `&content[body_start..]` is the body.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum FrontMatterFormat {
    /// `---` fenced YAML.
    Yaml,
    /// `+++` fenced TOML.
    Toml,
    /// A JSON object at the top of the document.
    Json,
}

impl FrontMatterFormat {
    /// The format's lower-case name: `yaml`, `toml` or `json`.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Yaml => "yaml",
            Self::Toml => "toml",
            Self::Json => "json",
        }
    }
}

impl core::fmt::Display for FrontMatterFormat {
    fn fmt(
        &self,
        f: &mut core::fmt::Formatter<'_>,
    ) -> core::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Locates the front-matter block without parsing it.
///
/// Returns the format, the raw block, and the byte offset at which the
/// document body begins. Detection order is YAML, TOML, JSON, the same
/// order [`extract_metadata`] uses; `None` means no shape matched.
///
/// # Example
///
/// ```
/// use metadata_gen::metadata::{detect_front_matter, FrontMatterFormat};
///
/// let doc = "---\ntitle: T\n---\nBody";
/// let (format, raw, body_start) = detect_front_matter(doc).unwrap();
/// assert_eq!(format, FrontMatterFormat::Yaml);
/// assert_eq!(raw, "title: T");
/// assert_eq!(&doc[body_start..], "Body");
/// ```
pub fn detect_front_matter(
    content: &str,
) -> Option<(FrontMatterFormat, &str, usize)> {
    match scan::scan(content) {
        Scan::Found {
            format,
            raw,
            body_offset,
        } => Some((format, raw, body_offset)),
        _ => None,
    }
}

/// Extracts metadata and returns the document body alongside it.
///
/// The body is the text after the closing delimiter, with one leading
/// newline removed so a `---` fence on its own line does not leave an
/// empty first line. [`extract_metadata`] is this without the body.
///
/// # Errors
///
/// The same errors as [`extract_metadata`].
///
/// # Example
///
/// ```
/// use metadata_gen::metadata::extract_metadata_with_body;
///
/// let doc = "---\ntitle: T\n---\n# Heading\n\nText";
/// let (meta, body) = extract_metadata_with_body(doc).unwrap();
/// assert_eq!(meta.get("title").map(String::as_str), Some("T"));
/// assert_eq!(body, "# Heading\n\nText");
/// ```
pub fn extract_metadata_with_body(
    content: &str,
) -> Result<(Metadata, &str), MetadataError> {
    let metadata = extract_metadata(content)?;
    let body_start = detect_front_matter(content)
        .map_or(content.len(), |(_, _, start)| start);
    let body = content[body_start..]
        .strip_prefix("\r\n")
        .or_else(|| content[body_start..].strip_prefix('\n'))
        .unwrap_or(&content[body_start..]);
    Ok((metadata, body))
}

/// Deserialises the front matter into a typed value.
///
/// Where [`extract_metadata`] flattens everything to strings, this hands
/// the raw block to the format's own `serde` deserialiser, so integers
/// stay integers, sequences stay sequences and nested tables become
/// nested structs. The document body is ignored.
///
/// # Errors
///
/// [`MetadataError::ExtractionError`] when no front matter is found;
/// the format's parse error (`YamlError`, `TomlError`, `JsonError`) when
/// the block does not deserialise into `T`.
///
/// # Example
///
/// ```
/// use metadata_gen::metadata::extract_typed;
///
/// #[derive(serde::Deserialize)]
/// struct Front { title: String, tags: Vec<String>, draft: bool }
///
/// let doc = "---\ntitle: T\ntags: [a, b]\ndraft: false\n---\nBody";
/// let front: Front = extract_typed(doc).unwrap();
/// assert_eq!(front.tags, ["a", "b"]);
/// assert!(!front.draft);
/// ```
pub fn extract_typed<T: serde::de::DeserializeOwned + 'static>(
    content: &str,
) -> Result<T, MetadataError> {
    let (format, raw, _) =
        detect_front_matter(content).ok_or_else(|| {
            MetadataError::ExtractionError {
                message: "No valid front matter found.".to_string(),
            }
        })?;
    front_matter::deserialize_block::<T>(
        format,
        raw,
        &ParseLimits::default(),
    )
}

/// Deserialises the front matter into `T` while borrowing from `content`.
///
/// The zero-copy counterpart of [`extract_typed`]: a field typed
/// `&'a str` (or `Cow<'a, str>`) points into `content` instead of being
/// copied, for every format whose text already holds the value verbatim.
/// Values that need unescaping (a YAML `"line\nbreak"`, a JSON `\u00e9`)
/// are materialised by the parser as owned strings, which is why a
/// `Cow<'a, str>` field is the general choice.
///
/// # Errors
///
/// As [`extract_typed`].
///
/// # Example
///
/// ```
/// use metadata_gen::extract_typed_borrowed;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Post<'a> {
///     title: &'a str,
///     tags: Vec<&'a str>,
/// }
///
/// let doc = "{\"title\": \"Borrowed\", \"tags\": [\"a\", \"b\"]}\nbody";
/// let post: Post<'_> = extract_typed_borrowed(doc).unwrap();
/// assert_eq!(post.title, "Borrowed");
/// assert_eq!(post.tags, ["a", "b"]);
/// ```
pub fn extract_typed_borrowed<'a, T: serde::Deserialize<'a>>(
    content: &'a str,
) -> Result<T, MetadataError> {
    let (format, raw, _) =
        detect_front_matter(content).ok_or_else(|| {
            MetadataError::ExtractionError {
                message: "No valid front matter found.".to_string(),
            }
        })?;
    front_matter::deserialize_borrowed::<T>(
        format,
        raw,
        &ParseLimits::default(),
    )
}

#[cfg(all(
    test,
    feature = "std",
    feature = "yaml",
    feature = "toml",
    feature = "json"
))]
mod tests;

#[cfg(all(
    test,
    feature = "std",
    feature = "yaml",
    feature = "toml",
    feature = "json"
))]
mod coverage_tests;

#[cfg(all(
    test,
    feature = "std",
    feature = "yaml",
    feature = "toml",
    feature = "json"
))]
mod typed_api_tests;