Model workspaces, users, and organizations

This commit is contained in:
2026-11-30 18:19:05 +08:00
parent deb535c256
commit 365be3870e
6 changed files with 162 additions and 64 deletions
+11 -64
View File
@@ -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<String>) -> Result<Self, IdError> {
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<String>) -> Result<Self, IdError> {
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};