mirror of
https://github.com/typst/typst
synced 2025-08-25 20:24:14 +08:00
Replace assert with Option, update call sites
This commit is contained in:
parent
1b50e2333f
commit
79c1d81b10
@ -205,7 +205,9 @@ impl Eval for ast::Label<'_> {
|
|||||||
type Output = Value;
|
type Output = Value;
|
||||||
|
|
||||||
fn eval(self, _: &mut Vm) -> SourceResult<Self::Output> {
|
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;
|
type Output = Content;
|
||||||
|
|
||||||
fn eval(self, vm: &mut Vm) -> SourceResult<Self::Output> {
|
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);
|
let mut elem = RefElem::new(target);
|
||||||
if let Some(supplement) = self.supplement() {
|
if let Some(supplement) = self.supplement() {
|
||||||
elem.push_supplement(Smart::Custom(Some(Supplement::Content(
|
elem.push_supplement(Smart::Custom(Some(Supplement::Content(
|
||||||
|
@ -53,9 +53,11 @@ pub struct Label(PicoStr);
|
|||||||
impl Label {
|
impl Label {
|
||||||
/// Creates a label from an interned string.
|
/// Creates a label from an interned string.
|
||||||
/// Callers need to ensure the given string is not empty.
|
/// Callers need to ensure the given string is not empty.
|
||||||
pub fn new(name: PicoStr) -> Self {
|
pub fn new(name: PicoStr) -> Option<Self> {
|
||||||
debug_assert!(name != PicoStr::EMPTY);
|
match name {
|
||||||
Self(name)
|
PicoStr::EMPTY => None,
|
||||||
|
_ => Some(Self(name)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolves the label to a string.
|
/// Resolves the label to a string.
|
||||||
|
@ -318,7 +318,12 @@ impl Bibliography {
|
|||||||
bail!("bibliography key must not be empty");
|
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) => {
|
indexmap::map::Entry::Vacant(vacant) => {
|
||||||
vacant.insert(entry);
|
vacant.insert(entry);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user