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, and resumable uploads, 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!(ContentHash, checked, validate_content_hash);
31crate::define_id!(DocumentNodeId, checked, |value| {
32    crate::macros::validate_non_empty("DocumentNodeId", value)
33});
34crate::define_id!(DocumentOpId, checked, |value| {
35    crate::macros::validate_non_empty("DocumentOpId", value)
36});
37crate::define_id!(DocumentTemplateId, checked, |value| {
38    crate::macros::validate_non_empty("DocumentTemplateId", value)
39});
40crate::define_id!(DriveDeviceId, checked, |value| {
41    crate::macros::validate_non_empty("DriveDeviceId", value)
42});
43
44const CONTENT_HASH_LEN: usize = 64;
45
46fn validate_content_hash(value: &str) -> Result<(), IdValidationError> {
47    if value.is_empty() {
48        return Err(IdValidationError::empty("ContentHash"));
49    }
50    if value.len() != CONTENT_HASH_LEN {
51        return Err(IdValidationError::invalid(
52            "ContentHash",
53            "must be exactly 64 hex characters",
54        ));
55    }
56    if !value
57        .bytes()
58        .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
59    {
60        return Err(IdValidationError::invalid(
61            "ContentHash",
62            "must be lowercase hex",
63        ));
64    }
65    Ok(())
66}