mirror of
https://github.com/typst/typst
synced 2025-08-26 04:34:14 +08:00
Fail on empty label name (#5776)
This commit is contained in:
parent
2a258a0c38
commit
c38558d9db
@ -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())))
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user