Replace assert with Option, update call sites

This commit is contained in:
Said Aroua 2025-06-13 23:21:57 +02:00
parent 1b50e2333f
commit 79c1d81b10
3 changed files with 16 additions and 6 deletions

View File

@ -205,7 +205,9 @@ impl Eval for ast::Label<'_> {
type Output = Value;
fn eval(self, _: &mut Vm) -> SourceResult<Self::Output> {
Ok(Value::Label(Label::new(PicoStr::intern(self.get()))))
Ok(Value::Label(
Label::new(PicoStr::intern(self.get())).expect("unexpected empty label"),
))
}
}
@ -213,7 +215,8 @@ impl Eval for ast::Ref<'_> {
type Output = Content;
fn eval(self, vm: &mut Vm) -> SourceResult<Self::Output> {
let target = Label::new(PicoStr::intern(self.target()));
let target = Label::new(PicoStr::intern(self.target()))
.expect("unexpected empty reference");
let mut elem = RefElem::new(target);
if let Some(supplement) = self.supplement() {
elem.push_supplement(Smart::Custom(Some(Supplement::Content(

View File

@ -53,9 +53,11 @@ pub struct Label(PicoStr);
impl Label {
/// Creates a label from an interned string.
/// Callers need to ensure the given string is not empty.
pub fn new(name: PicoStr) -> Self {
debug_assert!(name != PicoStr::EMPTY);
Self(name)
pub fn new(name: PicoStr) -> Option<Self> {
match name {
PicoStr::EMPTY => None,
_ => Some(Self(name)),
}
}
/// Resolves the label to a string.

View File

@ -318,7 +318,12 @@ impl Bibliography {
bail!("bibliography key must not be empty");
}
match map.entry(Label::new(PicoStr::intern(key))) {
let label = match Label::new(PicoStr::intern(key)) {
Some(lbl) => lbl,
None => bail!("unexpected error while creating a label"),
};
match map.entry(label) {
indexmap::map::Entry::Vacant(vacant) => {
vacant.insert(entry);
}