Skip to content

Commit a7b3aa0

Browse files
GabrielMajeriDylan-DPC
authored andcommitted
Add support for Rust edition 2018 in playpens (#1086)
* Add support for Rust edition 2018 in playpens * Add Rust edition support to rustdoc * Run rustfmt * Fix enum variant reference
1 parent a9160ac commit a7b3aa0

File tree

6 files changed

+106
-22
lines changed

6 files changed

+106
-22
lines changed

book-example/book.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mathjax-support = true
1010
[output.html.playpen]
1111
editable = true
1212
line-numbers = true
13+
edition = "2018"
1314

1415
[output.html.search]
1516
limit-results = 20

book-example/src/format/config.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ The following configuration options are available:
178178
an icon link will be output in the menu bar of the book.
179179
- **git-repository-icon:** The FontAwesome icon class to use for the git
180180
repository link. Defaults to `fa-github`.
181-
181+
182182
Available configuration options for the `[output.html.fold]` table:
183183

184184
- **enable:** Enable section-folding. When off, all folds are open.
@@ -193,6 +193,7 @@ Available configuration options for the `[output.html.playpen]` table:
193193
- **copy-js:** Copy JavaScript files for the editor to the output directory.
194194
Defaults to `true`.
195195
- **line-numbers** Display line numbers on editable sections of code. Requires both `editable` and `copy-js` to be `true`. Defaults to `false`.
196+
- **edition**: Rust edition to use by default for the code snippets. Defaults to `2015`.
196197

197198
[Ace]: https://ace.c9.io/
198199

src/book/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::preprocess::{
2727
use crate::renderer::{CmdRenderer, HtmlHandlebars, MarkdownRenderer, RenderContext, Renderer};
2828
use crate::utils;
2929

30-
use crate::config::Config;
30+
use crate::config::{Config, RustEdition};
3131

3232
/// The object used to manage and build a book.
3333
pub struct MDBook {
@@ -262,11 +262,17 @@ impl MDBook {
262262
let mut tmpf = utils::fs::create_file(&path)?;
263263
tmpf.write_all(ch.content.as_bytes())?;
264264

265-
let output = Command::new("rustdoc")
266-
.arg(&path)
267-
.arg("--test")
268-
.args(&library_args)
269-
.output()?;
265+
let mut cmd = Command::new("rustdoc");
266+
267+
cmd.arg(&path).arg("--test").args(&library_args);
268+
269+
if let Some(html_cfg) = self.config.html_config() {
270+
if html_cfg.playpen.edition == RustEdition::E2018 {
271+
cmd.args(&["--edition", "2018"]);
272+
}
273+
}
274+
275+
let output = cmd.output()?;
270276

271277
if !output.status.success() {
272278
bail!(ErrorKind::Subprocess(

src/config.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,8 @@ pub struct Playpen {
513513
pub copy_js: bool,
514514
/// Display line numbers on playpen snippets. Default: `false`.
515515
pub line_numbers: bool,
516+
/// Rust edition to use for the code. Default: `2015`.
517+
pub edition: RustEdition,
516518
}
517519

518520
impl Default for Playpen {
@@ -522,10 +524,66 @@ impl Default for Playpen {
522524
copyable: true,
523525
copy_js: true,
524526
line_numbers: false,
527+
edition: RustEdition::E2015,
525528
}
526529
}
527530
}
528531

532+
#[derive(Debug, Clone, PartialEq)]
533+
/// The edition of Rust used in a playpen code snippet
534+
pub enum RustEdition {
535+
/// The 2018 edition of Rust
536+
E2018,
537+
/// The 2015 edition of Rust
538+
E2015,
539+
}
540+
541+
impl Default for RustEdition {
542+
fn default() -> Self {
543+
RustEdition::E2015
544+
}
545+
}
546+
547+
impl Serialize for RustEdition {
548+
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
549+
where
550+
S: Serializer,
551+
{
552+
match self {
553+
RustEdition::E2015 => serializer.serialize_str("2015"),
554+
RustEdition::E2018 => serializer.serialize_str("2018"),
555+
}
556+
}
557+
}
558+
559+
impl<'de> Deserialize<'de> for RustEdition {
560+
fn deserialize<D>(de: D) -> std::result::Result<Self, D::Error>
561+
where
562+
D: Deserializer<'de>,
563+
{
564+
use serde::de::Error;
565+
566+
let raw = Value::deserialize(de)?;
567+
568+
let edition = match raw {
569+
Value::String(s) => s,
570+
_ => {
571+
return Err(D::Error::custom("Rust edition should be a string"));
572+
}
573+
};
574+
575+
let edition = match edition.as_str() {
576+
"2018" => RustEdition::E2018,
577+
"2015" => RustEdition::E2015,
578+
_ => {
579+
return Err(D::Error::custom("Unknown Rust edition"));
580+
}
581+
};
582+
583+
Ok(edition)
584+
}
585+
}
586+
529587
/// Configuration of the search functionality of the HTML renderer.
530588
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
531589
#[serde(default, rename_all = "kebab-case")]
@@ -630,6 +688,7 @@ mod tests {
630688
[output.html.playpen]
631689
editable = true
632690
editor = "ace"
691+
edition = "2018"
633692
634693
[preprocessor.first]
635694
@@ -658,6 +717,7 @@ mod tests {
658717
copyable: true,
659718
copy_js: true,
660719
line_numbers: false,
720+
edition: RustEdition::E2018,
661721
};
662722
let html_should_be = HtmlConfig {
663723
curly_quotes: true,

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::book::{Book, BookItem};
2-
use crate::config::{Config, HtmlConfig, Playpen};
2+
use crate::config::{Config, HtmlConfig, Playpen, RustEdition};
33
use crate::errors::*;
44
use crate::renderer::html_handlebars::helpers;
55
use crate::renderer::{RenderContext, Renderer};
@@ -608,14 +608,27 @@ fn add_playpen_pre(html: &str, playpen_config: &Playpen) -> String {
608608
let classes = &caps[2];
609609
let code = &caps[3];
610610

611-
if classes.contains("language-rust") {
612-
if (!classes.contains("ignore") && !classes.contains("noplaypen"))
613-
|| classes.contains("mdbook-runnable")
614-
{
615-
// wrap the contents in an external pre block
616-
format!(
617-
"<pre class=\"playpen\"><code class=\"{}\">{}</code></pre>",
618-
classes,
611+
612+
if (classes.contains("language-rust")
613+
&& !classes.contains("ignore")
614+
&& !classes.contains("noplaypen"))
615+
|| classes.contains("mdbook-runnable")
616+
{
617+
let mut classes = classes.to_string();
618+
match playpen_config.edition {
619+
RustEdition::E2018 => classes += " edition2018",
620+
_ => (),
621+
}
622+
623+
// wrap the contents in an external pre block
624+
format!(
625+
"<pre class=\"playpen\"><code class=\"{}\">{}</code></pre>",
626+
classes,
627+
{
628+
let content: Cow<'_, str> = if playpen_config.editable
629+
&& classes.contains("editable")
630+
|| text.contains("fn main")
631+
|| text.contains("quick_main!")
619632
{
620633
let content: Cow<'_, str> = if playpen_config.editable
621634
&& classes.contains("editable")

tests/rendered_output.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -490,12 +490,15 @@ fn markdown_options() {
490490
"<td>bim</td>",
491491
],
492492
);
493-
assert_contains_strings(&path, &[
494-
r##"<sup class="footnote-reference"><a href="#1">1</a></sup>"##,
495-
r##"<sup class="footnote-reference"><a href="#word">2</a></sup>"##,
496-
r##"<div class="footnote-definition" id="1"><sup class="footnote-definition-label">1</sup>"##,
497-
r##"<div class="footnote-definition" id="word"><sup class="footnote-definition-label">2</sup>"##,
498-
]);
493+
assert_contains_strings(
494+
&path,
495+
&[
496+
r##"<sup class="footnote-reference"><a href="#1">1</a></sup>"##,
497+
r##"<sup class="footnote-reference"><a href="#word">2</a></sup>"##,
498+
r##"<div class="footnote-definition" id="1"><sup class="footnote-definition-label">1</sup>"##,
499+
r##"<div class="footnote-definition" id="word"><sup class="footnote-definition-label">2</sup>"##,
500+
],
501+
);
499502
assert_contains_strings(&path, &["<del>strikethrough example</del>"]);
500503
assert_contains_strings(
501504
&path,

0 commit comments

Comments
 (0)