Cotton code
158 lines · 5.4 KB · RustRaw
//! Where CottonDB for Mac keeps the person's data — declared ONCE, here.//!//! `~/CottonDB`, not `~/Library/Application Support/CottonDB` (spec//! 2026-09-07-mac-home-folder-data-dir-design, H1): the product's pitch is a//! database the person owns, and Application Support is invisible in Finder and//! reads as the app's, not theirs. There is no picker: one place, the same on//! every Mac, so nobody ends up with their database on the Desktop.//!//! What did NOT survive the port: `electronUserDataDir`. It existed so a//! rehearsal's Chromium state would not contend with the installed app's//! single-instance lock. There is no Chromium and no `userData` directory any//! more — the Swift app keeps its own preferences under the standard macOS//! location keyed by the bundle id, and nothing of it lives in the data//! directory.use std::path::{Path, PathBuf};use cotton_core::flavor::Flavor;/// The REAL app's product name, and its directory's own name — derived from/// the one declaration of both apps' folders (`cotton_core::flavor`), so there/// is no second spelling. Cotton Dev's folder is/// `Flavor::Dev.data_dir_name()`; nothing here names it.pub const PRODUCT_NAME: &str = Flavor::Real.data_dir_name();/// The REAL app's bundle id.////// It stays what the Electron app shipped with, deliberately: macOS keys a/// Full Disk Access grant to the code signature AND the bundle id, and the app/// people already run holds that grant (design M11).pub const BUNDLE_ID: &str = "com.decemberfm.cottondb.mac";/// The environment variable that overrides the data directory.pub const DATA_DIR_ENV: &str = "COTTON_MAC_DATA_DIR";/// `<home>/CottonDB` or `<home>/CottonDev`, by which app this is — pure, so a/// test can pin it.pub fn home_data_dir(flavor: Flavor, home: impl AsRef<Path>) -> PathBuf {    flavor.home_data_dir(home)}/// `<home>/Library/Application Support/CottonDB` — where the REAL app's data/// directory used to be.////// Still named here because adoption has to find it once (H2); nothing else in/// this crate may name Application Support, and the guard in/// `apps/cotton/Tests` plus `paths` tests keep it that way. It is CottonDB's/// alone: Cotton Dev never had a folder there and never adopts one/// (`adopt::prepare_data_dir`).pub fn legacy_data_dir(home: impl AsRef<Path>) -> PathBuf {    home.as_ref()        .join("Library")        .join("Application Support")        .join(PRODUCT_NAME)}/// The data directory this run will use.#[derive(Debug, Clone, PartialEq, Eq)]pub struct ResolvedDataDir {    pub path: PathBuf,    /// True when `COTTON_MAC_DATA_DIR` named it. An override is an explicit    /// instruction — a rehearsal, a restore, a test — so adoption stays out of    /// it.    pub overridden: bool,}/// Resolve the data directory.////// A blank override reads as no override: an unset environment variable and one/// set to the empty string mean the same thing to a person typing the command.////// RESOLVING IS NOT ADMITTING: an override may name the other app's folder,/// and it is `adopt::foreign_folder_refusal` that says no to it.pub fn resolve_data_dir(    flavor: Flavor,    home: impl AsRef<Path>,    over: Option<&str>,) -> ResolvedDataDir {    if let Some(value) = over {        let trimmed = value.trim();        if !trimmed.is_empty() {            return ResolvedDataDir {                path: PathBuf::from(trimmed),                overridden: true,            };        }    }    ResolvedDataDir {        path: home_data_dir(flavor, home),        overridden: false,    }}#[cfg(test)]mod tests {    use super::*;    #[test]    fn the_data_dir_is_the_home_folder_s_cottondb_where_a_person_can_see_it() {        assert_eq!(            home_data_dir(Flavor::Real, "/Users/x"),            PathBuf::from("/Users/x/CottonDB")        );    }    #[test]    fn cotton_dev_keeps_its_own_folder_beside_it_never_inside_it() {        assert_eq!(            home_data_dir(Flavor::Dev, "/Users/x"),            PathBuf::from("/Users/x/CottonDev")        );        assert_eq!(            resolve_data_dir(Flavor::Dev, "/Users/x", None).path,            PathBuf::from("/Users/x/CottonDev")        );    }    #[test]    fn it_still_knows_the_old_path_because_adoption_has_to_find_it() {        assert_eq!(            legacy_data_dir("/Users/x"),            PathBuf::from("/Users/x/Library/Application Support/CottonDB")        );    }    #[test]    fn it_names_the_product_once() {        assert_eq!(PRODUCT_NAME, "CottonDB");    }    #[test]    fn it_resolves_to_the_home_folder_when_nothing_overrides_it() {        assert_eq!(            resolve_data_dir(Flavor::Real, "/Users/x", None),            ResolvedDataDir {                path: PathBuf::from("/Users/x/CottonDB"),                overridden: false            }        );    }    #[test]    fn it_honours_the_override_and_says_so_so_adoption_stays_out_of_it() {        assert_eq!(            resolve_data_dir(Flavor::Real, "/Users/x", Some("/tmp/scratch")),            ResolvedDataDir {                path: PathBuf::from("/tmp/scratch"),                overridden: true            }        );    }    #[test]    fn an_empty_override_reads_as_no_override_an_unset_variable_reads_as_one() {        assert!(!resolve_data_dir(Flavor::Real, "/Users/x", Some("")).overridden);        assert!(!resolve_data_dir(Flavor::Real, "/Users/x", Some("  ")).overridden);    }}