Skip to main content

get_entry

Function get_entry 

Source
pub fn get_entry<T: DeserializeOwned>(
    dir: impl AsRef<Path>,
    slug: &str,
) -> Result<Option<Entry<T>>>
Expand description

Loads a single entry from dir whose slug matches slug.

Returns Ok(None) when no Markdown file with that slug exists. Use get_collection when you need every entry or when you don’t know the slug ahead of time.

§Examples

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

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

let dir = tempdir().unwrap();
fs::write(dir.path().join("hello.md"), "---\ntitle: Hello\n---\nBody").unwrap();
let entry = get_entry::<Post>(dir.path(), "hello").unwrap();
assert!(entry.is_some());
assert!(get_entry::<Post>(dir.path(), "missing").unwrap().is_none());

§Errors

Same as get_collection.