Fail on empty label name (#5776)

This commit is contained in:
Said Aroua 2025-05-25 15:02:34 +02:00
parent 2a258a0c38
commit c38558d9db

View File

@ -1,7 +1,10 @@
use ecow::{eco_format, EcoString};
use typst_utils::{PicoStr, ResolvedPicoStr};
use crate::foundations::{func, scope, ty, Repr, Str};
use crate::{
diag::StrResult,
foundations::{bail, func, scope, ty, Repr, Str},
};
/// A label for an element.
///
@ -27,7 +30,8 @@ use crate::foundations::{func, scope, ty, Repr, Str};
/// # Syntax
/// This function also has dedicated syntax: You can create a label by enclosing
/// its name in angle brackets. This works both in markup and code. A label's
/// name can contain letters, numbers, `_`, `-`, `:`, and `.`.
/// name can contain letters, numbers, `_`, `-`, `:`, and `.`. Empty label names
/// get rejected.
///
/// Note that there is a syntactical difference when using the dedicated syntax
/// for this function. In the code below, the `[<a>]` terminates the heading and
@ -67,13 +71,17 @@ impl Label {
#[scope]
impl Label {
/// Creates a label from a string.
/// Creates a label from a string. Fails for empty strings.
#[func(constructor)]
pub fn construct(
/// The name of the label.
name: Str,
) -> Label {
Self(PicoStr::intern(name.as_str()))
) -> StrResult<Label> {
if name.is_empty() {
bail!("expected non-empty label name");
}
Ok(Self(PicoStr::intern(name.as_str())))
}
}