Cotton code
48 lines · 2.1 KB · RustRaw
//! Canonical person-handle identity and row construction, shared by the//! People engine and explicit schema moves. No lookup, clock, or mutation.use serde_json::{Map, Value};use crate::file_identity::Namespace;use crate::outbox::LocalChange;use super::derived_id::person_handle_id;use super::types::OpKind;/// The table.pub const PERSON_HANDLES: &str = "cotton_person_handles";pub use cotton_rules::handles::{normalise_handle, normalise_handle_in, social_handle, HandleKind, HandleSource};/// What a handle write was asked to hold.#[derive(Debug, Clone, PartialEq, Eq)]pub struct HandleWrite<'a> {    pub namespace: &'a Namespace,    pub workspace_id: &'a str,    pub person_id: &'a str,    pub kind: HandleKind,    /// As the source spelled it — what the page shows.    pub value: &'a str,    pub normalised: &'a str,    pub source: HandleSource,    pub verified_at: Option<&'a str>,    pub position: i64,    pub now: &'a str,}/// The row for one handle, under its derived id — so two devices adding the/// same handle write one row, and the unique key is never raced.pub fn handle_change(write: &HandleWrite<'_>) -> LocalChange {    let id = person_handle_id(write.namespace, write.kind.as_str(), write.normalised);    let mut patch = Map::new();    patch.insert("id".into(), Value::from(id.as_str()));    patch.insert("workspace_id".into(), Value::from(write.workspace_id));    patch.insert("person_id".into(), Value::from(write.person_id));    patch.insert("kind".into(), Value::from(write.kind.as_str()));    patch.insert("value".into(), Value::from(write.value.trim()));    patch.insert("normalised".into(), Value::from(write.normalised));    patch.insert("source".into(), Value::from(write.source.as_str()));    patch.insert("verified_at".into(), write.verified_at.map_or(Value::Null, Value::from));    patch.insert("position".into(), Value::from(write.position));    patch.insert("created_at".into(), Value::from(write.now));    patch.insert("updated_at".into(), Value::from(write.now));    LocalChange { delete: Default::default(), kind: OpKind::Upsert, table: PERSON_HANDLES.to_string(), row_id: id, patch: Some(patch) }}