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; [`EditorId`] is the
16//! client-chosen key of one open editor holding a document lock.
17//!
18//! Copyright (c) systemprompt.io — Business Source License 1.1.
19//! See <https://systemprompt.io> for licensing details.
20
21use crate::error::IdValidationError;
22
23crate::define_id!(DocumentId, uuid);
24crate::define_id!(DocumentFolderId, uuid);
25crate::define_id!(DocumentRevisionId, uuid);
26crate::define_id!(DocumentEditSessionId, uuid);
27crate::define_id!(DocumentCommentId, uuid);
28crate::define_id!(DocumentProposalId, uuid);
29crate::define_id!(DocumentAccessRequestId, uuid);
30crate::define_id!(DocumentUploadId, uuid);
31crate::define_id!(IssueReportId, uuid);
32crate::define_id!(ContentHash, checked, validate_content_hash);
33crate::define_id!(DocumentNodeId, checked, |value| {
34    crate::macros::validate_non_empty("DocumentNodeId", value)
35});
36crate::define_id!(DocumentOpId, checked, |value| {
37    crate::macros::validate_non_empty("DocumentOpId", value)
38});
39crate::define_id!(DocumentTemplateId, checked, |value| {
40    crate::macros::validate_non_empty("DocumentTemplateId", value)
41});
42crate::define_id!(EditorId, checked, |value| {
43    crate::macros::validate_non_empty("EditorId", value)
44});
45crate::define_id!(DriveDeviceId, checked, |value| {
46    crate::macros::validate_non_empty("DriveDeviceId", value)
47});
48
49const CONTENT_HASH_LEN: usize = 64;
50
51fn validate_content_hash(value: &str) -> Result<(), IdValidationError> {
52    if value.is_empty() {
53        return Err(IdValidationError::empty("ContentHash"));
54    }
55    if value.len() != CONTENT_HASH_LEN {
56        return Err(IdValidationError::invalid(
57            "ContentHash",
58            "must be exactly 64 hex characters",
59        ));
60    }
61    if !value
62        .bytes()
63        .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
64    {
65        return Err(IdValidationError::invalid(
66            "ContentHash",
67            "must be lowercase hex",
68        ));
69    }
70    Ok(())
71}