From c25e866796d6183850ded6a62ce5d980d832ca79 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 9 Aug 2025 16:32:13 -0700 Subject: [PATCH] Make SectionNumber field private This removes the `pub` status of the SectionNumber field. The intent is to make this potentially extensible in the future if we decide to add more fields, or change its internal representation. With the existence of the deref impls, generally this change shouldn't be visible except for the constructor, which hopefully shouldn't be too cumbersome to use `SectionNumber::new` instead. --- crates/mdbook-core/src/book.rs | 9 ++++++- crates/mdbook-driver/src/load.rs | 4 +-- crates/mdbook-summary/src/lib.rs | 44 ++++++++++++++++---------------- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/crates/mdbook-core/src/book.rs b/crates/mdbook-core/src/book.rs index 0f8048bc..040612df 100644 --- a/crates/mdbook-core/src/book.rs +++ b/crates/mdbook-core/src/book.rs @@ -180,7 +180,14 @@ impl Display for Chapter { /// A section number like "1.2.3", basically just a newtype'd `Vec` with /// a pretty `Display` impl. #[derive(Debug, PartialEq, Clone, Default, Serialize, Deserialize)] -pub struct SectionNumber(pub Vec); +pub struct SectionNumber(Vec); + +impl SectionNumber { + /// Creates a new [`SectionNumber`]. + pub fn new(numbers: impl Into>) -> SectionNumber { + SectionNumber(numbers.into()) + } +} impl Display for SectionNumber { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { diff --git a/crates/mdbook-driver/src/load.rs b/crates/mdbook-driver/src/load.rs index 2afedc74..77b1a41c 100644 --- a/crates/mdbook-driver/src/load.rs +++ b/crates/mdbook-driver/src/load.rs @@ -194,7 +194,7 @@ And here is some \ .unwrap(); let mut second = Link::new("Nested Chapter 1", &second_path); - second.number = Some(SectionNumber(vec![1, 2])); + second.number = Some(SectionNumber::new([1, 2])); root.nested_items.push(second.clone().into()); root.nested_items.push(SummaryItem::Separator); @@ -255,7 +255,7 @@ And here is some \ let nested = Chapter { name: String::from("Nested Chapter 1"), content: String::from("Hello World!"), - number: Some(SectionNumber(vec![1, 2])), + number: Some(SectionNumber::new([1, 2])), path: Some(PathBuf::from("second.md")), source_path: Some(PathBuf::from("second.md")), parent_names: vec![String::from("Chapter 1")], diff --git a/crates/mdbook-summary/src/lib.rs b/crates/mdbook-summary/src/lib.rs index f0aad211..f34488b7 100644 --- a/crates/mdbook-summary/src/lib.rs +++ b/crates/mdbook-summary/src/lib.rs @@ -536,7 +536,7 @@ impl<'a> SummaryParser<'a> { let mut link = self.parse_link(dest_url.to_string()); let mut number = parent.clone(); - number.0.push(num_existing_items as u32 + 1); + number.push(num_existing_items as u32 + 1); trace!( "Found chapter: {} {} ({})", number, @@ -602,7 +602,7 @@ fn update_section_numbers(sections: &mut [SummaryItem], level: usize, by: u32) { for section in sections { if let SummaryItem::Link(ref mut link) = *section { if let Some(ref mut number) = link.number { - number.0[level] += by; + number[level] += by; } update_section_numbers(&mut link.nested_items, level, by); @@ -779,7 +779,7 @@ mod tests { let link = Link { name: String::from("First"), location: Some(PathBuf::from("./first.md")), - number: Some(SectionNumber(vec![1])), + number: Some(SectionNumber::new([1])), ..Default::default() }; let should_be = vec![SummaryItem::Link(link)]; @@ -800,18 +800,18 @@ mod tests { SummaryItem::Link(Link { name: String::from("First"), location: Some(PathBuf::from("./first.md")), - number: Some(SectionNumber(vec![1])), + number: Some(SectionNumber::new([1])), nested_items: vec![SummaryItem::Link(Link { name: String::from("Nested"), location: Some(PathBuf::from("./nested.md")), - number: Some(SectionNumber(vec![1, 1])), + number: Some(SectionNumber::new([1, 1])), nested_items: Vec::new(), })], }), SummaryItem::Link(Link { name: String::from("Second"), location: Some(PathBuf::from("./second.md")), - number: Some(SectionNumber(vec![2])), + number: Some(SectionNumber::new([2])), nested_items: Vec::new(), }), ]; @@ -832,13 +832,13 @@ mod tests { SummaryItem::Link(Link { name: String::from("First"), location: Some(PathBuf::from("./first.md")), - number: Some(SectionNumber(vec![1])), + number: Some(SectionNumber::new([1])), nested_items: Vec::new(), }), SummaryItem::Link(Link { name: String::from("Second"), location: Some(PathBuf::from("./second.md")), - number: Some(SectionNumber(vec![2])), + number: Some(SectionNumber::new([2])), nested_items: Vec::new(), }), ]; @@ -860,24 +860,24 @@ mod tests { SummaryItem::Link(Link { name: String::from("First"), location: Some(PathBuf::from("./first.md")), - number: Some(SectionNumber(vec![1])), + number: Some(SectionNumber::new([1])), nested_items: Vec::new(), }), SummaryItem::Link(Link { name: String::from("Second"), location: Some(PathBuf::from("./second.md")), - number: Some(SectionNumber(vec![2])), + number: Some(SectionNumber::new([2])), nested_items: Vec::new(), }), SummaryItem::PartTitle(String::from("Title 2")), SummaryItem::Link(Link { name: String::from("Third"), location: Some(PathBuf::from("./third.md")), - number: Some(SectionNumber(vec![3])), + number: Some(SectionNumber::new([3])), nested_items: vec![SummaryItem::Link(Link { name: String::from("Fourth"), location: Some(PathBuf::from("./fourth.md")), - number: Some(SectionNumber(vec![3, 1])), + number: Some(SectionNumber::new([3, 1])), nested_items: Vec::new(), })], }), @@ -900,13 +900,13 @@ mod tests { SummaryItem::Link(Link { name: String::from("First"), location: Some(PathBuf::from("./first.md")), - number: Some(SectionNumber(vec![1])), + number: Some(SectionNumber::new([1])), nested_items: Vec::new(), }), SummaryItem::Link(Link { name: String::from("Second"), location: Some(PathBuf::from("./second.md")), - number: Some(SectionNumber(vec![2])), + number: Some(SectionNumber::new([2])), nested_items: Vec::new(), }), ]; @@ -928,7 +928,7 @@ mod tests { let should_be = vec![SummaryItem::Link(Link { name: String::from("Empty"), location: None, - number: Some(SectionNumber(vec![1])), + number: Some(SectionNumber::new([1])), nested_items: Vec::new(), })]; @@ -946,21 +946,21 @@ mod tests { SummaryItem::Link(Link { name: String::from("First"), location: Some(PathBuf::from("./first.md")), - number: Some(SectionNumber(vec![1])), + number: Some(SectionNumber::new([1])), nested_items: Vec::new(), }), SummaryItem::Separator, SummaryItem::Link(Link { name: String::from("Second"), location: Some(PathBuf::from("./second.md")), - number: Some(SectionNumber(vec![2])), + number: Some(SectionNumber::new([2])), nested_items: Vec::new(), }), SummaryItem::Separator, SummaryItem::Link(Link { name: String::from("Third"), location: Some(PathBuf::from("./third.md")), - number: Some(SectionNumber(vec![3])), + number: Some(SectionNumber::new([3])), nested_items: Vec::new(), }), ]; @@ -981,7 +981,7 @@ mod tests { let should_be = vec![SummaryItem::Link(Link { name: String::from("Chapter title"), location: Some(PathBuf::from("./chapter.md")), - number: Some(SectionNumber(vec![1])), + number: Some(SectionNumber::new([1])), nested_items: Vec::new(), })]; @@ -1000,13 +1000,13 @@ mod tests { SummaryItem::Link(Link { name: String::from("test1"), location: Some(PathBuf::from("./test link1.md")), - number: Some(SectionNumber(vec![1])), + number: Some(SectionNumber::new([1])), nested_items: Vec::new(), }), SummaryItem::Link(Link { name: String::from("test2"), location: Some(PathBuf::from("./test link2.md")), - number: Some(SectionNumber(vec![2])), + number: Some(SectionNumber::new([2])), nested_items: Vec::new(), }), ]; @@ -1089,7 +1089,7 @@ mod tests { SummaryItem::Link(Link { name: String::from(name), location: Some(PathBuf::from(location)), - number: Some(SectionNumber(numbers.to_vec())), + number: Some(SectionNumber::new(numbers)), nested_items, }) };