Skip to main content

ic_storage/
key.rs

1use serde::{Deserialize, Deserializer, Serialize, de::Error as _};
2use std::{fmt, str::FromStr};
3
4const MAX_KEY_BYTES: usize = 128;
5
6///
7/// `StableKey`
8///
9/// Canonical durable logical allocation identity.
10///
11/// A stable key names the logical store, not the current storage backend or
12/// allocation ID. Once committed, the key is permanently bound to its
13/// allocation; changing the key declares a new logical store.
14///
15/// Construction and deserialization enforce the same bounded canonical grammar.
16/// A checked key establishes identity syntax, not allocation or open authority.
17///
18
19#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
20pub struct StableKey(String);
21
22impl<'de> Deserialize<'de> for StableKey {
23    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
24        // Retain the serialized newtype shape before checking its owned text.
25        #[derive(Deserialize)]
26        #[serde(rename = "StableKey")]
27        struct Text(String);
28
29        let Text(value) = Text::deserialize(deserializer)?;
30        validate(&value).map_err(D::Error::custom)?;
31        Ok(Self(value))
32    }
33}
34
35impl StableKey {
36    /// Parse and validate a canonical stable key string.
37    ///
38    /// Keys are bounded lowercase ASCII dot-separated names ending in a
39    /// nonzero `.vN` suffix.
40    ///
41    /// # Errors
42    /// Returns an error when the name exceeds the length limit or violates stable-key syntax.
43    pub fn parse(value: impl AsRef<str>) -> Result<Self, StableKeyError> {
44        let value = value.as_ref();
45        validate(value)?;
46        Ok(Self(value.to_string()))
47    }
48
49    /// Borrow the canonical stable-key string.
50    #[must_use]
51    pub fn as_str(&self) -> &str {
52        &self.0
53    }
54
55    /// Consume the key and return the canonical stable-key string.
56    #[must_use]
57    pub fn into_string(self) -> String {
58        self.0
59    }
60}
61
62impl AsRef<str> for StableKey {
63    fn as_ref(&self) -> &str {
64        self.as_str()
65    }
66}
67
68impl fmt::Display for StableKey {
69    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
70        formatter.write_str(self.as_str())
71    }
72}
73
74impl FromStr for StableKey {
75    type Err = StableKeyError;
76
77    fn from_str(value: &str) -> Result<Self, Self::Err> {
78        Self::parse(value)
79    }
80}
81
82///
83/// `StableKeyError`
84///
85/// Stable-key grammar validation failure.
86#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
87#[error("stable key '{stable_key}' is invalid: {reason}")]
88pub struct StableKeyError {
89    /// Rejected stable-key string.
90    pub stable_key: String,
91    /// Stable-key grammar failure.
92    pub reason: &'static str,
93}
94
95pub fn validate(stable_key: &str) -> Result<(), StableKeyError> {
96    if stable_key.is_empty() {
97        return invalid(stable_key, "must not be empty");
98    }
99    if stable_key.len() > MAX_KEY_BYTES {
100        return invalid(stable_key, "must be at most 128 bytes");
101    }
102    if !stable_key.is_ascii() {
103        return invalid(stable_key, "must be ASCII");
104    }
105    if stable_key.bytes().any(|byte| byte.is_ascii_uppercase()) {
106        return invalid(stable_key, "must be lowercase");
107    }
108    if stable_key
109        .bytes()
110        .any(|byte| matches!(byte, b'\t'..=b'\r' | b' '))
111    {
112        return invalid(stable_key, "must not contain whitespace");
113    }
114    if stable_key.bytes().any(|byte| matches!(byte, b'/' | b'-')) {
115        return invalid(stable_key, "must not contain slashes or hyphens");
116    }
117    if stable_key.starts_with('.') || stable_key.ends_with('.') {
118        return invalid(stable_key, "must not start or end with a dot");
119    }
120
121    let Some(version_index) = stable_key
122        .as_bytes()
123        .windows(2)
124        .rposition(|pair| pair == b".v")
125    else {
126        return invalid(stable_key, "must end with .vN");
127    };
128    let version = &stable_key[version_index + 2..];
129    if version.is_empty()
130        || version.starts_with('0')
131        || !version.bytes().all(|byte| byte.is_ascii_digit())
132    {
133        return invalid(stable_key, "version suffix must be nonzero .vN");
134    }
135
136    let prefix = &stable_key[..version_index];
137    if prefix.is_empty() {
138        return invalid(
139            stable_key,
140            "must contain at least one segment before version",
141        );
142    }
143
144    for segment in prefix.as_bytes().split(|byte| *byte == b'.') {
145        if let Err(reason) = validate_segment(segment) {
146            return invalid(stable_key, reason);
147        }
148    }
149
150    Ok(())
151}
152
153// A namespace plus its synthetic `v1` suffix must fit the same key grammar.
154// Borrow the prefix rather than allocating a temporary key just to discard it.
155pub fn is_key_prefix(value: &str) -> bool {
156    let Some(prefix) = value.strip_suffix('.') else {
157        return false;
158    };
159    value.len() <= MAX_KEY_BYTES - "v1".len()
160        && prefix
161            .as_bytes()
162            .split(|byte| *byte == b'.')
163            .all(|segment| validate_segment(segment).is_ok())
164}
165
166fn validate_segment(segment: &[u8]) -> Result<(), &'static str> {
167    let mut bytes = segment.iter().copied();
168    let Some(first) = bytes.next() else {
169        return Err("must not contain empty segments");
170    };
171    if !first.is_ascii_lowercase() {
172        return Err("segments must start with a lowercase letter");
173    }
174    if !bytes.all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'_') {
175        return Err("segments may contain only lowercase letters, digits, and underscores");
176    }
177    Ok(())
178}
179
180fn invalid<T>(stable_key: &str, reason: &'static str) -> Result<T, StableKeyError> {
181    Err(StableKeyError {
182        stable_key: stable_key.to_string(),
183        reason,
184    })
185}
186
187#[cfg(test)]
188mod tests;