//! The manifest decides whether this app may found a Personal database. use std::path::Path; #[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)] #[serde(rename_all = "snake_case")] pub enum Genesis { #[default] Personal, None, } pub const NO_PERSONAL_DATABASE_IN_FLAVOUR: &str = "no_personal_database_in_flavour"; pub const NO_PERSONAL_MESSAGE: &str = "No personal database in this flavour."; impl Genesis { /// No manifest is Cotton. An explicit, unknown word is a broken manifest. pub fn of(manifest: Option<&serde_json::Value>) -> Result { match manifest.and_then(|manifest| manifest.get("genesis")) { None => Ok(Self::Personal), Some(value) => serde_json::from_value(value.clone()) .map_err(|_| "a manifest's genesis is personal or none".into()), } } /// Keep this launch's policy for creation paths handed only a data folder. /// This is app configuration, never a database row or a schema change. pub fn keep(self, root: &Path) -> Result<(), String> { std::fs::create_dir_all(root).map_err(|error| error.to_string())?; std::fs::write( root.join("genesis.json"), serde_json::to_vec(&self).unwrap(), ) .map_err(|error| error.to_string()) } pub fn at(root: &Path) -> Result { Self::at_under(root, Self::here()?) } fn at_under(root: &Path, declared: Self) -> Result { // A stale folder declaration never widens the bundled manifest. if declared == Self::None { return Ok(Self::None); } match std::fs::read(root.join("genesis.json")) { Ok(bytes) => serde_json::from_slice(&bytes) .map_err(|error| format!("the app's genesis policy does not read: {error}")), Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(declared), Err(error) => Err(error.to_string()), } } pub fn here() -> Result { if crate::flavor::current() == Some(crate::flavor::Flavor::Business) { Ok(crate::flavor::business_manifest()?.genesis) } else { Ok(Self::Personal) } } pub fn require_personal(self) -> Result<(), &'static str> { match self { Self::Personal => Ok(()), Self::None => Err(NO_PERSONAL_MESSAGE), } } } /// The tools that are compiled in rather than shipped as a package: each is /// an engine this build carries (a worker's `tool()`, a handler group's key, /// a picture the app draws), on by being compiled, so founding a database /// writes nothing for it. A manifest may name these beside kit packages. /// `disk` is the CI ledger's package, which the client's CI engine carries /// and installs itself. pub const BUILT_IN_TOOLS: &[&str] = &[ "accounts", "ahead", "ai_usage", "alarms", "assistants", "calls", "code", "connections", "devices", "disk", "feedback", "mood", "news", "outages", "reader", "storage", "stories", "thoughts", "videos", ]; /// What one id in a manifest's `tools` is. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ToolKind { /// A package the compiled kit or the shipped toolkits carry: installed at /// founding as ordinary data, removable, editable, upgradable. Package, /// A compiled engine ([`BUILT_IN_TOOLS`]). BuiltIn, } impl ToolKind { /// What `id` is, or `None` when this build has no such tool. pub fn of(id: &str) -> Option { if crate::kit::ids().contains(&id) || crate::toolkits::kit_packages().iter().any(|package| package.tool.id == id) { Some(Self::Package) } else if BUILT_IN_TOOLS.contains(&id) { Some(Self::BuiltIn) } else { None } } } /// The genesis set: the manifest's `tools`, which every database this app /// founds is born with (architect, 22 Oct 2026), or its kit's tools when it /// names a kit (`kit::AppKit`). No `tools` and no kit is the shell alone. App configuration kept beside `genesis.json`, never a row. #[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct GenesisTools(pub Vec); impl GenesisTools { /// The manifest's `tools`, each one a package or a built-in. An id that is /// neither, or one named twice, refuses the manifest — never a silent skip. pub fn of(manifest: Option<&serde_json::Value>) -> Result { // A manifest that names its kit is born with the kit's tools: the kit // is the one source of what the app holds (coordinator, 5 Nov 2026). if let Some(kit) = crate::kit::AppKit::of(manifest)? { return Self::checked(kit.held()); } let Some(tools) = manifest.and_then(|manifest| manifest.get("tools")) else { return Ok(Self::default()); }; let ids: Vec = serde_json::from_value(tools.clone()) .map_err(|_| "a manifest's tools is a list of tool ids".to_string())?; Self::checked(ids) } /// `ids`, refused unless each is a tool this build has, once. pub fn checked(ids: Vec) -> Result { let mut seen = std::collections::BTreeSet::new(); for id in &ids { if ToolKind::of(id).is_none() { return Err(format!("the manifest names {id:?}, which is neither a kit tool nor a built-in one")); } if !seen.insert(id) { return Err(format!("the manifest names {id:?} twice")); } } Ok(Self(ids)) } /// The packages to install, in the manifest's order. pub fn packages(&self) -> Vec<&str> { self.0.iter().map(String::as_str).filter(|id| ToolKind::of(id) == Some(ToolKind::Package)).collect() } /// Keep this launch's set for the creation paths handed only a data folder. pub fn keep(&self, root: &Path) -> Result<(), String> { std::fs::create_dir_all(root).map_err(|error| error.to_string())?; std::fs::write(root.join(GENESIS_TOOLS_FILE), serde_json::to_vec(self).unwrap()) .map_err(|error| error.to_string()) } /// The set this data folder's app keeps; none kept is the shell alone. pub fn at(root: &Path) -> Result { match std::fs::read(root.join(GENESIS_TOOLS_FILE)) { Ok(bytes) => serde_json::from_slice(&bytes) .map_err(|error| format!("the app's genesis tools do not read: {error}")), Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(Self::default()), Err(error) => Err(error.to_string()), } } } const GENESIS_TOOLS_FILE: &str = "genesis_tools.json"; #[cfg(test)] #[path = "genesis_tests.rs"] mod tests;