From 4f3d4cf38a58c53f69d7738fdb56b27467e7f122 Mon Sep 17 00:00:00 2001 From: LunarEclipse Date: Thu, 25 Apr 2024 15:33:39 +0200 Subject: [PATCH] Implemented backwards-compatibility for feed config --- components/config/src/config/languages.rs | 3 ++ components/config/src/config/mod.rs | 47 ++++++++++++++++++- .../content/src/front_matter/section.rs | 2 +- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/components/config/src/config/languages.rs b/components/config/src/config/languages.rs index ced0cf3437..6bc72488a3 100644 --- a/components/config/src/config/languages.rs +++ b/components/config/src/config/languages.rs @@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize}; use crate::config::search; use crate::config::taxonomies; +use crate::config::might_be_single; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(default)] @@ -15,9 +16,11 @@ pub struct LanguageOptions { /// Description of the site. Defaults to None pub description: Option, /// Whether to generate feeds for that language, defaults to `false` + #[serde(alias = "generate_feed")] pub generate_feeds: bool, /// The filenames to use for feeds. Used to find the templates, too. /// Defaults to ["atom.xml"], with "rss.xml" also having a template provided out of the box. + #[serde(deserialize_with = "might_be_single")] pub feed_filenames: Vec, pub taxonomies: Vec, /// Whether to generate search index for that language, defaults to `false` diff --git a/components/config/src/config/mod.rs b/components/config/src/config/mod.rs index ab7c6428d2..bf73eb8987 100644 --- a/components/config/src/config/mod.rs +++ b/components/config/src/config/mod.rs @@ -10,7 +10,8 @@ use std::path::{Path, PathBuf}; use libs::globset::GlobSet; use libs::toml::Value as Toml; -use serde::{Deserialize, Serialize}; +use serde::de::DeserializeOwned; +use serde::{Deserialize, Deserializer, Serialize}; use crate::theme::Theme; use errors::{anyhow, bail, Result}; @@ -50,11 +51,13 @@ pub struct Config { translations: HashMap, /// Whether to generate feeds. Defaults to false. + #[serde(alias = "generate_feed")] pub generate_feeds: bool, /// The number of articles to include in the feed. Defaults to including all items. pub feed_limit: Option, /// The filenames to use for feeds. Used to find the templates, too. /// Defaults to ["atom.xml"], with "rss.xml" also having a template provided out of the box. + #[serde(deserialize_with = "might_be_single")] pub feed_filenames: Vec, /// If set, files from static/ will be hardlinked instead of copied to the output dir. pub hard_link_static: bool, @@ -979,3 +982,45 @@ author = "person@example.com (Some Person)" assert_eq!(config.author, Some("person@example.com (Some Person)".to_owned())) } } + +/// Used for deserializing values that can be either a single value or a vec of values +pub(crate) fn might_be_single<'de, T, D>(deserializer: D) -> Result, D::Error> +where + T: DeserializeOwned, + D: Deserializer<'de>, +{ + let v = MightBeSingle::deserialize(deserializer)?; + Ok(v.into()) +} + +#[derive(Debug, Clone, Deserialize, PartialEq, Eq)] +#[serde(untagged)] +pub(crate) enum MightBeSingle { + Multiple(Vec), + One(T), + None, +} + +impl From> for Vec { + fn from(x: MightBeSingle) -> Vec { + use MightBeSingle::*; + + match x { + Multiple(v) => v, + One(v) => vec![v], + None => vec![], + } + } +} + +impl From> for MightBeSingle { + fn from(value: Vec) -> Self { + Self::Multiple(value) + } +} + +impl Default for MightBeSingle { + fn default() -> Self { + Self::None + } +} diff --git a/components/content/src/front_matter/section.rs b/components/content/src/front_matter/section.rs index df6bb50ded..43b579d511 100644 --- a/components/content/src/front_matter/section.rs +++ b/components/content/src/front_matter/section.rs @@ -68,7 +68,7 @@ pub struct SectionFrontMatter { #[serde(skip_serializing)] pub aliases: Vec, /// Whether to generate a feed for the current section - #[serde(skip_serializing)] + #[serde(skip_serializing, alias = "generate_feed")] pub generate_feeds: bool, /// Any extra parameter present in the front matter pub extra: Map,