diff --git a/src/book/mod.rs b/src/book/mod.rs index 286234bd..7f1159b4 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -274,6 +274,10 @@ impl MDBook { RustEdition::E2018 => { cmd.args(&["--edition", "2018"]); } + RustEdition::E2021 => { + cmd.args(&["--edition", "2021"]) + .args(&["-Z", "unstable-options"]); + } } } diff --git a/src/config.rs b/src/config.rs index c384192c..bf4aabbb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -467,6 +467,9 @@ pub struct RustConfig { #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] /// Rust edition to use for the code. pub enum RustEdition { + /// The 2021 edition of Rust + #[serde(rename = "2021")] + E2021, /// The 2018 edition of Rust #[serde(rename = "2018")] E2018, @@ -855,6 +858,26 @@ mod tests { assert_eq!(got.rust, rust_should_be); } + #[test] + fn edition_2021() { + let src = r#" + [book] + title = "mdBook Documentation" + description = "Create book from markdown files. Like Gitbook but implemented in Rust" + authors = ["Mathieu David"] + src = "./source" + [rust] + edition = "2021" + "#; + + let rust_should_be = RustConfig { + edition: Some(RustEdition::E2021), + }; + + let got = Config::from_str(src).unwrap(); + assert_eq!(got.rust, rust_should_be); + } + #[test] fn load_arbitrary_output_type() { #[derive(Debug, Deserialize, PartialEq)] diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 80b06bf8..02b7da46 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -836,13 +836,15 @@ fn add_playground_pre( { let contains_e2015 = classes.contains("edition2015"); let contains_e2018 = classes.contains("edition2018"); - let edition_class = if contains_e2015 || contains_e2018 { + let contains_e2021 = classes.contains("edition2021"); + let edition_class = if contains_e2015 || contains_e2018 || contains_e2021 { // the user forced edition, we should not overwrite it "" } else { match edition { Some(RustEdition::E2015) => " edition2015", Some(RustEdition::E2018) => " edition2018", + Some(RustEdition::E2021) => " edition2021", None => "", } }; @@ -1064,4 +1066,28 @@ mod tests { assert_eq!(&*got, *should_be); } } + #[test] + fn add_playground_edition2021() { + let inputs = [ + ("x()", + "
\n#![allow(unused)]\nfn main() {\nx()\n}\n
"), + ("fn main() {}", + "
fn main() {}\n
"), + ("fn main() {}", + "
fn main() {}\n
"), + ("fn main() {}", + "
fn main() {}\n
"), + ]; + for (src, should_be) in &inputs { + let got = add_playground_pre( + src, + &Playground { + editable: true, + ..Playground::default() + }, + Some(RustEdition::E2021), + ); + assert_eq!(&*got, *should_be); + } + } } diff --git a/src/theme/book.js b/src/theme/book.js index 5e386369..4d395821 100644 --- a/src/theme/book.js +++ b/src/theme/book.js @@ -108,16 +108,19 @@ function playground_text(playground) { let text = playground_text(code_block); let classes = code_block.querySelector('code').classList; - let has_2018 = classes.contains("edition2018"); - let edition = has_2018 ? "2018" : "2015"; - + let edition = "2015"; + if(classes.contains("edition2018")) { + edition = "2018"; + } else if(classes.contains("edition2021")) { + edition = "2021"; + } var params = { version: "stable", optimize: "0", code: text, edition: edition }; - + alert(params.edition); if (text.indexOf("#![feature") !== -1) { params.version = "nightly"; }