Skip to main content

format_many

Function format_many 

Source
pub fn format_many(
    style: &CounterStyle,
    values: &[NumberedValue<'_>],
    options: &FormatOptions,
) -> Result<Vec<Segment>, FormatError>
Expand description

Writes several counter values in style as one text and returns the segments of that text: the word, the delimiters, and the values as the counter style’s number writer writes them.

Each value carries the counters it is numbered within (NumberedValue), and the values come in the caller’s order. Under the default writer:

  • values numbered within the same counters, with the same values, and counted in the same scoped instance (or in none) form one group, and the groups are written in the order of their first value in the caller’s list;
  • within a group, the values are sorted, and runs of consecutive values are collapsed into ranges; a range never spans two groups. A counter style whose ranges is off collapses nothing: the values are sorted and each is written separately, 1,2,3;
  • a single run of exactly two consecutive values is written as two values, 1 and 2, not as a range;
  • no value is removed: a value given twice is written twice.

The link marks of the segments are positions in the caller’s list: the first value is 0, regardless of where the sorting and the grouping put it. Each number links to its own value, and the texts that join numbers link to nothing. The word before the numbers links to the first number written when the counter style’s name_in_link is on (the default), and to nothing otherwise; with a single value and name_in_link on, the whole text is one link.

With no value at all, the result is the counter style’s empty join text, written without the word and without delimiters.

§Errors

The same as format: a sub-number at a level the counter style declares no format for, a within-joiner that refers to a counter that is not an ancestor, and a failure to build the content.

§Examples

use flm_core::lang::{build_language, LangInputs};
use flm_core::stdlib::features::numbering::{
    format_many, CounterStyleDecl, CounterValue, FormatOptions, JoinDecl, NumberedValue,
};

let lang = build_language(LangInputs::new());
let mut decl = CounterStyleDecl::new("example.style.item");
decl.join = JoinDecl::compact();
let style = decl.compile(&lang).expect("the style compiles");

let values = [CounterValue::new(3), CounterValue::new(1), CounterValue::new(2)];
let written: Vec<NumberedValue<'_>> = values.iter().map(NumberedValue::new).collect();
let segments = format_many(&style, &written, &FormatOptions::new())
    .expect("the numbers are written");
// Three consecutive values are one range: `1`, `–`, `3`.
assert_eq!(segments.len(), 3);
assert_eq!(segments[0].link(), Some(1));
assert_eq!(segments[1].link(), None);
assert_eq!(segments[2].link(), Some(0));