summaryrefslogtreecommitdiff
path: root/library/src
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-03-27 11:58:17 +0200
committerLaurenz <laurmaedje@gmail.com>2023-03-27 12:12:57 +0200
commita5c88c19b300c64fafd6a19e3f5f2fe6422a7b67 (patch)
tree42192332e277624a35c6e3db568c0ff8196b582a /library/src
parent10d8fd966130f1e60d328b08a7fcce61961d6974 (diff)
Fix enum parsing and allow zero in numberings
Fixes #353.
Diffstat (limited to 'library/src')
-rw-r--r--library/src/layout/enum.rs12
-rw-r--r--library/src/meta/counter.rs14
-rw-r--r--library/src/meta/numbering.rs33
3 files changed, 34 insertions, 25 deletions
diff --git a/library/src/layout/enum.rs b/library/src/layout/enum.rs
index 1be57d4c..ec4fbc18 100644
--- a/library/src/layout/enum.rs
+++ b/library/src/layout/enum.rs
@@ -100,8 +100,8 @@ pub struct EnumElem {
/// [Ahead],
/// )
/// ```
- #[default(NonZeroUsize::ONE)]
- pub start: NonZeroUsize,
+ #[default(1)]
+ pub start: usize,
/// Whether to display the full numbering, including the numbers of
/// all parent enumerations.
@@ -225,7 +225,7 @@ impl Layout for EnumElem {
pub struct EnumItem {
/// The item's number.
#[positional]
- pub number: Option<NonZeroUsize>,
+ pub number: Option<usize>,
/// The item's body.
#[required]
@@ -245,11 +245,11 @@ cast_from_value! {
v: Content => v.to::<Self>().cloned().unwrap_or_else(|| Self::new(v.clone())),
}
-struct Parent(NonZeroUsize);
+struct Parent(usize);
cast_from_value! {
Parent,
- v: NonZeroUsize => Self(v),
+ v: usize => Self(v),
}
cast_to_value! {
@@ -257,7 +257,7 @@ cast_to_value! {
}
impl Fold for Parent {
- type Output = Vec<NonZeroUsize>;
+ type Output = Vec<usize>;
fn fold(self, mut outer: Self::Output) -> Self::Output {
outer.push(self.0);
diff --git a/library/src/meta/counter.rs b/library/src/meta/counter.rs
index 0ebf144c..fb656c76 100644
--- a/library/src/meta/counter.rs
+++ b/library/src/meta/counter.rs
@@ -372,8 +372,8 @@ impl Counter {
) -> SourceResult<EcoVec<(CounterState, NonZeroUsize)>> {
let mut vt = Vt { world, tracer, provider, introspector };
let mut state = CounterState(match &self.0 {
- CounterKey::Selector(_) => smallvec![],
- _ => smallvec![NonZeroUsize::ONE],
+ CounterKey::Selector(_) => smallvec![0],
+ _ => smallvec![1],
});
let mut page = NonZeroUsize::ONE;
let mut stops = eco_vec![(state.clone(), page)];
@@ -506,7 +506,7 @@ pub trait Count {
/// Counts through elements with different levels.
#[derive(Debug, Clone, PartialEq, Hash)]
-pub struct CounterState(pub SmallVec<[NonZeroUsize; 3]>);
+pub struct CounterState(pub SmallVec<[usize; 3]>);
impl CounterState {
/// Advance the counter and return the numbers for the given heading.
@@ -534,13 +534,13 @@ impl CounterState {
}
while self.0.len() < level {
- self.0.push(NonZeroUsize::ONE);
+ self.0.push(1);
}
}
/// Get the first number of the state.
- pub fn first(&self) -> NonZeroUsize {
- self.0.first().copied().unwrap_or(NonZeroUsize::ONE)
+ pub fn first(&self) -> usize {
+ self.0.first().copied().unwrap_or(1)
}
/// Display the counter state with a numbering.
@@ -551,7 +551,7 @@ impl CounterState {
cast_from_value! {
CounterState,
- num: NonZeroUsize => Self(smallvec![num]),
+ num: usize => Self(smallvec![num]),
array: Array => Self(array
.into_iter()
.map(Value::cast)
diff --git a/library/src/meta/numbering.rs b/library/src/meta/numbering.rs
index 6facb833..0827e160 100644
--- a/library/src/meta/numbering.rs
+++ b/library/src/meta/numbering.rs
@@ -62,7 +62,7 @@ pub fn numbering(
/// If `numbering` is a pattern and more numbers than counting symbols are
/// given, the last counting symbol with its prefix is repeated.
#[variadic]
- numbers: Vec<NonZeroUsize>,
+ numbers: Vec<usize>,
) -> Value {
numbering.apply_vm(vm, &numbers)?
}
@@ -78,25 +78,23 @@ pub enum Numbering {
impl Numbering {
/// Apply the pattern to the given numbers.
- pub fn apply_vm(&self, vm: &mut Vm, numbers: &[NonZeroUsize]) -> SourceResult<Value> {
+ pub fn apply_vm(&self, vm: &mut Vm, numbers: &[usize]) -> SourceResult<Value> {
Ok(match self {
Self::Pattern(pattern) => Value::Str(pattern.apply(numbers).into()),
Self::Func(func) => {
- let args = Args::new(
- func.span(),
- numbers.iter().map(|n| Value::Int(n.get() as i64)),
- );
+ let args =
+ Args::new(func.span(), numbers.iter().map(|&n| Value::Int(n as i64)));
func.call_vm(vm, args)?
}
})
}
/// Apply the pattern to the given numbers.
- pub fn apply_vt(&self, vt: &mut Vt, numbers: &[NonZeroUsize]) -> SourceResult<Value> {
+ pub fn apply_vt(&self, vt: &mut Vt, numbers: &[usize]) -> SourceResult<Value> {
Ok(match self {
Self::Pattern(pattern) => Value::Str(pattern.apply(numbers).into()),
Self::Func(func) => {
- func.call_vt(vt, numbers.iter().map(|n| Value::Int(n.get() as i64)))?
+ func.call_vt(vt, numbers.iter().map(|&n| Value::Int(n as i64)))?
}
})
}
@@ -147,7 +145,7 @@ pub struct NumberingPattern {
impl NumberingPattern {
/// Apply the pattern to the given number.
- pub fn apply(&self, numbers: &[NonZeroUsize]) -> EcoString {
+ pub fn apply(&self, numbers: &[usize]) -> EcoString {
let mut fmt = EcoString::new();
let mut numbers = numbers.into_iter();
@@ -179,7 +177,7 @@ impl NumberingPattern {
}
/// Apply only the k-th segment of the pattern to a number.
- pub fn apply_kth(&self, k: usize, number: NonZeroUsize) -> EcoString {
+ pub fn apply_kth(&self, k: usize, number: usize) -> EcoString {
let mut fmt = EcoString::new();
if let Some((prefix, _, _)) = self.pieces.first() {
fmt.push_str(prefix);
@@ -282,13 +280,16 @@ impl NumberingKind {
}
/// Apply the numbering to the given number.
- pub fn apply(self, n: NonZeroUsize, case: Case) -> EcoString {
- let mut n = n.get();
+ pub fn apply(self, mut n: usize, case: Case) -> EcoString {
match self {
Self::Arabic => {
eco_format!("{n}")
}
Self::Letter => {
+ if n == 0 {
+ return '-'.into();
+ }
+
n -= 1;
let mut letters = vec![];
@@ -308,6 +309,10 @@ impl NumberingKind {
String::from_utf8(letters).unwrap().into()
}
Self::Roman => {
+ if n == 0 {
+ return 'N'.into();
+ }
+
// Adapted from Yann Villessuzanne's roman.rs under the
// Unlicense, at https://github.com/linfir/roman.rs/
let mut fmt = EcoString::new();
@@ -347,6 +352,10 @@ impl NumberingKind {
fmt
}
Self::Symbol => {
+ if n == 0 {
+ return '-'.into();
+ }
+
const SYMBOLS: &[char] = &['*', '†', '‡', '§', '¶', '‖'];
let symbol = SYMBOLS[(n - 1) % SYMBOLS.len()];
let amount = ((n - 1) / SYMBOLS.len()) + 1;