Cotton code
43 lines · 2.0 KB · RustRaw
//! Where a book's facts came from (7 Nov 2026): the//! `book` table's `filled_from` is film's shape, a JSON object naming, for//! each column a fill wrote, the source it came from//! (`{"author": "opf", "pages": "open_library"}`). It is kept by the app//! (readonly): only a fill writes it, and only for the columns it filled.//!//! One closed list of sources for every door: the phone's EPUB add and Add a//! folder (`opf`, cotton-ffi media), the object's `book_file` pass (`opf`),//! `import_goodreads` (`goodreads`) and `find_book` (`open_library`), the//! last three in the Worker (`apps/business/src/books.ts` `BOOK_SOURCES`).//! Both sides answer `tests/fixtures/book_filled_from.json` alike, so a source//! added here is added there, never one alone.use serde_json::{json, Map, Value};/// A fill read from the book's own package document (an EPUB's OPF, a/// Calibre `metadata.opf`).pub const FROM_OPF: &str = "opf";/// A fill a Goodreads library export made.pub const FROM_GOODREADS: &str = "goodreads";/// A fill an Open Library lookup made (Find details).pub const FROM_OPEN_LIBRARY: &str = "open_library";/// Every source a book's `filled_from` names, and no other.pub const SOURCES: [&str; 3] = [FROM_OPF, FROM_GOODREADS, FROM_OPEN_LIBRARY];/// The list as the shared fixture holds it: the drift check of the two sides.pub fn lists() -> Value {    json!({ "sources": SOURCES })}/// A row's `filled_from` text (`held`) with each of `columns` said as `from`/// beside what it said already; text that is no JSON object is started again.pub fn stamped<'a>(held: Option<&str>, columns: impl IntoIterator<Item = &'a str>, from: &str) -> String {    debug_assert!(SOURCES.contains(&from), "{from} is no book source");    let mut said: Map<String, Value> = held.and_then(|text| serde_json::from_str(text).ok()).unwrap_or_default();    for column in columns {        said.insert(column.to_string(), Value::from(from));    }    Value::Object(said).to_string()}#[cfg(test)]mod tests;