f-office-docx 0.2.4

DOCX model and Word's layout rules.
Documentation
//! The content walks: the walks over the run content of a story's blocks, with the content of
//! the table cells, of the text box of each floating or inline drawing and of each part of its
//! group, through which the field functions ask what a story holds and give the fields of the
//! body their stored results. One function names every kind of content and says which of them
//! hold a story, so that a new kind must be sorted there, and each walk checks the kinds that
//! it does not enter against it.

use std::rc::Rc;

use super::{Block, CodePart, Content};
use crate::floating::Floating;

/// Whether any content of `blocks` passes `test`: the content of the paragraphs and of the
/// table cells, of the text box of each floating or inline drawing and of each part of its
/// group, and the fields nested in the code of an `IF` field.
pub(super) fn any_content(blocks: &[Block], test: &mut dyn FnMut(&Content) -> bool) -> bool {
    blocks.iter().any(|block| match block {
        Block::Paragraph(paragraph) => paragraph
            .runs
            .iter()
            .flat_map(|run| &run.content)
            .any(|content| content_passes(content, test)),
        Block::Table(table) => table
            .rows
            .iter()
            .flat_map(|row| &row.cells)
            .any(|cell| any_content(&cell.blocks, test)),
    })
}

/// Whether `content` passes `test`, or any content of the story or the code that it holds.
fn content_passes(content: &Content, test: &mut dyn FnMut(&Content) -> bool) -> bool {
    if test(content) {
        return true;
    }
    match content {
        Content::Floating(floating) => drawing_passes(floating, test),
        Content::Drawing(drawing) => drawing_passes(&drawing.shape, test),
        Content::If(field) => field.parts.iter().any(|part| match part {
            CodePart::Field(nested) => content_passes(nested, test),
            CodePart::Text(_) => false,
        }),
        other => {
            debug_assert!(holds_no_story(other), "{other:?}");
            false
        }
    }
}

/// Whether any content of the text box of `drawing` or of a part of its group passes `test`.
fn drawing_passes(drawing: &Floating, test: &mut dyn FnMut(&Content) -> bool) -> bool {
    any_content(&drawing.blocks, test)
        || drawing
            .parts
            .iter()
            .any(|(_, _, part)| drawing_passes(part, test))
}

/// Calls `visit` for each content of `blocks`, in the order of the document: the content of the
/// paragraphs and of the table cells, and after a floating or inline drawing the content of its
/// text box and then of each part of its group. The fields nested in the code of an `IF` field
/// are not visited: layout gives them their values when it evaluates the field.
pub(super) fn visit_contents_mut(blocks: &mut [Block], visit: &mut dyn FnMut(&mut Content)) {
    for block in blocks {
        match block {
            Block::Paragraph(paragraph) => {
                for content in paragraph
                    .runs
                    .iter_mut()
                    .flat_map(|run| run.content.iter_mut())
                {
                    visit_content_mut(content, visit);
                }
            }
            Block::Table(table) => {
                for cell in table.rows.iter_mut().flat_map(|row| row.cells.iter_mut()) {
                    visit_contents_mut(&mut cell.blocks, visit);
                }
            }
        }
    }
}

/// Visits `content`, then the content of the story that it holds.
fn visit_content_mut(content: &mut Content, visit: &mut dyn FnMut(&mut Content)) {
    visit(content);
    match content {
        Content::Floating(floating) => visit_drawing_mut(floating, visit),
        Content::Drawing(drawing) => visit_drawing_mut(&mut Rc::make_mut(drawing).shape, visit),
        // Layout gives the fields nested in an `IF` field's code their values.
        Content::If(_) => {}
        other => debug_assert!(holds_no_story(other), "{other:?}"),
    }
}

/// Visits the content of the text box of `drawing`, then of each part of its group.
fn visit_drawing_mut(drawing: &mut Floating, visit: &mut dyn FnMut(&mut Content)) {
    visit_contents_mut(&mut drawing.blocks, visit);
    for (_, _, part) in &mut drawing.parts {
        visit_drawing_mut(part, visit);
    }
}

/// Whether `content` holds no story and no code that the content walks enter: every kind but a
/// floating drawing, an inline drawing and an `IF` field. It names every kind, so that a new
/// one must be sorted here, and each walk checks the kinds that it does not enter against it.
const fn holds_no_story(content: &Content) -> bool {
    match content {
        Content::Floating(_) | Content::Drawing(_) | Content::If(_) => false,
        Content::Text(_)
        | Content::Tab
        | Content::PositionTab { .. }
        | Content::LineBreak
        | Content::ClearingBreak
        | Content::PageBreak
        | Content::ColumnBreak
        | Content::FieldBegin
        | Content::FieldLock
        | Content::DropDown(_)
        | Content::CheckBox(_)
        | Content::FieldCode(_)
        | Content::FieldSeparate
        | Content::FieldEnd
        | Content::PageNumber
        | Content::PageCount
        | Content::Date(_)
        | Content::DocProperty(_)
        | Content::StyleRef(_)
        | Content::PageRef(_)
        | Content::NumberRef(_)
        | Content::Bookmark(_)
        | Content::FieldMark
        | Content::HyperlinkEdge
        | Content::ControlEdge
        | Content::RemovedEdge
        | Content::CommentAnchor(_)
        | Content::FootnoteReference(..)
        | Content::FootnoteMark
        | Content::EndnoteReference(..)
        | Content::EndnoteMark
        | Content::CommentMark
        | Content::Separator
        | Content::ContinuationSeparator
        | Content::Stack(_)
        | Content::Grown(_)
        | Content::Radical(_)
        | Content::Accent(_)
        | Content::Matrix(_)
        | Content::MathBar(_)
        | Content::MathSlash(_)
        | Content::MathLimit(_)
        | Content::GroupChr(_)
        | Content::MathBox(_)
        | Content::Scripts(_)
        | Content::Advance(_)
        | Content::Object { .. } => true,
    }
}

#[cfg(test)]
mod tests;