Cotton code
137 lines · 7.5 KB · RustRaw
//! The pictures built-in tools ship as DATA: each is written into every//! database as a photo record (`photo:<sha256>`) and a definition names it as//! `{"photo": …}` — the same form an imported tool's picture takes — so the//! picture is a record the person can replace, never an imageset in the app.//! Content-addressed: the file's name is the SHA-256 of its bytes./// `(sha256, bytes)` for every shipped tool picture, each a cut-out PNG./// They are the business kits' own (the home kitchen's, screenwriting's): a/// core built for one Cotton app's kit carries none (`crate::kit_pictures`).#[cfg(cotton_app_core)]pub const BUILTIN_TOOL_PICTURES: &[(&str, &[u8])] = &[];/// See above: the whole core's.#[cfg(not(cotton_app_core))]pub const BUILTIN_TOOL_PICTURES: &[(&str, &[u8])] = &[    // Home kitchen kit, Today's menu: a menu card on a wooden clipboard.    (        "fc8a3902f7078f93914f6c845b7ca601941df1df38945ecbb289ac3d2852c485",        include_bytes!("../../kits/kitchen/pictures/fc8a3902f7078f93914f6c845b7ca601941df1df38945ecbb289ac3d2852c485.png"),    ),    // Home kitchen kit, Kitchen (the tool): a brass order spike holding chits.    (        "116b7043feeed0389ae0452a08f6591f2867f8ebf8a34f811701ef7c3fe4a140",        include_bytes!("../../kits/kitchen/pictures/116b7043feeed0389ae0452a08f6591f2867f8ebf8a34f811701ef7c3fe4a140.png"),    ),    // Home kitchen kit, Customers: a red cloth-bound khata ledger.    (        "39655737b449b5a77c8eeca3db8f4f9d844fc4271f331043c3c1da729a88d760",        include_bytes!("../../kits/kitchen/pictures/39655737b449b5a77c8eeca3db8f4f9d844fc4271f331043c3c1da729a88d760.png"),    ),    // Home kitchen kit, Deliveries: a three-tier steel tiffin carrier.    (        "b2c5d49aa05ad823987a98219f76a6038ed855971f1a2850defd47f302069be5",        include_bytes!("../../kits/kitchen/pictures/b2c5d49aa05ad823987a98219f76a6038ed855971f1a2850defd47f302069be5.png"),    ),    // Home kitchen kit, Stock: a steel masala dabba.    (        "072fe138acfaba464f7566b2b41423b63097301abb4a5683db1b57f32c8fef28",        include_bytes!("../../kits/kitchen/pictures/072fe138acfaba464f7566b2b41423b63097301abb4a5683db1b57f32c8fef28.png"),    ),    // Home kitchen kit, Money: an old tin cash box.    (        "b23a56d94fd226be5e2e0ba0ecee687ba20df2a54f902c358ee1cfc187209b92",        include_bytes!("../../kits/kitchen/pictures/b23a56d94fd226be5e2e0ba0ecee687ba20df2a54f902c358ee1cfc187209b92.png"),    ),    // Screenwriting kit, Scenes: storyboard panels under a binder clip.    (        "f1b6f1787aff6bfdf2d00ff5913ac22008e84556ba2812d13daf6360b6e6698d",        include_bytes!("../../toolkits/pictures/f1b6f1787aff6bfdf2d00ff5913ac22008e84556ba2812d13daf6360b6e6698d.png"),    ),    // Screenwriting kit, Characters: a scatter of vintage headshots.    (        "84596da2838bb9b16a381014ea70b202b104c18e9edd451b8b301f14bf91f89a",        include_bytes!("../../toolkits/pictures/84596da2838bb9b16a381014ea70b202b104c18e9edd451b8b301f14bf91f89a.png"),    ),    // Screenwriting kit, Beats: index cards in a rubber band.    (        "2b0cd0233b874f32731a5a9a7a738efa3454d7cc45d582281464df5d2589a4d0",        include_bytes!("../../toolkits/pictures/2b0cd0233b874f32731a5a9a7a738efa3454d7cc45d582281464df5d2589a4d0.png"),    ),    // Screenwriting kit, Readers' notes: a script page marked in red pencil.    (        "8ff05235ab3e3531144d27792f291cbfd3a1f939f250a45aa0ae086fe6c85603",        include_bytes!("../../toolkits/pictures/8ff05235ab3e3531144d27792f291cbfd3a1f939f250a45aa0ae086fe6c85603.png"),    ),];/// Where the app ships the object a built-in picture names (`ToolPhoto::object`),/// relative to this crate: the server seeds the photo record from these bytes.pub const SHIPPED_OBJECTS_DIR: &str = "../../apps/loom/Sources/CottonPaper/Media.xcassets";#[cfg(test)]mod tests {    use super::*;    use crate::definitions::{builtin_table_definitions, ToolPicture};    use sha2::{Digest, Sha256};    /// RULE: a built-in definition's photo picture is a picture this build    /// ships, under the hash of its own bytes — the package-style pictures in    /// this crate, or the object the app ships that the picture names, whose    /// bytes the server seeds — and every picture shipped here is named;    /// otherwise a database is seeded with a photo no card draws, or a card    /// names a photo no database holds.    #[test]    fn every_photo_picture_is_shipped_and_every_shipped_picture_is_named() {        for (sha, bytes) in BUILTIN_TOOL_PICTURES {            assert_eq!(hex::encode(Sha256::digest(bytes)), *sha, "{sha}.png is not named by its own hash");        }        let root = cotton_store::manifest_dir().join(SHIPPED_OBJECTS_DIR);        let mut named = Vec::new();        for definition in builtin_table_definitions() {            let Some(ToolPicture::Photo(picture)) = &definition.picture else { continue };            let sha = picture.photo.strip_prefix("photo:").expect("a photo picture is photo:<sha256>").to_string();            match &picture.object {                Some(object) => {                    let path = root.join(format!("{object}.imageset/{object}.png"));                    let bytes = std::fs::read(&path).unwrap_or_else(|error| panic!("{}: {error}", path.display()));                    assert_eq!(hex::encode(Sha256::digest(&bytes)), sha, "{}'s picture is not {object}'s bytes", definition.id);                }                None => assert!(BUILTIN_TOOL_PICTURES.iter().any(|(shipped, _)| *shipped == sha), "{sha} is not shipped"),            }            named.push(sha);        }        // A shipped kit's carried tool (crates/cotton-core/toolkits/shipped.json)        // names its picture the same way, and ships it here too.        let kit_packages: Vec<_> = crate::toolkits::shipped().into_iter().flat_map(|kit| kit.tools).filter_map(|tool| match tool {            crate::toolkits::KitTool::Package { package } => Some(package),            crate::toolkits::KitTool::Kit(_) => None,        }).collect();        // A carried package's table may name one too (the kitchen's menu, customers, deliveries).        for table in kit_packages.iter().flat_map(|package| package.tables.iter()) {            let Some(ToolPicture::Photo(picture)) = &table.picture else { continue };            let sha = picture.photo.strip_prefix("photo:").expect("a photo picture is photo:<sha256>").to_string();            assert!(BUILTIN_TOOL_PICTURES.iter().any(|(shipped, _)| *shipped == sha), "{}: {sha} is not shipped", table.id);            named.push(sha);        }        for tool in kit_packages.into_iter().map(|package| package.tool) {            let Some(ToolPicture::Photo(picture)) = tool.picture else { continue };            let sha = picture.photo.strip_prefix("photo:").expect("a photo picture is photo:<sha256>").to_string();            assert!(picture.object.is_none(), "{}: a kit tool's picture is a package-style picture, not an object", tool.id);            assert!(BUILTIN_TOOL_PICTURES.iter().any(|(shipped, _)| *shipped == sha), "{sha} is not shipped");            named.push(sha);        }        for (sha, _) in BUILTIN_TOOL_PICTURES {            assert!(named.iter().any(|photo| photo == sha), "{sha}.png is named by no definition");        }    }}include!(concat!(env!("OUT_DIR"), "/tool_pictures.rs"));/// Every picture Cotton ships, including the assets the app names by object.pub fn shipped_bytes(hash: &str) -> Option<&'static [u8]> {    BUILTIN_TOOL_PICTURES.iter().chain(OBJECT_TOOL_PICTURES.iter())        .find(|(shipped, _)| *shipped == hash).map(|(_, bytes)| *bytes)}