use std::collections::BTreeMap;
use laravel::config::{Env, Section};
#[derive(Debug, Clone, PartialEq)]
pub enum Localized {
One(String),
Per(BTreeMap<String, String>),
}
impl Localized {
pub fn for_locale(&self, locale: &str) -> Option<&str> {
let text = match self {
Self::One(text) => text.as_str(),
Self::Per(texts) => texts.get(locale).or_else(|| texts.values().next())?.as_str(),
};
(!text.is_empty()).then_some(text)
}
}
impl From<&str> for Localized {
fn from(text: &str) -> Self {
Self::One(text.to_owned())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Segments {
pub category: BTreeMap<String, String>,
pub tag: BTreeMap<String, String>,
}
impl Default for Segments {
fn default() -> Self {
Self {
category: pairs([("en", "category"), ("es", "categoria")]),
tag: pairs([("en", "tag"), ("es", "etiqueta")]),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Views {
pub index: String,
pub show: String,
pub category: String,
pub tag: String,
}
impl Default for Views {
fn default() -> Self {
Self {
index: "blog.index".to_owned(),
show: "blog.show".to_owned(),
category: "blog.index".to_owned(),
tag: "blog.index".to_owned(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CardSettings {
pub enabled: bool,
pub template: String,
pub path: String,
pub brand: Option<String>,
}
impl Default for CardSettings {
fn default() -> Self {
Self {
enabled: true,
template: "post".to_owned(),
path: "img/blog".to_owned(),
brand: None,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct FeedSettings {
pub enabled: bool,
pub title: Option<Localized>,
pub description: Option<Localized>,
pub limit: usize,
}
impl Default for FeedSettings {
fn default() -> Self {
Self {
enabled: true,
title: None,
description: None,
limit: 20,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct CollectionConfig {
pub path: String,
pub locales: Option<Vec<String>>,
pub paths: BTreeMap<String, String>,
pub segments: Segments,
pub views: Views,
pub per_page: usize,
pub cards: CardSettings,
pub feed: FeedSettings,
}
impl Default for CollectionConfig {
fn default() -> Self {
Self {
path: "content/blog".to_owned(),
locales: None,
paths: pairs([("en", "blog"), ("es", "blog")]),
segments: Segments::default(),
views: Views::default(),
per_page: 12,
cards: CardSettings::default(),
feed: FeedSettings::default(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CacheSettings {
pub enabled: bool,
pub check_interval: Option<u64>,
}
impl Default for CacheSettings {
fn default() -> Self {
Self {
enabled: true,
check_interval: None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum HtmlInput {
#[default]
Allow,
Strip,
Escape,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MarkdownSettings {
pub html_input: HtmlInput,
pub allow_unsafe_links: bool,
pub external_links: bool,
}
impl Default for MarkdownSettings {
fn default() -> Self {
Self {
html_input: HtmlInput::Allow,
allow_unsafe_links: false,
external_links: true,
}
}
}
fn pairs<const N: usize>(pairs: [(&str, &str); N]) -> BTreeMap<String, String> {
pairs
.into_iter()
.map(|(key, value)| (key.to_owned(), value.to_owned()))
.collect()
}
fn default_collections() -> BTreeMap<String, CollectionConfig> {
BTreeMap::from([("blog".to_owned(), CollectionConfig::default())])
}
fn cache_from_env(env: &Env) -> Option<CacheSettings> {
Some(CacheSettings {
enabled: env.get("LARABLOG_CACHE").unwrap_or(true),
check_interval: env.get("LARABLOG_CACHE_CHECK_INTERVAL"),
})
}
#[derive(Debug, Clone, Section)]
pub struct LarablogConfig {
#[section(default = default_collections())]
pub collections: BTreeMap<String, CollectionConfig>,
#[env("LARABLOG_SHOW_SCHEDULED", default = false)]
pub show_scheduled: bool,
#[env("LARABLOG_SHOW_DRAFTS", default = false)]
pub show_drafts: bool,
#[section(default = 220)]
pub words_per_minute: u32,
#[env(with = cache_from_env)]
pub cache: CacheSettings,
pub markdown: MarkdownSettings,
}