text-document-search 1.10.2

Find and replace use cases for text-document
Documentation
// Hand-added (NOT generated by Qleany): the addressable-text accessor.
use super::find_all_uc::{FindAllUnitOfWorkFactoryTrait, build_full_text};
use crate::AddressableTextResultDto;
use anyhow::Result;

/// Hand the caller the document's **addressable text** — the exact string the document's
/// own offsets index into.
///
/// Every offset the document deals out lives in one char space: `find_all`/`find_text`
/// match positions, `replace_ranges` ranges, a block's `document_position`, a cursor or
/// an editor widget's selection. The string that space indexes is the text `find_all`
/// searches — which, until this use case, no caller could reach from a live document.
/// The only whole-document strings on offer were the plain-text *exports*, which omit
/// the `U+FFFC` anchor an embedded table occupies, so every offset taken against them
/// drifts by two characters per preceding table. That drift is not hypothetical: a
/// comment anchored on prose after a table captured its quote two characters off
/// (`"salt-bleached"` stored as `"lt-bleached d"`).
///
/// Implemented as *literally* `find_all`'s own text builder behind `find_all`'s own
/// unit-of-work type — not a lookalike walk — so the string returned here and the string
/// searched there are one definition. If the builder changes, both change together.
pub struct AddressableTextUseCase {
    uow_factory: Box<dyn FindAllUnitOfWorkFactoryTrait>,
}

impl AddressableTextUseCase {
    pub fn new(uow_factory: Box<dyn FindAllUnitOfWorkFactoryTrait>) -> Self {
        AddressableTextUseCase { uow_factory }
    }

    pub fn execute(&mut self) -> Result<AddressableTextResultDto> {
        let uow = self.uow_factory.create();
        uow.begin_transaction()?;
        let text = build_full_text(uow.as_ref())?;
        uow.end_transaction()?;
        Ok(AddressableTextResultDto { text })
    }
}