diff --git a/book-example/book.toml b/book-example/book.toml index 0c5ece6b..d21c8487 100644 --- a/book-example/book.toml +++ b/book-example/book.toml @@ -3,6 +3,8 @@ title = "mdBook Documentation" description = "Create book from markdown files. Like Gitbook but implemented in Rust" authors = ["Mathieu David", "Michael-F-Bryan"] language = "en" + +[rust] edition = "2018" [output.html] diff --git a/book-example/src/format/config.md b/book-example/src/format/config.md index a76c9d08..82c896c3 100644 --- a/book-example/src/format/config.md +++ b/book-example/src/format/config.md @@ -59,9 +59,19 @@ language = "en" ### Rust options -Options for the Rust compiler used for playpen. +Options for the Rust language, relevant to running tests and playground +integration. -- **edition**: Rust edition to use by default for the code snippets. Defaults to `rustdoc` defaults (2015). +- **edition**: Rust edition to use by default for the code snippets. Default + is "2015". Individual code blocks can be controlled with the `edition2015` + or `edition2018` annotations, such as: + + ~~~text + ```rust,edition2015 + // This only works in 2015. + let try = true; + ``` + ~~~ ### Build options diff --git a/src/config.rs b/src/config.rs index b0b09f90..36190ebf 100644 --- a/src/config.rs +++ b/src/config.rs @@ -72,7 +72,7 @@ pub struct Config { pub book: BookConfig, /// Information about the build environment. pub build: BuildConfig, - /// Information passed to the Rust playground + /// Information about Rust language support. pub rust: RustConfig, rest: Value, } @@ -340,6 +340,7 @@ impl<'de> Deserialize<'de> for Config { impl Serialize for Config { fn serialize(&self, s: S) -> std::result::Result { use serde::ser::Error; + // TODO: This should probably be removed and use a derive instead. let mut table = self.rest.clone(); @@ -349,8 +350,10 @@ impl Serialize for Config { return Err(S::Error::custom("Unable to serialize the BookConfig")); } }; + let rust_config = Value::try_from(&self.rust).expect("should always be serializable"); table.insert("book", book_config).expect("unreachable"); + table.insert("rust", rust_config).expect("unreachable"); table.serialize(s) } } @@ -449,55 +452,17 @@ pub struct RustConfig { pub edition: Option, } -#[derive(Debug, Copy, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] /// Rust edition to use for the code. pub enum RustEdition { /// The 2018 edition of Rust + #[serde(rename = "2018")] E2018, /// The 2015 edition of Rust + #[serde(rename = "2015")] E2015, } -impl Serialize for RustEdition { - fn serialize(&self, serializer: S) -> std::result::Result - where - S: Serializer, - { - match self { - RustEdition::E2015 => serializer.serialize_str("2015"), - RustEdition::E2018 => serializer.serialize_str("2018"), - } - } -} - -impl<'de> Deserialize<'de> for RustEdition { - fn deserialize(de: D) -> std::result::Result - where - D: Deserializer<'de>, - { - use serde::de::Error; - - let raw = Value::deserialize(de)?; - - let edition = match raw { - Value::String(s) => s, - _ => { - return Err(D::Error::custom("Rust edition should be a string")); - } - }; - - let edition = match edition.as_str() { - "2018" => RustEdition::E2018, - "2015" => RustEdition::E2015, - e => { - return Err(D::Error::custom(format!("Unknown Rust edition: {}", e))); - } - }; - - Ok(edition) - } -} - /// Configuration for the HTML renderer. #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "kebab-case")] diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 302ec419..14012c12 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -808,7 +808,7 @@ mod tests { fn add_playpen_edition2015() { let inputs = [ ("x()", - "
\n#![allow(unused_variables)]\nfn main() {\nx()\n}\n
"), + "
\n#![allow(unused)]\nfn main() {\nx()\n}\n
"), ("fn main() {}", "
fn main() {}\n
"), ("fn main() {}", @@ -832,7 +832,7 @@ mod tests { fn add_playpen_edition2018() { let inputs = [ ("x()", - "
\n#![allow(unused_variables)]\nfn main() {\nx()\n}\n
"), + "
\n#![allow(unused)]\nfn main() {\nx()\n}\n
"), ("fn main() {}", "
fn main() {}\n
"), ("fn main() {}",