84 lines · 3.7 KB · RustRaw
//! `cards.pages` — a long text cut into the pages a card shows one at a time//! (`cotton_core::transforms::text_pages`), each with its "2 / 4" said in the//! database's `cards` lines. The card sends how much a page holds, measured//! off its typewriter at the size it is drawn: `{ text, columns, first, rest }`.//! Worked out when the card is drawn; nothing is stored.//!//! Given a record instead (`{ table, id, columns?, first?, rest? }`), the//! record's card page by page (`cotton_core::rules::card_record_pages`): page//! 1 the face, `{ kind: "face" }`, and its body column (a todo's notes) after//! it, `{ kind: "text", column, text }`, each with its label.use serde_json::{json, Value};use super::MethodError;use crate::handle::CottonHandle;use cotton_core::rules::shell_lines;use cotton_core::rules::card_record_pages::{record_pages, RecordPage};use cotton_core::transforms::text_pages::{pages, PageRoom};pub fn cut(handle: &CottonHandle, request: &Value) -> Result<Value, MethodError> { if request.get("table").is_some() || request.get("id").is_some() { return of_record(&handle.client(), request); } let text = request.get("text").and_then(Value::as_str).unwrap_or_default(); let count = |field: &str| { request .get(field) .and_then(Value::as_u64) .filter(|n| *n > 0) .map(|n| n as usize) .ok_or_else(|| MethodError::new("bad_request", format!("`{field}` is a count above nought"))) }; let room = PageRoom { columns: count("columns")?, first: count("first")?, rest: count("rest")? }; let rows = handle .client() .with_file(|file| Ok(cotton_tools::presentation::line_rows(file.driver, file.workspace_id))) .unwrap_or_default(); let cut = pages(text, room); let of = shell_lines::number(cut.len() as i64); let pages: Vec<Value> = cut .iter() .enumerate() .map(|(index, range)| { let at = shell_lines::number(index as i64 + 1); let label = shell_lines::resolve("cards", "note.page", &rows, &[("at", &at), ("of", &of)]); json!({ "text": &text[range.clone()], "label": label }) }) .collect(); Ok(json!({ "pages": pages }))}fn of_record(client: &crate::shell::client::ClientController, request: &Value) -> Result<Value, MethodError> { let card = crate::phone::card_actions::read_card(client, request, "cards.pages")?; let count = |field: &str| request.get(field).and_then(Value::as_u64).filter(|n| *n > 0).map(|n| n as usize); let room = match (count("columns"), count("rest")) { (Some(columns), Some(rest)) => Some(PageRoom { columns, first: count("first").unwrap_or(rest), rest }), _ => None, }; let rows = client .with_file(|file| Ok(cotton_tools::presentation::line_rows(file.driver, file.workspace_id))) .unwrap_or_default(); let cut = record_pages(&card.definition, &card.data, room); let of = shell_lines::number(cut.len() as i64); let pages: Vec<Value> = cut .iter() .enumerate() .map(|(index, page)| { let at = shell_lines::number(index as i64 + 1); let label = shell_lines::resolve("cards", "note.page", &rows, &[("at", &at), ("of", &of)]); match page { RecordPage::Face => json!({ "kind": "face", "label": label }), RecordPage::Text { column, range } => { let text = card.data.get(column).and_then(Value::as_str).unwrap_or_default(); json!({ "kind": "text", "column": column, "text": &text[range.clone()], "label": label }) } } }) .collect(); Ok(json!({ "pages": pages }))}#[cfg(test)]#[path = "card_pages_tests.rs"]mod tests;