//! A record's card, page by page (`cards.pages {table, id}`): page 1 is the //! face — title, art and foot — and the pages after it are the card's //! `pages` columns in order (a meeting's summary, decisions, transcript), or //! without them its body column (a todo's `notes`), each cut as a note's text //! is cut, where it holds any. A card with nothing to turn to has one page. use crate::definitions::card::CardSlot; use crate::definitions::TableDefinition; use crate::transforms::text_pages::{pages, PageRoom}; use serde_json::{Map, Value}; use std::ops::Range; /// One page of a card. #[derive(Debug, Clone, PartialEq, Eq)] pub enum RecordPage { /// The face itself. Face, /// A run of the body column's text. Text { column: String, range: Range }, } /// The pages of `record` under `definition`. With `room` the body is cut to /// the pages a card holds (every one a later page's size: the face is page /// 1); without it the body is one page. pub fn record_pages(definition: &TableDefinition, record: &Map, room: Option) -> Vec { let mut cut = vec![RecordPage::Face]; let turned = definition.card.as_ref().map(|card| card.pages.clone()).filter(|pages| !pages.is_empty()); for column in turned.unwrap_or_else(|| body_column(definition).into_iter().collect()) { let text = record.get(&column).and_then(Value::as_str).unwrap_or_default(); if text.trim().is_empty() { continue; } let ranges = match room { Some(room) => pages(text, PageRoom { first: room.rest, ..room }), None => vec![0..text.len()], }; cut.extend(ranges.into_iter().map(|range| RecordPage::Text { column: column.clone(), range })); } cut } /// The column the card's body slot reads. pub fn body_column(definition: &TableDefinition) -> Option { definition .card .as_ref()? .fields .iter() .find(|field| field.slot == CardSlot::Body) .and_then(|field| field.column.clone()) } #[cfg(test)] #[path = "card_record_pages_tests.rs"] mod tests;