Skip to content

Commit 28ce8f5

Browse files
committed
Some edition cleanup and fixes.
1 parent 255756c commit 28ce8f5

File tree

4 files changed

+24
-47
lines changed

4 files changed

+24
-47
lines changed

book-example/book.toml

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title = "mdBook Documentation"
33
description = "Create book from markdown files. Like Gitbook but implemented in Rust"
44
authors = ["Mathieu David", "Michael-F-Bryan"]
55
language = "en"
6+
7+
[rust]
68
edition = "2018"
79

810
[output.html]

book-example/src/format/config.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,19 @@ language = "en"
5959

6060
### Rust options
6161

62-
Options for the Rust compiler used for playpen.
63-
64-
- **edition**: Rust edition to use by default for the code snippets. Defaults to `rustdoc` defaults (2015).
62+
Options for the Rust language, relevant to running tests and playground
63+
integration.
64+
65+
- **edition**: Rust edition to use by default for the code snippets. Default
66+
is "2015". Individual code blocks can be controlled with the `edition2015`
67+
or `edition2018` annotations, such as:
68+
69+
~~~text
70+
```rust,edition2015
71+
// This only works in 2015.
72+
let try = true;
73+
```
74+
~~~
6575

6676
### Build options
6777

src/config.rs

+7-42
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub struct Config {
7272
pub book: BookConfig,
7373
/// Information about the build environment.
7474
pub build: BuildConfig,
75-
/// Information passed to the Rust playground
75+
/// Information about Rust language support.
7676
pub rust: RustConfig,
7777
rest: Value,
7878
}
@@ -340,6 +340,7 @@ impl<'de> Deserialize<'de> for Config {
340340
impl Serialize for Config {
341341
fn serialize<S: Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> {
342342
use serde::ser::Error;
343+
// TODO: This should probably be removed and use a derive instead.
343344

344345
let mut table = self.rest.clone();
345346

@@ -349,8 +350,10 @@ impl Serialize for Config {
349350
return Err(S::Error::custom("Unable to serialize the BookConfig"));
350351
}
351352
};
353+
let rust_config = Value::try_from(&self.rust).expect("should always be serializable");
352354

353355
table.insert("book", book_config).expect("unreachable");
356+
table.insert("rust", rust_config).expect("unreachable");
354357
table.serialize(s)
355358
}
356359
}
@@ -449,55 +452,17 @@ pub struct RustConfig {
449452
pub edition: Option<RustEdition>,
450453
}
451454

452-
#[derive(Debug, Copy, Clone, PartialEq)]
455+
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
453456
/// Rust edition to use for the code.
454457
pub enum RustEdition {
455458
/// The 2018 edition of Rust
459+
#[serde(rename = "2018")]
456460
E2018,
457461
/// The 2015 edition of Rust
462+
#[serde(rename = "2015")]
458463
E2015,
459464
}
460465

461-
impl Serialize for RustEdition {
462-
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
463-
where
464-
S: Serializer,
465-
{
466-
match self {
467-
RustEdition::E2015 => serializer.serialize_str("2015"),
468-
RustEdition::E2018 => serializer.serialize_str("2018"),
469-
}
470-
}
471-
}
472-
473-
impl<'de> Deserialize<'de> for RustEdition {
474-
fn deserialize<D>(de: D) -> std::result::Result<Self, D::Error>
475-
where
476-
D: Deserializer<'de>,
477-
{
478-
use serde::de::Error;
479-
480-
let raw = Value::deserialize(de)?;
481-
482-
let edition = match raw {
483-
Value::String(s) => s,
484-
_ => {
485-
return Err(D::Error::custom("Rust edition should be a string"));
486-
}
487-
};
488-
489-
let edition = match edition.as_str() {
490-
"2018" => RustEdition::E2018,
491-
"2015" => RustEdition::E2015,
492-
e => {
493-
return Err(D::Error::custom(format!("Unknown Rust edition: {}", e)));
494-
}
495-
};
496-
497-
Ok(edition)
498-
}
499-
}
500-
501466
/// Configuration for the HTML renderer.
502467
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
503468
#[serde(default, rename_all = "kebab-case")]

src/renderer/html_handlebars/hbs_renderer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ mod tests {
808808
fn add_playpen_edition2015() {
809809
let inputs = [
810810
("<code class=\"language-rust\">x()</code>",
811-
"<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>"),
811+
"<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>"),
812812
("<code class=\"language-rust\">fn main() {}</code>",
813813
"<pre class=\"playpen\"><code class=\"language-rust edition2015\">fn main() {}\n</code></pre>"),
814814
("<code class=\"language-rust edition2015\">fn main() {}</code>",
@@ -832,7 +832,7 @@ mod tests {
832832
fn add_playpen_edition2018() {
833833
let inputs = [
834834
("<code class=\"language-rust\">x()</code>",
835-
"<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>"),
835+
"<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>"),
836836
("<code class=\"language-rust\">fn main() {}</code>",
837837
"<pre class=\"playpen\"><code class=\"language-rust edition2018\">fn main() {}\n</code></pre>"),
838838
("<code class=\"language-rust edition2015\">fn main() {}</code>",

0 commit comments

Comments
 (0)