larablog 0.1.0

Markdown blogs for Laravel Rust in several languages, where every language has its own URL.
Documentation
use serde::Serialize;
use serde_json::{Map, Value};

use crate::support;

/// A category in one language: the same `key` everywhere, a name and a slug of its own.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Category {
    /// The collection it belongs to.
    pub collection: String,
    /// As written in the posts' frontmatter.
    pub key: String,
    /// The language this is the category in.
    pub locale: String,
    /// Its name.
    pub name: String,
    /// Its URL segment.
    pub slug: String,
    /// What it is about.
    pub description: Option<String>,
    /// A colour or a class name for your views.
    pub color: Option<String>,
    /// Any other field of its entry in `_categories.yml`.
    pub meta: Map<String, Value>,
}

impl Category {
    /// A field of the category's entry that has no property of its own, in this language when it is
    /// written per language. `accent` is also what the social card is tinted with.
    pub fn meta(&self, key: &str) -> Option<Value> {
        let value = support::data_get(&Value::Object(self.meta.clone()), key)?.clone();

        match &value {
            Value::Object(map) if map.contains_key(&self.locale) => map.get(&self.locale).cloned(),
            _ => Some(value),
        }
    }

    /// Its address, absolute.
    pub fn url(&self) -> String {
        self.url_with(true)
    }

    /// Its address, absolute or just the path.
    pub fn url_with(&self, absolute: bool) -> String {
        crate::collection(&self.collection).map_or_else(
            |_| String::new(),
            |blog| blog.url("category", Some(&self.locale), &[("category", &self.slug)], absolute),
        )
    }
}