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.
This commit is contained in:
Eric Huss 2025-08-09 16:32:13 -07:00
parent 1d1274e53a
commit c25e866796
3 changed files with 32 additions and 25 deletions

View file

@ -180,7 +180,14 @@ impl Display for Chapter {
/// A section number like "1.2.3", basically just a newtype'd `Vec<u32>` with
/// a pretty `Display` impl.
#[derive(Debug, PartialEq, Clone, Default, Serialize, Deserialize)]
pub struct SectionNumber(pub Vec<u32>);
pub struct SectionNumber(Vec<u32>);
impl SectionNumber {
/// Creates a new [`SectionNumber`].
pub fn new(numbers: impl Into<Vec<u32>>) -> SectionNumber {
SectionNumber(numbers.into())
}
}
impl Display for SectionNumber {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {

View file

@ -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")],

View file

@ -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,
})
};