ferri-pdf 0.1.6

HTML and CSS to PDF renderer for print, in pure Rust
Documentation
use crate::style::{
    GeneratedContent, StyleError,
    box_values::unsupported,
    component::Component,
    grammar::{Keyword, Label, Production},
};
use std::sync::{Arc, LazyLock};

#[derive(Clone, Debug, PartialEq, serde::Serialize)]
pub(crate) struct StringAssignment {
    pub name: Arc<str>,
    pub content: GeneratedContent,
}

static EMPTY_ASSIGNMENTS: LazyLock<Arc<[StringAssignment]>> = LazyLock::new(Arc::default);

pub(crate) fn empty() -> Arc<[StringAssignment]> {
    EMPTY_ASSIGNMENTS.clone()
}

pub(crate) fn assignments(
    value: &Component,
    scope: &crate::style::counter_style::Scope<'_>,
) -> Result<Arc<[StringAssignment]>, StyleError> {
    if value.keyword() == Some(Keyword::None) {
        return Ok(empty());
    }
    let mut parts = Vec::new();
    assignment_parts(value, &mut parts);
    let (pairs, remainder) = parts.as_chunks::<2>();
    let assignments = pairs
        .iter()
        .map(|[name, content]| {
            let Component::Ident(name) = name.unwrap() else {
                return Err(unsupported(name));
            };
            let Component::Reference(Label::Production(Production::NamedStringContent), content) =
                content
            else {
                return Err(unsupported(content));
            };
            Ok(StringAssignment {
                name: Arc::from(name.as_ref()),
                content: GeneratedContent::from_list(content, scope, None)?,
            })
        })
        .collect::<Result<Vec<_>, StyleError>>()?;
    if !remainder.is_empty() || assignments.is_empty() {
        return Err(unsupported(value));
    }
    Ok(assignments.into())
}

fn assignment_parts<'a>(value: &'a Component, parts: &mut Vec<&'a Component>) {
    match value {
        Component::Reference(Label::Production(Production::NamedStringContent), _) => {
            parts.push(value);
        }
        Component::Reference(_, inner) => assignment_parts(inner, parts),
        Component::Group(items) => {
            for item in items {
                assignment_parts(item, parts);
            }
        }
        Component::Delim(',') => {}
        _ => parts.push(value),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::style::{
        QuoteDepth, Quotes,
        counter_style::CounterStyle,
        grammar::Property,
        matcher::{ValueContext, match_value},
    };

    const TEST_CONTENT_LIMIT: usize = 1024;

    fn parse(source: &str) -> Arc<[StringAssignment]> {
        let component = match_value(
            Property::StringSet.data().root,
            source,
            &mut 0,
            ValueContext::Element,
        )
        .expect("string-set grammar must match within its work budget")
        .unwrap_or_else(|| panic!("invalid string-set test input: {source}"));
        assignments(
            &component,
            &crate::style::counter_style::Scope::predefined(),
        )
        .unwrap_or_else(|error| panic!("cannot compute {source}: {error}"))
    }

    #[test]
    fn assignment_lists_keep_names_order_empty_values_and_counter_arguments() {
        let assignments = parse(
            "heading 'Chapter ' counter(chapter, upper-roman), footer counters(section, '.', lower-alpha), heading ''",
        );
        assert_eq!(
            assignments
                .iter()
                .map(|assignment| assignment.name.as_ref())
                .collect::<Vec<_>>(),
            ["heading", "footer", "heading"]
        );
        let values = assignments
            .iter()
            .map(|assignment| {
                assignment
                    .content
                    .element_text_with_counters(
                        TEST_CONTENT_LIMIT,
                        &mut |request| match request.name.as_ref() {
                            "chapter" => {
                                assert_eq!(request.style, CounterStyle::predefined("upper-roman"));
                                assert_eq!(request.separator, None);
                                Ok(request.style.representation(4))
                            }
                            "section" => {
                                assert_eq!(request.style, CounterStyle::predefined("lower-alpha"));
                                assert_eq!(request.separator.as_deref(), Some("."));
                                Ok("a.b".to_owned().into())
                            }
                            name => panic!("unexpected counter request {name}"),
                        },
                        &Quotes::default(),
                        &mut QuoteDepth::default(),
                    )
                    .expect("assignment text must preserve its counter expression")
            })
            .collect::<Vec<_>>();
        assert_eq!(values, ["Chapter IV", "a.b", ""]);
        assert!(parse("none").is_empty());
    }
    #[test]
    fn assignment_binding_captures_element_values_but_retains_page_counters() {
        let assignments =
            parse("heading content(before) counter(chapter) '/' counter(page) ':' counter(pages)");
        let mut requests = Vec::new();
        let bound = assignments[0]
            .content
            .bind_element_sources(
                TEST_CONTENT_LIMIT,
                &mut |source| {
                    assert_eq!(source, crate::style::ElementContent::Before);
                    Ok("Chapter ".into())
                },
                &mut |request| {
                    requests.push(request.name.to_string());
                    Ok("3".to_owned().into())
                },
            )
            .expect("document values can be bound before pagination");
        assert_eq!(requests, ["chapter"]);
        assert_eq!(
            bound
                .bound_page_output(2, 7, TEST_CONTENT_LIMIT)
                .unwrap()
                .text(),
            "Chapter 3/2:7"
        );
        assert_eq!(
            bound
                .bound_page_output(4, 9, TEST_CONTENT_LIMIT)
                .unwrap()
                .text(),
            "Chapter 3/4:9"
        );
        let limit = "Chapter 3/2:7".len() - 1;
        let cut = bound.bound_page_output(2, 7, limit).unwrap();
        assert_eq!(
            (cut.text(), cut.limited()),
            ("Chapter 3/2:", true),
            "page text past the limit is left out and reported"
        );
        let assignment = parse("heading 'AB' content(text) 'F'");
        let cut = assignment[0]
            .content
            .bind_element_sources(4, &mut |_| Ok("CDE".into()), &mut |_| {
                unreachable!("no counter in fixture")
            })
            .expect("element values past the limit are cut, not failed");
        let output = cut.bound_page_output(1, 1, TEST_CONTENT_LIMIT).unwrap();
        assert_eq!(
            (output.text(), output.limited()),
            ("ABCD", true),
            "binding cuts the element value at the limit, leaves out the parts after it, and \
             its output reports the cut"
        );
    }
}