Skip to main content

systemprompt_identifiers/
documents.rs

1//! Shared document store identifiers.
2//!
3//! Documents, folders, revisions, edit sessions, comments, proposals and
4//! access requests, resumable uploads and issue reports, are UUID-backed.
5//! [`ContentHash`] is a lowercase hex SHA-256 digest: the blob address a
6//! revision, a drive manifest entry and a bridge commit all carry, so it is
7//! checked to exactly 64 lowercase hex characters. The digest itself is
8//! computed in `systemprompt_provider_contracts::documents::content_hash`,
9//! keeping this crate free of hashing dependencies.
10//!
11//! [`DocumentNodeId`] is the stable anchor of one node of a document
12//! projection, minted once and carried across revisions;
13//! [`DocumentOpId`] is the caller-chosen key that makes a write idempotent;
14//! [`DocumentTemplateId`] names a registered template; [`DriveDeviceId`]
15//! names the bridge device that committed a revision.
16//!
17//! Copyright (c) systemprompt.io — Business Source License 1.1.
18//! See <https://systemprompt.io> for licensing details.
19
20use crate::error::IdValidationError;
21
22crate::define_id!(DocumentId, uuid);
23crate::define_id!(DocumentFolderId, uuid);
24crate::define_id!(DocumentRevisionId, uuid);
25crate::define_id!(DocumentEditSessionId, uuid);
26crate::define_id!(DocumentCommentId, uuid);
27crate::define_id!(DocumentProposalId, uuid);
28crate::define_id!(DocumentAccessRequestId, uuid);
29crate::define_id!(DocumentUploadId, uuid);
30crate::define_id!(IssueReportId, uuid);
31crate::define_id!(ContentHash, checked, validate_content_hash);
32crate::define_id!(DocumentNodeId, checked, |value| {
33    crate::macros::validate_non_empty("DocumentNodeId", value)
34});
35crate::define_id!(DocumentOpId, checked, |value| {
36    crate::macros::validate_non_empty("DocumentOpId", value)
37});
38crate::define_id!(DocumentTemplateId, checked, |value| {
39    crate::macros::validate_non_empty("DocumentTemplateId", value)
40});
41crate::define_id!(DriveDeviceId, checked, |value| {
42    crate::macros::validate_non_empty("DriveDeviceId", value)
43});
44
45const CONTENT_HASH_LEN: usize = 64;
46
47fn validate_content_hash(value: &str) -> Result<(), IdValidationError> {
48    if value.is_empty() {
49        return Err(IdValidationError::empty("ContentHash"));
50    }
51    if value.len() != CONTENT_HASH_LEN {
52        return Err(IdValidationError::invalid(
53            "ContentHash",
54            "must be exactly 64 hex characters",
55        ));
56    }
57    if !value
58        .bytes()
59        .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
60    {
61        return Err(IdValidationError::invalid(
62            "ContentHash",
63            "must be lowercase hex",
64        ));
65    }
66    Ok(())
67}