Add a workspace document model and SQL storage adapter
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "documents"
|
||||
version = "0.1.0"
|
||||
edition.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
serde_json.workspace = true
|
||||
thiserror.workspace = true
|
||||
@@ -0,0 +1,66 @@
|
||||
//! Workspace document identities and contents.
|
||||
|
||||
use serde_json::Value;
|
||||
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,
|
||||
}
|
||||
|
||||
#[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(())
|
||||
}
|
||||
Reference in New Issue
Block a user