Skip to content

Commit 8befcc7

Browse files
authored
Merge pull request #2779 from ehuss/non_exhaustive
Switch all public types to non_exhaustive
2 parents 1d1274e + 5956092 commit 8befcc7

File tree

18 files changed

+121
-146
lines changed

18 files changed

+121
-146
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ members = [
99
all = { level = "allow", priority = -2 }
1010
correctness = { level = "warn", priority = -1 }
1111
complexity = { level = "warn", priority = -1 }
12+
exhaustive_enums = "warn"
13+
exhaustive_structs = "warn"
14+
manual_non_exhaustive = "warn"
1215

1316
[workspace.lints.rust]
1417
missing_docs = "warn"

crates/mdbook-core/src/book.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ use std::path::PathBuf;
1616
/// [`iter()`]: #method.iter
1717
/// [`for_each_mut()`]: #method.for_each_mut
1818
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
19+
#[non_exhaustive]
1920
pub struct Book {
2021
/// The sections in this book.
2122
pub sections: Vec<BookItem>,
22-
__non_exhaustive: (),
2323
}
2424

2525
impl Book {
@@ -30,10 +30,7 @@ impl Book {
3030

3131
/// Creates a new book with the given items.
3232
pub fn new_with_items(items: Vec<BookItem>) -> Book {
33-
Book {
34-
sections: items,
35-
__non_exhaustive: (),
36-
}
33+
Book { sections: items }
3734
}
3835

3936
/// Get a depth-first iterator over the items in the book.
@@ -81,6 +78,7 @@ where
8178

8279
/// Enum representing any type of item which can be added to a book.
8380
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
81+
#[non_exhaustive]
8482
pub enum BookItem {
8583
/// A nested chapter.
8684
Chapter(Chapter),
@@ -99,6 +97,7 @@ impl From<Chapter> for BookItem {
9997
/// The representation of a "chapter", usually mapping to a single file on
10098
/// disk however it may contain multiple sub-chapters.
10199
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
100+
#[non_exhaustive]
102101
pub struct Chapter {
103102
/// The chapter's name.
104103
pub name: String,
@@ -180,7 +179,14 @@ impl Display for Chapter {
180179
/// A section number like "1.2.3", basically just a newtype'd `Vec<u32>` with
181180
/// a pretty `Display` impl.
182181
#[derive(Debug, PartialEq, Clone, Default, Serialize, Deserialize)]
183-
pub struct SectionNumber(pub Vec<u32>);
182+
pub struct SectionNumber(Vec<u32>);
183+
184+
impl SectionNumber {
185+
/// Creates a new [`SectionNumber`].
186+
pub fn new(numbers: impl Into<Vec<u32>>) -> SectionNumber {
187+
SectionNumber(numbers.into())
188+
}
189+
}
184190

185191
impl Display for SectionNumber {
186192
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {

crates/mdbook-core/src/config.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ use toml::value::Table;
6464
/// The overall configuration object for MDBook, essentially an in-memory
6565
/// representation of `book.toml`.
6666
#[derive(Debug, Clone, PartialEq)]
67+
#[non_exhaustive]
6768
pub struct Config {
6869
/// Metadata about the book.
6970
pub book: BookConfig,
@@ -386,6 +387,7 @@ fn is_legacy_format(table: &Value) -> bool {
386387
/// loading it from disk.
387388
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
388389
#[serde(default, rename_all = "kebab-case")]
390+
#[non_exhaustive]
389391
pub struct BookConfig {
390392
/// The book's title.
391393
pub title: Option<String>,
@@ -429,6 +431,7 @@ impl BookConfig {
429431

430432
/// Text direction to use for HTML output
431433
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
434+
#[non_exhaustive]
432435
pub enum TextDirection {
433436
/// Left to right.
434437
#[serde(rename = "ltr")]
@@ -454,6 +457,7 @@ impl TextDirection {
454457
/// Configuration for the build procedure.
455458
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
456459
#[serde(default, rename_all = "kebab-case")]
460+
#[non_exhaustive]
457461
pub struct BuildConfig {
458462
/// Where to put built artefacts relative to the book's root directory.
459463
pub build_dir: PathBuf,
@@ -481,13 +485,15 @@ impl Default for BuildConfig {
481485
/// Configuration for the Rust compiler(e.g., for playground)
482486
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
483487
#[serde(default, rename_all = "kebab-case")]
488+
#[non_exhaustive]
484489
pub struct RustConfig {
485490
/// Rust edition used in playground
486491
pub edition: Option<RustEdition>,
487492
}
488493

489-
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
490494
/// Rust edition to use for the code.
495+
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
496+
#[non_exhaustive]
491497
pub enum RustEdition {
492498
/// The 2024 edition of Rust
493499
#[serde(rename = "2024")]
@@ -506,6 +512,7 @@ pub enum RustEdition {
506512
/// Configuration for the HTML renderer.
507513
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
508514
#[serde(default, rename_all = "kebab-case")]
515+
#[non_exhaustive]
509516
pub struct HtmlConfig {
510517
/// The theme directory, if specified.
511518
pub theme: Option<PathBuf>,
@@ -625,6 +632,7 @@ impl HtmlConfig {
625632
/// Configuration for how to render the print icon, print.html, and print.css.
626633
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
627634
#[serde(default, rename_all = "kebab-case")]
635+
#[non_exhaustive]
628636
pub struct Print {
629637
/// Whether print support is enabled.
630638
pub enable: bool,
@@ -644,6 +652,7 @@ impl Default for Print {
644652
/// Configuration for how to fold chapters of sidebar.
645653
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
646654
#[serde(default, rename_all = "kebab-case")]
655+
#[non_exhaustive]
647656
pub struct Fold {
648657
/// When off, all folds are open. Default: `false`.
649658
pub enable: bool,
@@ -656,6 +665,7 @@ pub struct Fold {
656665
/// Configuration for tweaking how the HTML renderer handles the playground.
657666
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
658667
#[serde(default, rename_all = "kebab-case")]
668+
#[non_exhaustive]
659669
pub struct Playground {
660670
/// Should playground snippets be editable? Default: `false`.
661671
pub editable: bool,
@@ -685,6 +695,7 @@ impl Default for Playground {
685695
/// Configuration for tweaking how the HTML renderer handles code blocks.
686696
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
687697
#[serde(default, rename_all = "kebab-case")]
698+
#[non_exhaustive]
688699
pub struct Code {
689700
/// A prefix string to hide lines per language (one or more chars).
690701
pub hidelines: HashMap<String, String>,
@@ -693,6 +704,7 @@ pub struct Code {
693704
/// Configuration of the search functionality of the HTML renderer.
694705
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
695706
#[serde(default, rename_all = "kebab-case")]
707+
#[non_exhaustive]
696708
pub struct Search {
697709
/// Enable the search feature. Default: `true`.
698710
pub enable: bool,
@@ -750,6 +762,7 @@ impl Default for Search {
750762
/// Search options for chapters (or paths).
751763
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
752764
#[serde(default, rename_all = "kebab-case")]
765+
#[non_exhaustive]
753766
pub struct SearchChapterSettings {
754767
/// Whether or not indexing is enabled, default `true`.
755768
pub enable: Option<bool>,

crates/mdbook-driver/src/builtin_preprocessors/index.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{path::Path, sync::LazyLock};
88
/// A preprocessor for converting file name `README.md` to `index.md` since
99
/// `README.md` is the de facto index file in markdown-based documentation.
1010
#[derive(Default)]
11+
#[non_exhaustive]
1112
pub struct IndexPreprocessor;
1213

1314
impl IndexPreprocessor {

crates/mdbook-driver/src/builtin_preprocessors/links.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const MAX_LINK_NESTED_DEPTH: usize = 10;
2626
/// - `{{# playground}}` - Insert runnable Rust files
2727
/// - `{{# title}}` - Override \<title\> of a webpage.
2828
#[derive(Default)]
29+
#[non_exhaustive]
2930
pub struct LinkPreprocessor;
3031

3132
impl LinkPreprocessor {

crates/mdbook-driver/src/builtin_renderers/markdown_renderer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ use mdbook_core::utils;
55
use mdbook_renderer::{RenderContext, Renderer};
66
use std::fs;
77

8-
#[derive(Default)]
98
/// A renderer to output the Markdown after the preprocessors have run. Mostly useful
109
/// when debugging preprocessors.
10+
#[derive(Default)]
11+
#[non_exhaustive]
1112
pub struct MarkdownRenderer;
1213

1314
impl MarkdownRenderer {

crates/mdbook-driver/src/load.rs

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ fn load_summary_item<P: AsRef<Path> + Clone>(
9595
SummaryItem::Separator => Ok(BookItem::Separator),
9696
SummaryItem::Link(link) => load_chapter(link, src_dir, parent_names).map(BookItem::Chapter),
9797
SummaryItem::PartTitle(title) => Ok(BookItem::PartTitle(title.clone())),
98+
_ => panic!("SummaryItem {item:?} not covered"),
9899
}
99100
}
100101

@@ -194,7 +195,7 @@ And here is some \
194195
.unwrap();
195196

196197
let mut second = Link::new("Nested Chapter 1", &second_path);
197-
second.number = Some(SectionNumber(vec![1, 2]));
198+
second.number = Some(SectionNumber::new([1, 2]));
198199

199200
root.nested_items.push(second.clone().into());
200201
root.nested_items.push(SummaryItem::Separator);
@@ -252,28 +253,21 @@ And here is some \
252253
fn load_recursive_link_with_separators() {
253254
let (root, temp) = nested_links();
254255

255-
let nested = Chapter {
256-
name: String::from("Nested Chapter 1"),
257-
content: String::from("Hello World!"),
258-
number: Some(SectionNumber(vec![1, 2])),
259-
path: Some(PathBuf::from("second.md")),
260-
source_path: Some(PathBuf::from("second.md")),
261-
parent_names: vec![String::from("Chapter 1")],
262-
sub_items: Vec::new(),
263-
};
264-
let should_be = BookItem::Chapter(Chapter {
265-
name: String::from("Chapter 1"),
266-
content: String::from(DUMMY_SRC),
267-
number: None,
268-
path: Some(PathBuf::from("chapter_1.md")),
269-
source_path: Some(PathBuf::from("chapter_1.md")),
270-
parent_names: Vec::new(),
271-
sub_items: vec![
272-
BookItem::Chapter(nested.clone()),
273-
BookItem::Separator,
274-
BookItem::Chapter(nested),
275-
],
276-
});
256+
let mut nested = Chapter::new(
257+
"Nested Chapter 1",
258+
String::from("Hello World!"),
259+
"second.md",
260+
vec![String::from("Chapter 1")],
261+
);
262+
nested.number = Some(SectionNumber::new([1, 2]));
263+
let mut chapter =
264+
Chapter::new("Chapter 1", String::from(DUMMY_SRC), "chapter_1.md", vec![]);
265+
chapter.sub_items = vec![
266+
BookItem::Chapter(nested.clone()),
267+
BookItem::Separator,
268+
BookItem::Chapter(nested),
269+
];
270+
let should_be = BookItem::Chapter(chapter);
277271

278272
let got = load_summary_item(&SummaryItem::Link(root), temp.path(), Vec::new()).unwrap();
279273
assert_eq!(got, should_be);
@@ -282,17 +276,15 @@ And here is some \
282276
#[test]
283277
fn load_a_book_with_a_single_chapter() {
284278
let (link, temp) = dummy_link();
285-
let summary = Summary {
286-
numbered_chapters: vec![SummaryItem::Link(link)],
287-
..Default::default()
288-
};
289-
let sections = vec![BookItem::Chapter(Chapter {
290-
name: String::from("Chapter 1"),
291-
content: String::from(DUMMY_SRC),
292-
path: Some(PathBuf::from("chapter_1.md")),
293-
source_path: Some(PathBuf::from("chapter_1.md")),
294-
..Default::default()
295-
})];
279+
let mut summary = Summary::default();
280+
summary.numbered_chapters = vec![SummaryItem::Link(link)];
281+
let chapter = Chapter::new(
282+
"Chapter 1",
283+
String::from(DUMMY_SRC),
284+
PathBuf::from("chapter_1.md"),
285+
vec![],
286+
);
287+
let sections = vec![BookItem::Chapter(chapter)];
296288
let should_be = Book::new_with_items(sections);
297289

298290
let got = load_book_from_disk(&summary, temp.path()).unwrap();
@@ -303,16 +295,9 @@ And here is some \
303295
#[test]
304296
fn cant_load_chapters_with_an_empty_path() {
305297
let (_, temp) = dummy_link();
306-
let summary = Summary {
307-
numbered_chapters: vec![SummaryItem::Link(Link {
308-
name: String::from("Empty"),
309-
location: Some(PathBuf::from("")),
310-
..Default::default()
311-
})],
312-
313-
..Default::default()
314-
};
315-
298+
let mut summary = Summary::default();
299+
let link = Link::new("Empty", "");
300+
summary.numbered_chapters = vec![SummaryItem::Link(link)];
316301
let got = load_book_from_disk(&summary, temp.path());
317302
assert!(got.is_err());
318303
}
@@ -323,14 +308,9 @@ And here is some \
323308
let dir = temp.path().join("nested");
324309
fs::create_dir(&dir).unwrap();
325310

326-
let summary = Summary {
327-
numbered_chapters: vec![SummaryItem::Link(Link {
328-
name: String::from("nested"),
329-
location: Some(dir),
330-
..Default::default()
331-
})],
332-
..Default::default()
333-
};
311+
let mut summary = Summary::default();
312+
let link = Link::new("nested", dir);
313+
summary.numbered_chapters = vec![SummaryItem::Link(link)];
334314

335315
let got = load_book_from_disk(&summary, temp.path());
336316
assert!(got.is_err());

crates/mdbook-driver/src/mdbook.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ impl MDBook {
136136
/// BookItem::Chapter(ref chapter) => {},
137137
/// BookItem::Separator => {},
138138
/// BookItem::PartTitle(ref title) => {}
139+
/// _ => {}
139140
/// }
140141
/// }
141142
///
@@ -329,6 +330,7 @@ impl MDBook {
329330
RustEdition::E2024 => {
330331
cmd.args(["--edition", "2024"]);
331332
}
333+
_ => panic!("RustEdition {edition:?} not covered"),
332334
}
333335
}
334336

crates/mdbook-driver/src/mdbook/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ fn config_respects_preprocessor_selection() {
230230

231231
let cfg = Config::from_str(cfg_str).unwrap();
232232

233-
let html_renderer = HtmlHandlebars;
233+
let html_renderer = HtmlHandlebars::default();
234234
let pre = LinkPreprocessor::new();
235235

236236
let should_run = preprocessor_should_run(&pre, &html_renderer, &cfg).unwrap();

0 commit comments

Comments
 (0)