-
Notifications
You must be signed in to change notification settings - Fork 991
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a test for feed config backwards-compat, fixed bugs
- Fixed language config merge bug found by a test - Adjusted two existing tests to fully check stuff related to multiple feeds - Added a new test for backwards-compatibility of the changes - Fixed bugs found by the newly added test
- Loading branch information
1 parent
625957a
commit 2dae1d1
Showing
3 changed files
with
74 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,7 @@ pub struct Config { | |
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")] | ||
#[serde(alias = "feed_filename", 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, | ||
|
@@ -400,6 +400,48 @@ impl Default for Config { | |
} | ||
} | ||
|
||
/// 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 | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
@@ -981,46 +1023,17 @@ author = "[email protected] (Some Person)" | |
let config = Config::parse(config).unwrap(); | ||
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) | ||
} | ||
} | ||
#[test] | ||
fn test_backwards_compatibility_for_feeds() { | ||
let config = r#" | ||
base_url = "example.com" | ||
generate_feed = true | ||
feed_filename = "test.xml" | ||
"#; | ||
|
||
impl<T> Default for MightBeSingle<T> { | ||
fn default() -> Self { | ||
Self::None | ||
let config = Config::parse(config).unwrap(); | ||
assert_eq!(config.generate_feeds, true); | ||
assert_eq!(config.feed_filenames, vec!["test.xml".to_owned()]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters