//! The definition file format (architect, 14 Oct 2026): every shipped//! definition, board and theme file says which format it is written in, and//! each shipped file has a content hash a person's edits record, so an//! upgrade knows which shipped copy those edits were made against.//!//! `format_version` is one integer for the whole format, raised only by a//! decision. A file without one, or with one newer than this build reads, is//! refused with the file and both versions named. A stored ROW is not a file://! rows stay tolerant (a newer device's row is relayed, never refused).use serde::Serialize;use serde_json::Value;use sha2::{Digest, Sha256};usesuper::DefinitionError;/// The definition format this build reads and writes.pubconst FORMAT_VERSION: u32 = 1;/// A shipped file's format version, refused when it is missing or newer than/// this build's. `file` is the file's path under `definitions/`.pubfn check_file(file: &str, raw: &Value) -> Result<u32, DefinitionError> {letSome(found) = raw.get("format_version") else {returnErr(DefinitionError::NoFormatVersion { file: file.to_string(), build: FORMAT_VERSION }); };match found.as_u64().and_then(|found| u32::try_from(found).ok()).filter(|found| *found >= 1) {Some(found) if found <= FORMAT_VERSION => Ok(found),Some(found) => Err(DefinitionError::NewerFormat { file: file.to_string(), found: found.to_string(), build: FORMAT_VERSION }),None => Err(DefinitionError::NewerFormat { file: file.to_string(), found: found.to_string(), build: FORMAT_VERSION }), }}/// An authored definition (a person's row, a package's table) may leave the/// version out; when it names one, it is one this build reads.pubfn check_authored(file: &str, raw: &Value) -> Result<(), DefinitionError> {match raw.get("format_version") {None | Some(Value::Null) => Ok(()),Some(_) => check_file(file, raw).map(|_| ()), }}/// The content hash of one file's bytes: `sha256:<hex>`, over the bytes/// alone, so the same file hashes the same in the crate, in the app's bundle/// and in git.pubfn file_hash(bytes: &[u8]) -> String { format!("sha256:{:x}", Sha256::digest(bytes))}/// Every shipped file as `(path under definitions/, json)`: the tables, the/// boards and the themes.pubfn shipped_files() -> implIterator<Item = (String, &'static str)> {super::BUILTIN_DEFINITION_FILES .iter() .map(|(id, json)| (format!("{id}.json"), *json)) .chain(super::board::BUILTIN_BOARD_FILES.iter().map(|(id, json)| (format!("boards/{id}.json"), *json))) .chain(super::theme::BUILTIN_THEME_FILES.iter().map(|(id, json)| (format!("themes/{id}.json"), *json)))}/// The hash of the shipped file at `path` (as [`shipped_files`] names it),/// computed once per process.pubfn shipped_hash(path: &str) -> Option<&'static str> {static HASHES: std::sync::OnceLock<Vec<(String, String)>> = std::sync::OnceLock::new(); HASHES .get_or_init(|| shipped_files().map(|(path, json)| (path, file_hash(json.as_bytes()))).collect()) .iter() .find(|(at, _)| at == path) .map(|(_, hash)| hash.as_str())}/// One edit an upgrade could not carry onto the new shipped copy: the key it/// was at, the value the new copy draws there (null when the key is gone)/// and the person's value that was dropped.#[derive(Debug, Clone, PartialEq, Serialize)]pubstructDroppedEdit {pub key: String,pub kept: Value,pub dropped: Value,}#[cfg(test)]#[path = "format_tests.rs"]mod tests;