Skip to main content

nodedb_types/id/
document.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Document identifier.
4
5use std::fmt;
6
7use serde::{Deserialize, Serialize};
8
9use super::error::{IdError, validate};
10
11/// Identifies a document/row across all engines.
12#[derive(
13    Debug,
14    Clone,
15    PartialEq,
16    Eq,
17    Hash,
18    Serialize,
19    Deserialize,
20    rkyv::Archive,
21    rkyv::Serialize,
22    rkyv::Deserialize,
23)]
24pub struct DocumentId(String);
25
26impl DocumentId {
27    /// Construct a `DocumentId`, validating the input string.
28    ///
29    /// Returns `Err(IdError)` if the string is empty, exceeds
30    /// [`ID_MAX_LEN`][super::error::ID_MAX_LEN] bytes, or contains a NUL byte.
31    pub fn try_new(id: impl Into<String>) -> Result<Self, IdError> {
32        let s = id.into();
33        validate(&s)?;
34        Ok(Self(s))
35    }
36
37    /// Construct without validation. Caller must guarantee the input was
38    /// already validated by `try_new` (or came from a previously-validated
39    /// source like deserialized wire bytes from a NodeDB server).
40    pub fn from_validated(id: String) -> Self {
41        Self(id)
42    }
43
44    pub fn as_str(&self) -> &str {
45        &self.0
46    }
47}
48
49impl fmt::Display for DocumentId {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        f.write_str(&self.0)
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::super::error::ID_MAX_LEN;
58    use super::*;
59
60    #[test]
61    fn try_new_accepts_valid() {
62        let d = DocumentId::try_new("doc-abc-123").expect("valid");
63        assert_eq!(d.as_str(), "doc-abc-123");
64    }
65
66    #[test]
67    fn try_new_rejects_empty() {
68        assert_eq!(DocumentId::try_new(""), Err(IdError::Empty));
69    }
70
71    #[test]
72    fn try_new_rejects_too_long() {
73        let long = "x".repeat(ID_MAX_LEN + 1);
74        assert!(matches!(
75            DocumentId::try_new(long),
76            Err(IdError::TooLong { .. })
77        ));
78    }
79
80    #[test]
81    fn try_new_rejects_nul() {
82        assert_eq!(DocumentId::try_new("ab\0cd"), Err(IdError::ContainsNul));
83    }
84
85    #[test]
86    fn try_new_accepts_max_length() {
87        let exact = "a".repeat(ID_MAX_LEN);
88        assert!(DocumentId::try_new(exact).is_ok());
89    }
90
91    #[test]
92    fn try_new_accepts_unicode() {
93        assert!(DocumentId::try_new("doc-ünïcödé-001").is_ok());
94    }
95
96    #[test]
97    fn from_validated_does_not_validate() {
98        let oversized = "z".repeat(ID_MAX_LEN * 2);
99        let d = DocumentId::from_validated(oversized.clone());
100        assert_eq!(d.as_str(), oversized);
101    }
102}