//! 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")]pubenumGenesis { #[default]Personal,None,}pubconst NO_PERSONAL_DATABASE_IN_FLAVOUR: &str = "no_personal_database_in_flavour";pubconst NO_PERSONAL_MESSAGE: &str = "No personal database in this flavour.";implGenesis {/// No manifest is Cotton. An explicit, unknown word is a broken manifest.pubfn of(manifest: Option<&serde_json::Value>) -> Result<Self, String> {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.pubfn 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()) }pubfn at(root: &Path) -> Result<Self, String> {Self::at_under(root, Self::here()?) }fn at_under(root: &Path, declared: Self) -> Result<Self, String> {// A stale folder declaration never widens the bundled manifest.if declared == Self::None {returnOk(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()), } }pubfn here() -> Result<Self, String> {ifcrate::flavor::current() == Some(crate::flavor::Flavor::Business) {Ok(crate::flavor::business_manifest()?.genesis) } else {Ok(Self::Personal) } }pubfn require_personal(self) -> Result<(), &'static str> {matchself {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.pubconst 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)]pubenumToolKind {/// 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,}implToolKind {/// What `id` is, or `None` when this build has no such tool.pubfn of(id: &str) -> Option<Self> {ifcrate::kit::ids().contains(&id) || crate::toolkits::kit_packages().iter().any(|package| package.tool.id == id) {Some(Self::Package) } elseif 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)]pubstructGenesisTools(pubVec<String>);implGenesisTools {/// 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.pubfn of(manifest: Option<&serde_json::Value>) -> Result<Self, String> {// 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).ifletSome(kit) = crate::kit::AppKit::of(manifest)? {returnSelf::checked(kit.held()); }letSome(tools) = manifest.and_then(|manifest| manifest.get("tools")) else {returnOk(Self::default()); };let ids: Vec<String> = 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.pubfn checked(ids: Vec<String>) -> Result<Self, String> {letmut seen = std::collections::BTreeSet::new();for id in &ids {ifToolKind::of(id).is_none() {returnErr(format!("the manifest names {id:?}, which is neither a kit tool nor a built-in one")); }if !seen.insert(id) {returnErr(format!("the manifest names {id:?} twice")); } }Ok(Self(ids)) }/// The packages to install, in the manifest's order.pubfn 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.pubfn 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.pubfn at(root: &Path) -> Result<Self, String> {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;