//! Which file this is, and which name its derived ids are made under — two //! values that are usually the same string and must never be confused. //! //! A database made at the stop keeps the old file's records, and those records //! were derived under the OLD file's id: a project made from the slug `cotton` //! has the id `memory_project_id(old_id, "cotton")` on every device. So the new //! file has its own id (its `_meta.workspace_id`) and the old one as its //! NAMESPACE, and every producer that derives an id there must reach the old //! value or two devices write the same project twice. A database made later has //! its own id as both. //! //! The two answers are not interchangeable the other way either. A media key //! and a repository's clone path say where BYTES live, and two files sharing a //! namespace would share one folder; they take the file's own id. //! //! So each is its own type, and the only way to get one outside a test is to //! read it off an open file ([`read_file_identity`]). A `&str` from a request //! body, a token or a key's first segment cannot become either by accident. use std::fmt; use rusqlite::types::Value; use crate::driver::Driver; use crate::error::Result; /// The file's own id: `_meta.workspace_id`. What media keys, repository ids /// and clone paths are made from. #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct FileId(String); /// The value derived record ids take in this file: its own `cotton_workspaces` /// row's `namespace` when set, else the file's own id. #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct Namespace(String); impl FileId { pub fn as_str(&self) -> &str { &self.0 } /// For TESTS only: a file id no file was opened for. Behind the test-only /// features, so no shipping build has it. #[cfg(any(test, feature = "test-template", feature = "fixtures"))] pub fn for_tests(id: &str) -> Self { Self(id.to_string()) } } impl Namespace { pub fn as_str(&self) -> &str { &self.0 } /// The namespace of a database this machine holds no file of — the /// personal object the Mac's importer sends to: its database id, exactly /// as for a file whose workspace row sets no namespace of its own. pub fn of_database(database_id: &str) -> Self { Self(database_id.to_string()) } /// For TESTS only, as [`FileId::for_tests`]. #[cfg(any(test, feature = "test-template", feature = "fixtures"))] pub fn for_tests(namespace: &str) -> Self { Self(namespace.to_string()) } } impl fmt::Display for FileId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(&self.0) } } impl fmt::Display for Namespace { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(&self.0) } } /// Both answers for one open file, read together so they cannot come from two /// different files. #[derive(Clone, Debug, PartialEq, Eq)] pub struct FileIdentity { id: FileId, namespace: Namespace, } impl FileIdentity { pub fn id(&self) -> &FileId { &self.id } pub fn namespace(&self) -> &Namespace { &self.namespace } /// For TESTS only: a file whose namespace is its own id, which is what /// every database made after the stop is. #[cfg(any(test, feature = "test-template", feature = "fixtures"))] pub fn for_tests(id: &str) -> Self { Self { id: FileId::for_tests(id), namespace: Namespace::for_tests(id), } } } /// Which file `db` is and which namespace it derives under, or `None` when the /// file has never been seeded. /// /// The namespace comes from the file's OWN `cotton_workspaces` row — the one /// whose id is `_meta.workspace_id` — and never from any other row a file may /// hold: the column is fixed when that row is created (`mint` refuses a change), /// so it is the same answer on every device that holds the file. A file whose /// own row is not there yet (a replica before its first exchange) derives /// under its own id, which is what that row will say for any database not /// made at the stop. pub fn read_file_identity(db: &Driver) -> Result> { let Some(meta) = db.get("SELECT workspace_id FROM _meta", &[])? else { return Ok(None); }; let id = meta.text("workspace_id").unwrap_or_default().to_string(); let namespace = db .get( "SELECT namespace FROM cotton_workspaces WHERE id = ?", &[Value::Text(id.clone())], )? .and_then(|row| row.text("namespace").map(str::to_string)) .unwrap_or_else(|| id.clone()); Ok(Some(FileIdentity { id: FileId(id), namespace: Namespace(namespace), })) } #[cfg(test)] #[path = "file_identity_tests.rs"] mod tests;