From 365be3870e999e137dc731d561477fad9e30ec2d Mon Sep 17 00:00:00 2001 From: Codex Bot Date: Mon, 30 Nov 2026 18:19:05 +0800 Subject: [PATCH] Model workspaces, users, and organizations --- crates/documents/src/document.rs | 36 +++++++++++++ crates/documents/src/id.rs | 20 ++++++++ crates/documents/src/lib.rs | 75 ++++------------------------ crates/documents/src/organization.rs | 42 ++++++++++++++++ crates/documents/src/user.rs | 24 +++++++++ crates/documents/src/workspace.rs | 29 +++++++++++ 6 files changed, 162 insertions(+), 64 deletions(-) create mode 100644 crates/documents/src/document.rs create mode 100644 crates/documents/src/id.rs create mode 100644 crates/documents/src/organization.rs create mode 100644 crates/documents/src/user.rs create mode 100644 crates/documents/src/workspace.rs diff --git a/crates/documents/src/document.rs b/crates/documents/src/document.rs new file mode 100644 index 0000000..0a143e6 --- /dev/null +++ b/crates/documents/src/document.rs @@ -0,0 +1,36 @@ +use serde_json::Value; + +use super::{ + id::{IdError, validate_id}, + workspace::WorkspaceId, +}; + +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +pub struct DocumentId(String); + +impl DocumentId { + pub const MAX_BYTES: usize = 128; + + pub fn new(value: impl Into) -> Result { + let value = value.into(); + validate_id(&value, Self::MAX_BYTES, IdError::Document)?; + Ok(Self(value)) + } + + pub fn as_str(&self) -> &str { + &self.0 + } +} + +/// A document ID identifies a document within its workspace. +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +pub struct DocumentKey { + pub workspace: WorkspaceId, + pub document: DocumentId, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct Document { + pub key: DocumentKey, + pub body: Value, +} diff --git a/crates/documents/src/id.rs b/crates/documents/src/id.rs new file mode 100644 index 0000000..6adb466 --- /dev/null +++ b/crates/documents/src/id.rs @@ -0,0 +1,20 @@ +use thiserror::Error; + +#[derive(Debug, Error, PartialEq, Eq)] +pub enum IdError { + #[error("a workspace ID must contain 1–96 bytes and no NUL")] + Workspace, + #[error("a document ID must contain 1–128 bytes and no NUL")] + Document, + #[error("a user ID must contain 1–128 bytes and no NUL")] + User, + #[error("an organization ID must contain 1–128 bytes and no NUL")] + Organization, +} + +pub(super) fn validate_id(value: &str, max_bytes: usize, error: IdError) -> Result<(), IdError> { + if value.is_empty() || value.len() > max_bytes || value.contains('\0') { + return Err(error); + } + Ok(()) +} diff --git a/crates/documents/src/lib.rs b/crates/documents/src/lib.rs index 2b2329b..6e161eb 100644 --- a/crates/documents/src/lib.rs +++ b/crates/documents/src/lib.rs @@ -1,66 +1,13 @@ -//! Workspace document identities and contents. +//! Logical models for documents, workspaces, users, and organizations. -use serde_json::Value; -use thiserror::Error; +mod document; +mod id; +mod organization; +mod user; +mod workspace; -#[derive(Debug, Error, PartialEq, Eq)] -pub enum IdError { - #[error("a workspace ID must contain 1–96 bytes and no NUL")] - Workspace, - #[error("a document ID must contain 1–128 bytes and no NUL")] - Document, -} - -#[derive(Clone, Debug, Eq, Hash, PartialEq)] -pub struct WorkspaceId(String); - -impl WorkspaceId { - pub const MAX_BYTES: usize = 96; - - pub fn new(value: impl Into) -> Result { - let value = value.into(); - validate_id(&value, Self::MAX_BYTES, IdError::Workspace)?; - Ok(Self(value)) - } - - pub fn as_str(&self) -> &str { - &self.0 - } -} - -#[derive(Clone, Debug, Eq, Hash, PartialEq)] -pub struct DocumentId(String); - -impl DocumentId { - pub const MAX_BYTES: usize = 128; - - pub fn new(value: impl Into) -> Result { - let value = value.into(); - validate_id(&value, Self::MAX_BYTES, IdError::Document)?; - Ok(Self(value)) - } - - pub fn as_str(&self) -> &str { - &self.0 - } -} - -/// A document ID identifies a document within its workspace. -#[derive(Clone, Debug, Eq, Hash, PartialEq)] -pub struct DocumentKey { - pub workspace: WorkspaceId, - pub document: DocumentId, -} - -#[derive(Clone, Debug, PartialEq)] -pub struct Document { - pub key: DocumentKey, - pub body: Value, -} - -fn validate_id(value: &str, max_bytes: usize, error: IdError) -> Result<(), IdError> { - if value.is_empty() || value.len() > max_bytes || value.contains('\0') { - return Err(error); - } - Ok(()) -} +pub use document::{Document, DocumentId, DocumentKey}; +pub use id::IdError; +pub use organization::{Organization, OrganizationId, OrganizationMembership, OrganizationRole}; +pub use user::{User, UserId}; +pub use workspace::{Workspace, WorkspaceId}; diff --git a/crates/documents/src/organization.rs b/crates/documents/src/organization.rs new file mode 100644 index 0000000..0407d15 --- /dev/null +++ b/crates/documents/src/organization.rs @@ -0,0 +1,42 @@ +use super::{ + id::{IdError, validate_id}, + user::UserId, +}; + +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +pub struct OrganizationId(String); + +impl OrganizationId { + pub const MAX_BYTES: usize = 128; + + pub fn new(value: impl Into) -> Result { + let value = value.into(); + validate_id(&value, Self::MAX_BYTES, IdError::Organization)?; + Ok(Self(value)) + } + + pub fn as_str(&self) -> &str { + &self.0 + } +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct Organization { + pub id: OrganizationId, + pub name: String, +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum OrganizationRole { + Owner, + Admin, + Member, +} + +/// Links a user to an organization with a role in that organization. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct OrganizationMembership { + pub organization_id: OrganizationId, + pub user_id: UserId, + pub role: OrganizationRole, +} diff --git a/crates/documents/src/user.rs b/crates/documents/src/user.rs new file mode 100644 index 0000000..9e1be6e --- /dev/null +++ b/crates/documents/src/user.rs @@ -0,0 +1,24 @@ +use super::id::{IdError, validate_id}; + +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +pub struct UserId(String); + +impl UserId { + pub const MAX_BYTES: usize = 128; + + pub fn new(value: impl Into) -> Result { + let value = value.into(); + validate_id(&value, Self::MAX_BYTES, IdError::User)?; + Ok(Self(value)) + } + + pub fn as_str(&self) -> &str { + &self.0 + } +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct User { + pub id: UserId, + pub display_name: String, +} diff --git a/crates/documents/src/workspace.rs b/crates/documents/src/workspace.rs new file mode 100644 index 0000000..dc701fa --- /dev/null +++ b/crates/documents/src/workspace.rs @@ -0,0 +1,29 @@ +use super::{ + id::{IdError, validate_id}, + organization::OrganizationId, +}; + +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +pub struct WorkspaceId(String); + +impl WorkspaceId { + pub const MAX_BYTES: usize = 96; + + pub fn new(value: impl Into) -> Result { + let value = value.into(); + validate_id(&value, Self::MAX_BYTES, IdError::Workspace)?; + Ok(Self(value)) + } + + pub fn as_str(&self) -> &str { + &self.0 + } +} + +/// A workspace belongs to one organization and contains documents. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct Workspace { + pub id: WorkspaceId, + pub organization_id: OrganizationId, + pub name: String, +}