Skip to content

Commit

Permalink
added #[serde(deny_unknown_fields)] to front-matter, fixed problems t…
Browse files Browse the repository at this point in the history
…his found in tests
  • Loading branch information
LunarEclipse363 committed Jun 17, 2024
1 parent 56cb421 commit 2d3ea16
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 45 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- Added support for generating multiple kinds of feeds at once
- Changed config options named `generate_feed` to `generate_feeds` (both in config.toml and in section front-matter)
- Changed config option `feed_filename: String` to `feed_filenames: Vec<String>`
- The config file no longer allows arbitrary fields outside the `[extra]` section (front-matter is unaffected)
- The config file no longer allows arbitrary fields outside the `[extra]` section

## 0.18.0 (2023-12-18)

Expand Down
28 changes: 1 addition & 27 deletions components/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod config;
pub mod highlighting;
mod theme;

use std::{marker::PhantomData, path::Path};
use std::path::Path;

pub use crate::config::{
languages::LanguageOptions,
Expand All @@ -14,35 +14,9 @@ pub use crate::config::{
Config,
};
use errors::Result;
use serde::Deserialize;

/// Get and parse the config.
/// If it doesn't succeed, exit
pub fn get_config(filename: &Path) -> Result<Config> {
Config::from_file(filename)
}

/// This is used to print an error message for deprecated fields
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Deprecated<T> {
_type: PhantomData<T>,
}

impl<T> Deprecated<T> {
pub fn new() -> Self {
Self { _type: PhantomData }
}
}

pub trait DeprecationReason {
const REASON: &'static str;
}

impl<'de, T: DeprecationReason> Deserialize<'de> for Deprecated<T> {
fn deserialize<D>(_deserializer: D) -> std::prelude::v1::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
Err(serde::de::Error::custom(format!("Failed to parse a deprecated option: {}", T::REASON)))
}
}
14 changes: 1 addition & 13 deletions components/content/src/front_matter/section.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use libs::tera::{Map, Value};
use serde::{Deserialize, Serialize};

use config::{Deprecated, DeprecationReason};
use errors::Result;
use utils::de::fix_toml_dates;
use utils::types::InsertAnchor;
Expand All @@ -13,7 +12,7 @@ static DEFAULT_PAGINATE_PATH: &str = "page";

/// The front matter of every section
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
#[serde(default, deny_unknown_fields)]
pub struct SectionFrontMatter {
/// <title> of the page
pub title: Option<String>,
Expand Down Expand Up @@ -68,23 +67,13 @@ pub struct SectionFrontMatter {
/// redirect to this
#[serde(skip_serializing)]
pub aliases: Vec<String>,
/// Deprecated
#[serde(skip_serializing)]
pub generate_feed: Deprecated<DeprecatedGenerateFeed>,
/// Whether to generate a feed for the current section
#[serde(skip_serializing)]
pub generate_feeds: bool,
/// Any extra parameter present in the front matter
pub extra: Map<String, Value>,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct DeprecatedGenerateFeed {}

impl DeprecationReason for DeprecatedGenerateFeed {
const REASON: &'static str = "generate_feed is deprecated, please use generate_feeds instead";
}

impl SectionFrontMatter {
pub fn parse(raw: &RawFrontMatter) -> Result<SectionFrontMatter> {
let mut f: SectionFrontMatter = raw.deserialize()?;
Expand Down Expand Up @@ -124,7 +113,6 @@ impl Default for SectionFrontMatter {
transparent: false,
page_template: None,
aliases: Vec::new(),
generate_feed: Deprecated::new(),
generate_feeds: false,
extra: Map::new(),
draft: false,
Expand Down
2 changes: 1 addition & 1 deletion components/content/src/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ mod tests {
create_dir_all(path.join(&article_path).join("foo/baz/quux"))
.expect("create nested temp dir");
let mut f = File::create(article_path.join("_index.md")).unwrap();
f.write_all(b"+++\nslug=\"hey\"\n+++\n").unwrap();
f.write_all(b"+++\n+++\n").unwrap();
File::create(article_path.join("example.js")).unwrap();
File::create(article_path.join("graph.jpg")).unwrap();
File::create(article_path.join("fail.png")).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion test_site_i18n/content/blog/_index.fr.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
+++
title = "Mon blog"
sort_by = "date"
insert_anchors = "right"
insert_anchor_links = "right"
+++

[Dernières nouvelles](#news)
Expand Down
2 changes: 1 addition & 1 deletion test_site_i18n/content/blog/_index.it.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
+++
title = "Il mio blog"
sort_by = "date"
insert_anchors = "right"
insert_anchor_links = "right"
+++
2 changes: 1 addition & 1 deletion test_site_i18n/content/blog/_index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
+++
title = "My blog"
sort_by = "date"
insert_anchors = "left"
insert_anchor_links = "left"
+++

0 comments on commit 2d3ea16

Please sign in to comment.