Some edition cleanup and fixes.

This commit is contained in:
Eric Huss 2020-04-21 12:21:56 -07:00
parent 255756cfee
commit 28ce8f5ac0
4 changed files with 23 additions and 46 deletions

View File

@ -3,6 +3,8 @@ title = "mdBook Documentation"
description = "Create book from markdown files. Like Gitbook but implemented in Rust" description = "Create book from markdown files. Like Gitbook but implemented in Rust"
authors = ["Mathieu David", "Michael-F-Bryan"] authors = ["Mathieu David", "Michael-F-Bryan"]
language = "en" language = "en"
[rust]
edition = "2018" edition = "2018"
[output.html] [output.html]

View File

@ -59,9 +59,19 @@ language = "en"
### Rust options ### 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 ### Build options

View File

@ -72,7 +72,7 @@ pub struct Config {
pub book: BookConfig, pub book: BookConfig,
/// Information about the build environment. /// Information about the build environment.
pub build: BuildConfig, pub build: BuildConfig,
/// Information passed to the Rust playground /// Information about Rust language support.
pub rust: RustConfig, pub rust: RustConfig,
rest: Value, rest: Value,
} }
@ -340,6 +340,7 @@ impl<'de> Deserialize<'de> for Config {
impl Serialize for Config { impl Serialize for Config {
fn serialize<S: Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> { fn serialize<S: Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> {
use serde::ser::Error; use serde::ser::Error;
// TODO: This should probably be removed and use a derive instead.
let mut table = self.rest.clone(); let mut table = self.rest.clone();
@ -349,8 +350,10 @@ impl Serialize for Config {
return Err(S::Error::custom("Unable to serialize the BookConfig")); 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("book", book_config).expect("unreachable");
table.insert("rust", rust_config).expect("unreachable");
table.serialize(s) table.serialize(s)
} }
} }
@ -449,55 +452,17 @@ pub struct RustConfig {
pub edition: Option<RustEdition>, pub edition: Option<RustEdition>,
} }
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
/// Rust edition to use for the code. /// Rust edition to use for the code.
pub enum RustEdition { pub enum RustEdition {
/// The 2018 edition of Rust /// The 2018 edition of Rust
#[serde(rename = "2018")]
E2018, E2018,
/// The 2015 edition of Rust /// The 2015 edition of Rust
#[serde(rename = "2015")]
E2015, E2015,
} }
impl Serialize for RustEdition {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
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<D>(de: D) -> std::result::Result<Self, D::Error>
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. /// Configuration for the HTML renderer.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")] #[serde(default, rename_all = "kebab-case")]

View File

@ -808,7 +808,7 @@ mod tests {
fn add_playpen_edition2015() { fn add_playpen_edition2015() {
let inputs = [ let inputs = [
("<code class=\"language-rust\">x()</code>", ("<code class=\"language-rust\">x()</code>",
"<pre class=\"playpen\"><code class=\"language-rust edition2015\">\n<span class=\"boring\">#![allow(unused_variables)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}\n</span></code></pre>"), "<pre class=\"playpen\"><code class=\"language-rust edition2015\">\n<span class=\"boring\">#![allow(unused)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}\n</span></code></pre>"),
("<code class=\"language-rust\">fn main() {}</code>", ("<code class=\"language-rust\">fn main() {}</code>",
"<pre class=\"playpen\"><code class=\"language-rust edition2015\">fn main() {}\n</code></pre>"), "<pre class=\"playpen\"><code class=\"language-rust edition2015\">fn main() {}\n</code></pre>"),
("<code class=\"language-rust edition2015\">fn main() {}</code>", ("<code class=\"language-rust edition2015\">fn main() {}</code>",
@ -832,7 +832,7 @@ mod tests {
fn add_playpen_edition2018() { fn add_playpen_edition2018() {
let inputs = [ let inputs = [
("<code class=\"language-rust\">x()</code>", ("<code class=\"language-rust\">x()</code>",
"<pre class=\"playpen\"><code class=\"language-rust edition2018\">\n<span class=\"boring\">#![allow(unused_variables)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}\n</span></code></pre>"), "<pre class=\"playpen\"><code class=\"language-rust edition2018\">\n<span class=\"boring\">#![allow(unused)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}\n</span></code></pre>"),
("<code class=\"language-rust\">fn main() {}</code>", ("<code class=\"language-rust\">fn main() {}</code>",
"<pre class=\"playpen\"><code class=\"language-rust edition2018\">fn main() {}\n</code></pre>"), "<pre class=\"playpen\"><code class=\"language-rust edition2018\">fn main() {}\n</code></pre>"),
("<code class=\"language-rust edition2015\">fn main() {}</code>", ("<code class=\"language-rust edition2015\">fn main() {}</code>",