f-office-docx 0.2.4

DOCX model and Word's layout rules.
Documentation
use std::rc::Rc;

use f_office_units::Length;

use super::any_content;
use crate::body::fields::keep_stored_properties;
use crate::body::{
    Block, CodePart, Content, DocProperty, IfField, InlineDrawing, Paragraph, Run, StyleRef,
    refers_to_pages, refers_to_styles,
};
use crate::floating::Floating;
use crate::table::{Cell, Row, Table};

fn paragraph(content: Vec<Content>) -> Block {
    Block::Paragraph(Paragraph {
        runs: vec![Run {
            content,
            ..Run::default()
        }],
        ..Paragraph::empty()
    })
}

/// A drawing with a text box of `blocks` and the group parts `parts`.
fn drawing(blocks: Vec<Block>, parts: Vec<Floating>) -> Floating {
    Floating {
        blocks,
        parts: parts
            .into_iter()
            .map(|part| (Length::ZERO, Length::ZERO, part))
            .collect(),
        ..Floating::default()
    }
}

/// Blocks that hold `content` in one place each, named: a paragraph, a table cell, the text box
/// of a floating drawing, of an inline drawing and of a part of a group, and the code of an
/// `IF` field.
fn places(content: &Content) -> Vec<(&'static str, Vec<Block>)> {
    let alone = || vec![paragraph(vec![content.clone()])];
    let cell = Block::Table(Table {
        rows: vec![Row {
            cells: vec![Cell {
                blocks: alone(),
                ..Cell::default()
            }],
            ..Row::default()
        }],
        ..Table::default()
    });
    let group = drawing(Vec::new(), vec![drawing(alone(), Vec::new())]);
    let code = IfField {
        parts: vec![
            CodePart::Text("IF ".to_owned()),
            CodePart::Field(content.clone()),
            CodePart::Text(" = 1 \"a\" \"b\"".to_owned()),
        ],
        stored: String::new(),
    };
    vec![
        ("a paragraph", alone()),
        ("a table cell", vec![cell]),
        (
            "a floating text box",
            vec![paragraph(vec![Content::Floating(Box::new(drawing(
                alone(),
                Vec::new(),
            )))])],
        ),
        (
            "an inline text box",
            vec![paragraph(vec![Content::Drawing(Rc::new(
                InlineDrawing::of_shape(drawing(alone(), Vec::new())),
            ))])],
        ),
        (
            "a group part's text box",
            vec![paragraph(vec![Content::Floating(Box::new(group))])],
        ),
        (
            "an IF code",
            vec![paragraph(vec![Content::If(Box::new(code))])],
        ),
    ]
}

#[test]
fn finds_page_and_style_fields_in_cells_text_boxes_group_parts_and_if_codes() {
    let style = Content::StyleRef(Box::new(StyleRef {
        style: "Heading1".to_owned(),
        last: false,
        number: false,
        stored: String::new(),
    }));
    for (place, blocks) in places(&Content::PageCount) {
        assert!(refers_to_pages(&blocks), "{place}");
        assert!(!refers_to_styles(&blocks), "{place}");
    }
    for (place, blocks) in places(&style) {
        assert!(refers_to_styles(&blocks), "{place}");
        assert!(!refers_to_pages(&blocks), "{place}");
    }
    assert!(!refers_to_pages(&[paragraph(vec![Content::PageNumber])]));
}

#[test]
fn gives_a_doc_property_its_stored_result_in_every_text_box_but_not_in_an_if_code() {
    let property = Content::DocProperty(Box::new(DocProperty {
        name: "Client".to_owned(),
        picture: None,
        stored: "Stored".to_owned(),
    }));
    for (place, mut blocks) in places(&property) {
        keep_stored_properties(&mut blocks);
        let in_code = place == "an IF code";
        let field = any_content(&blocks, &mut |c| matches!(c, Content::DocProperty(_)));
        let stored = any_content(&blocks, &mut |c| *c == Content::Text("Stored".to_owned()));
        assert_eq!((field, stored), (in_code, !in_code), "{place}");
    }
}