Skip to main content

get_collection

Function get_collection 

Source
pub fn get_collection<T: DeserializeOwned>(
    dir: impl AsRef<Path>,
) -> Result<Vec<Entry<T>>>
Expand description

Loads every Markdown file under dir whose frontmatter matches T. Returns entries sorted by slug.

§Errors

  • Returns the first I/O error encountered while walking the directory.
  • Returns the first frontmatter deserialisation error, with the failing path in the error chain (anyhow::Error::context).

Files without a frontmatter delimiter are silently skipped — the collection is for structured content, and pages without frontmatter aren’t part of the schema.

§Determinism

Output is sorted by Entry::slug (lexicographic). Callers that hash collections for golden tests or fingerprinting benefit directly.

§Examples

use serde::Deserialize;
use ssg::collections::get_collection;
use tempfile::tempdir;
use std::fs;

#[derive(Debug, Deserialize)]
struct Post { title: String }

let dir = tempdir().unwrap();
fs::write(dir.path().join("a.md"), "---\ntitle: Hi\n---\nBody").unwrap();
let posts: Vec<_> = get_collection::<Post>(dir.path()).unwrap();
assert_eq!(posts.len(), 1);
assert_eq!(posts[0].data.title, "Hi");