merry_runtime/
checkpoint.rs1use thiserror::Error;
2
3mod reference;
4
5pub use reference::{CheckpointRef, CheckpointRefManifest, CheckpointSourceKind};
6
7#[derive(Debug, Clone, PartialEq, Eq, Error)]
8pub enum CheckpointError {
9 #[error("{field} must not be blank")]
10 BlankField { field: &'static str },
11
12 #[error("{field} must be a provider-safe identifier")]
13 InvalidIdentifier { field: &'static str },
14
15 #[error("checkpoint sequence range start {start} must be <= end {end}")]
16 InvalidSequenceRange { start: u64, end: u64 },
17
18 #[error("checkpoint ref {ref_id} is duplicated")]
19 DuplicateRef { ref_id: String },
20}
21
22#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
23pub struct CheckpointId(String);
24
25#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
26pub struct CheckpointRefId(String);
27
28macro_rules! impl_checkpoint_id {
29 ($type:ident, $field:literal) => {
30 impl $type {
31 pub fn new(value: &str) -> Result<Self, CheckpointError> {
32 validate_identifier($field, value)?;
33 Ok(Self(value.to_owned()))
34 }
35
36 #[must_use]
37 pub fn as_str(&self) -> &str {
38 &self.0
39 }
40 }
41 };
42}
43
44impl_checkpoint_id!(CheckpointId, "checkpoint id");
45impl_checkpoint_id!(CheckpointRefId, "checkpoint ref id");
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub struct CheckpointSequenceRange {
49 start: u64,
50 end: u64,
51}
52
53impl CheckpointSequenceRange {
54 pub fn new(start: u64, end: u64) -> Result<Self, CheckpointError> {
55 if start > end {
56 return Err(CheckpointError::InvalidSequenceRange { start, end });
57 }
58 Ok(Self { start, end })
59 }
60
61 #[must_use]
62 pub fn start(&self) -> u64 {
63 self.start
64 }
65
66 #[must_use]
67 pub fn end(&self) -> u64 {
68 self.end
69 }
70}
71
72pub(super) fn validate_identifier(field: &'static str, value: &str) -> Result<(), CheckpointError> {
73 if value.trim().is_empty() {
74 return Err(CheckpointError::BlankField { field });
75 }
76 if value.trim() != value {
77 return Err(CheckpointError::InvalidIdentifier { field });
78 }
79 if value
80 .bytes()
81 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b':'))
82 {
83 return Ok(());
84 }
85 Err(CheckpointError::InvalidIdentifier { field })
86}