Skip to content

Commit c25e866

Browse files
committed
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.
1 parent 1d1274e commit c25e866

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

crates/mdbook-core/src/book.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,14 @@ impl Display for Chapter {
180180
/// A section number like "1.2.3", basically just a newtype'd `Vec<u32>` with
181181
/// a pretty `Display` impl.
182182
#[derive(Debug, PartialEq, Clone, Default, Serialize, Deserialize)]
183-
pub struct SectionNumber(pub Vec<u32>);
183+
pub struct SectionNumber(Vec<u32>);
184+
185+
impl SectionNumber {
186+
/// Creates a new [`SectionNumber`].
187+
pub fn new(numbers: impl Into<Vec<u32>>) -> SectionNumber {
188+
SectionNumber(numbers.into())
189+
}
190+
}
184191

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

crates/mdbook-driver/src/load.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ And here is some \
194194
.unwrap();
195195

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

199199
root.nested_items.push(second.clone().into());
200200
root.nested_items.push(SummaryItem::Separator);
@@ -255,7 +255,7 @@ And here is some \
255255
let nested = Chapter {
256256
name: String::from("Nested Chapter 1"),
257257
content: String::from("Hello World!"),
258-
number: Some(SectionNumber(vec![1, 2])),
258+
number: Some(SectionNumber::new([1, 2])),
259259
path: Some(PathBuf::from("second.md")),
260260
source_path: Some(PathBuf::from("second.md")),
261261
parent_names: vec![String::from("Chapter 1")],

crates/mdbook-summary/src/lib.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ impl<'a> SummaryParser<'a> {
536536
let mut link = self.parse_link(dest_url.to_string());
537537

538538
let mut number = parent.clone();
539-
number.0.push(num_existing_items as u32 + 1);
539+
number.push(num_existing_items as u32 + 1);
540540
trace!(
541541
"Found chapter: {} {} ({})",
542542
number,
@@ -602,7 +602,7 @@ fn update_section_numbers(sections: &mut [SummaryItem], level: usize, by: u32) {
602602
for section in sections {
603603
if let SummaryItem::Link(ref mut link) = *section {
604604
if let Some(ref mut number) = link.number {
605-
number.0[level] += by;
605+
number[level] += by;
606606
}
607607

608608
update_section_numbers(&mut link.nested_items, level, by);
@@ -779,7 +779,7 @@ mod tests {
779779
let link = Link {
780780
name: String::from("First"),
781781
location: Some(PathBuf::from("./first.md")),
782-
number: Some(SectionNumber(vec![1])),
782+
number: Some(SectionNumber::new([1])),
783783
..Default::default()
784784
};
785785
let should_be = vec![SummaryItem::Link(link)];
@@ -800,18 +800,18 @@ mod tests {
800800
SummaryItem::Link(Link {
801801
name: String::from("First"),
802802
location: Some(PathBuf::from("./first.md")),
803-
number: Some(SectionNumber(vec![1])),
803+
number: Some(SectionNumber::new([1])),
804804
nested_items: vec![SummaryItem::Link(Link {
805805
name: String::from("Nested"),
806806
location: Some(PathBuf::from("./nested.md")),
807-
number: Some(SectionNumber(vec![1, 1])),
807+
number: Some(SectionNumber::new([1, 1])),
808808
nested_items: Vec::new(),
809809
})],
810810
}),
811811
SummaryItem::Link(Link {
812812
name: String::from("Second"),
813813
location: Some(PathBuf::from("./second.md")),
814-
number: Some(SectionNumber(vec![2])),
814+
number: Some(SectionNumber::new([2])),
815815
nested_items: Vec::new(),
816816
}),
817817
];
@@ -832,13 +832,13 @@ mod tests {
832832
SummaryItem::Link(Link {
833833
name: String::from("First"),
834834
location: Some(PathBuf::from("./first.md")),
835-
number: Some(SectionNumber(vec![1])),
835+
number: Some(SectionNumber::new([1])),
836836
nested_items: Vec::new(),
837837
}),
838838
SummaryItem::Link(Link {
839839
name: String::from("Second"),
840840
location: Some(PathBuf::from("./second.md")),
841-
number: Some(SectionNumber(vec![2])),
841+
number: Some(SectionNumber::new([2])),
842842
nested_items: Vec::new(),
843843
}),
844844
];
@@ -860,24 +860,24 @@ mod tests {
860860
SummaryItem::Link(Link {
861861
name: String::from("First"),
862862
location: Some(PathBuf::from("./first.md")),
863-
number: Some(SectionNumber(vec![1])),
863+
number: Some(SectionNumber::new([1])),
864864
nested_items: Vec::new(),
865865
}),
866866
SummaryItem::Link(Link {
867867
name: String::from("Second"),
868868
location: Some(PathBuf::from("./second.md")),
869-
number: Some(SectionNumber(vec![2])),
869+
number: Some(SectionNumber::new([2])),
870870
nested_items: Vec::new(),
871871
}),
872872
SummaryItem::PartTitle(String::from("Title 2")),
873873
SummaryItem::Link(Link {
874874
name: String::from("Third"),
875875
location: Some(PathBuf::from("./third.md")),
876-
number: Some(SectionNumber(vec![3])),
876+
number: Some(SectionNumber::new([3])),
877877
nested_items: vec![SummaryItem::Link(Link {
878878
name: String::from("Fourth"),
879879
location: Some(PathBuf::from("./fourth.md")),
880-
number: Some(SectionNumber(vec![3, 1])),
880+
number: Some(SectionNumber::new([3, 1])),
881881
nested_items: Vec::new(),
882882
})],
883883
}),
@@ -900,13 +900,13 @@ mod tests {
900900
SummaryItem::Link(Link {
901901
name: String::from("First"),
902902
location: Some(PathBuf::from("./first.md")),
903-
number: Some(SectionNumber(vec![1])),
903+
number: Some(SectionNumber::new([1])),
904904
nested_items: Vec::new(),
905905
}),
906906
SummaryItem::Link(Link {
907907
name: String::from("Second"),
908908
location: Some(PathBuf::from("./second.md")),
909-
number: Some(SectionNumber(vec![2])),
909+
number: Some(SectionNumber::new([2])),
910910
nested_items: Vec::new(),
911911
}),
912912
];
@@ -928,7 +928,7 @@ mod tests {
928928
let should_be = vec![SummaryItem::Link(Link {
929929
name: String::from("Empty"),
930930
location: None,
931-
number: Some(SectionNumber(vec![1])),
931+
number: Some(SectionNumber::new([1])),
932932
nested_items: Vec::new(),
933933
})];
934934

@@ -946,21 +946,21 @@ mod tests {
946946
SummaryItem::Link(Link {
947947
name: String::from("First"),
948948
location: Some(PathBuf::from("./first.md")),
949-
number: Some(SectionNumber(vec![1])),
949+
number: Some(SectionNumber::new([1])),
950950
nested_items: Vec::new(),
951951
}),
952952
SummaryItem::Separator,
953953
SummaryItem::Link(Link {
954954
name: String::from("Second"),
955955
location: Some(PathBuf::from("./second.md")),
956-
number: Some(SectionNumber(vec![2])),
956+
number: Some(SectionNumber::new([2])),
957957
nested_items: Vec::new(),
958958
}),
959959
SummaryItem::Separator,
960960
SummaryItem::Link(Link {
961961
name: String::from("Third"),
962962
location: Some(PathBuf::from("./third.md")),
963-
number: Some(SectionNumber(vec![3])),
963+
number: Some(SectionNumber::new([3])),
964964
nested_items: Vec::new(),
965965
}),
966966
];
@@ -981,7 +981,7 @@ mod tests {
981981
let should_be = vec![SummaryItem::Link(Link {
982982
name: String::from("Chapter title"),
983983
location: Some(PathBuf::from("./chapter.md")),
984-
number: Some(SectionNumber(vec![1])),
984+
number: Some(SectionNumber::new([1])),
985985
nested_items: Vec::new(),
986986
})];
987987

@@ -1000,13 +1000,13 @@ mod tests {
10001000
SummaryItem::Link(Link {
10011001
name: String::from("test1"),
10021002
location: Some(PathBuf::from("./test link1.md")),
1003-
number: Some(SectionNumber(vec![1])),
1003+
number: Some(SectionNumber::new([1])),
10041004
nested_items: Vec::new(),
10051005
}),
10061006
SummaryItem::Link(Link {
10071007
name: String::from("test2"),
10081008
location: Some(PathBuf::from("./test link2.md")),
1009-
number: Some(SectionNumber(vec![2])),
1009+
number: Some(SectionNumber::new([2])),
10101010
nested_items: Vec::new(),
10111011
}),
10121012
];
@@ -1089,7 +1089,7 @@ mod tests {
10891089
SummaryItem::Link(Link {
10901090
name: String::from(name),
10911091
location: Some(PathBuf::from(location)),
1092-
number: Some(SectionNumber(numbers.to_vec())),
1092+
number: Some(SectionNumber::new(numbers)),
10931093
nested_items,
10941094
})
10951095
};

0 commit comments

Comments
 (0)