mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 07:35:02 +00:00
* Add craft-wordmodel: WordCraft's document model without its editing code WordCraft's crates/doc (wordcraft-doc, at 07f97737) moved as it is, so the DOCX, RTF, ODT and other text-format crates can read into and write from one model. Module paths, type names and serde attributes are unchanged: an app that re-exports the crate keeps its native file format. Left in the app: range editing (edit.rs), IME text and equation editing. Not model, and already in craft-libs: code pages (craft-rtf), the ZIP entry-count check (craft-opc), the compound-file guard (craft-cfb). The paragraph walks moved to walk.rs and the unit helpers of wordcraft-geom to geom.rs. * craft-wordmodel: math_tree, the arguments of an equation node and a walk over its runs The DOCX reader and writer walk an equation's runs. The walk and the child accessors it uses were in WordCraft's equation editor (math_edit.rs), which stays in the app; they are structure, not editing. * craft-wordmodel: keep the document operations (edit.rs) with the model The DOCX and RTF readers use them: alternative-format chunks are merged into a document with adopt_blocks, and a reader formats ranges with format_range and inserts objects. They operate on the document, not on a view, so they belong beside it. The tests that drove them come back with them; the paragraph walks stay in walk.rs and edit re-exports them. base64 0.23, as WordCraft and craft-offcrypto use.
31 lines
1.3 KiB
Rust
31 lines
1.3 KiB
Rust
//! A count of the characters a paragraph's object lookups walk, the blocks
|
|
//! [`crate::Document::para_paths`] visits, and each block lookup by path with the table levels it
|
|
//! descends, for the linear-time tests of the crates that use them: an input 8x the size must cost
|
|
//! about 8x the steps, which a busy machine can't skew the way it skews a timing. Counted only in
|
|
//! tests (this crate's, and other crates' through the `test-support` feature, which they turn on
|
|
//! as a dev-dependency); in the app the count is nothing.
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
thread_local! {
|
|
/// The steps counted on this thread.
|
|
static STEPS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
|
|
}
|
|
|
|
/// Count `n` steps of a walk (nothing outside tests). Public for the walks other crates do over a
|
|
/// paragraph's text themselves (Markdown and HTML comment export, for one).
|
|
#[inline(always)]
|
|
pub fn steps(n: usize) {
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
STEPS.with(|s| s.set(s.get().saturating_add(n as u64)));
|
|
#[cfg(not(any(test, feature = "test-support")))]
|
|
let _ = n;
|
|
}
|
|
|
|
/// The steps `f` takes.
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
pub fn count(f: impl FnOnce()) -> u64 {
|
|
let before = STEPS.with(std::cell::Cell::get);
|
|
f();
|
|
STEPS.with(std::cell::Cell::get).saturating_sub(before)
|
|
}
|