Skip to main content

systemprompt_identifiers/
documents.rs

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