Skip to content

Commit

Permalink
Implemented backwards-compatibility for feed config
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarEclipse363 committed Apr 25, 2024
1 parent 395b420 commit 0379e03
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
3 changes: 3 additions & 0 deletions components/config/src/config/languages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -15,9 +16,11 @@ pub struct LanguageOptions {
/// Description of the site. Defaults to None
pub description: Option<String>,
/// 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<String>,
pub taxonomies: Vec<taxonomies::TaxonomyConfig>,
/// Whether to generate search index for that language, defaults to `false`
Expand Down
47 changes: 46 additions & 1 deletion components/config/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -50,11 +51,13 @@ pub struct Config {
translations: HashMap<String, String>,

/// 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<usize>,
/// 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<String>,
/// If set, files from static/ will be hardlinked instead of copied to the output dir.
pub hard_link_static: bool,
Expand Down Expand Up @@ -979,3 +982,45 @@ author = "[email protected] (Some Person)"
assert_eq!(config.author, Some("[email protected] (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<Vec<T>, 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<T> {
Multiple(Vec<T>),
One(T),
None,
}

impl<T> From<MightBeSingle<T>> for Vec<T> {
fn from(x: MightBeSingle<T>) -> Vec<T> {
use MightBeSingle::*;

match x {
Multiple(v) => v,
One(v) => vec![v],
None => vec![],
}
}
}

impl<T> From<Vec<T>> for MightBeSingle<T> {
fn from(value: Vec<T>) -> Self {
Self::Multiple(value)
}
}

impl<T> Default for MightBeSingle<T> {
fn default() -> Self {
Self::None
}
}
2 changes: 1 addition & 1 deletion components/content/src/front_matter/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub struct SectionFrontMatter {
#[serde(skip_serializing)]
pub aliases: Vec<String>,
/// 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<String, Value>,
Expand Down

0 comments on commit 0379e03

Please sign in to comment.