//! 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}; use super::DefinitionError; /// The definition format this build reads and writes. pub const 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/`. pub fn check_file(file: &str, raw: &Value) -> Result { let Some(found) = raw.get("format_version") else { return Err(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. pub fn 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:`, over the bytes /// alone, so the same file hashes the same in the crate, in the app's bundle /// and in git. pub fn 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. pub fn shipped_files() -> impl Iterator { 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. pub fn shipped_hash(path: &str) -> Option<&'static str> { static HASHES: std::sync::OnceLock> = 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)] pub struct DroppedEdit { pub key: String, pub kept: Value, pub dropped: Value, } #[cfg(test)] #[path = "format_tests.rs"] mod tests;