Skip to main content

stikk_model/
id.rs

1//! Validated object-id and ref-name newtypes.
2//!
3//! Design `stikk-04` MOD-01. stikk never fabricates an identifier (data model INV-9): an object id
4//! or ref name is either parsed from prikk's own output or rejected. These newtypes keep an
5//! unvalidated string from being mistaken for a real identity, and provide the abbreviation the UI
6//! shows with full-copy on focus (FR-124). They are deliberately permissive about *what* prikk's id
7//! space is (stikk is not prikk's authority on identity) and strict only about the shape prikk
8//! documents: a 64-character lowercase-hex object id, and a namespace-prefixed ref name.
9
10use std::fmt;
11
12use crate::error::StikkError;
13
14/// A prikk object identifier: 64 lowercase hexadecimal characters (a SHA-256 digest in hex).
15///
16/// Validated on construction so a malformed string never travels the UI as if it were an identity.
17#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
18pub struct ObjectId(String);
19
20impl ObjectId {
21    /// Parse a 64-character lowercase-hex object id, rejecting any other shape.
22    ///
23    /// # Errors
24    /// Returns [`StikkError::Environment`] when the string is not exactly 64 lowercase hex
25    /// characters — a shape prikk would never emit, so encountering it means stikk misread prikk's
26    /// output, which is an environment fault, not a user refusal.
27    pub fn parse(value: &str) -> crate::error::Result<Self> {
28        if value.len() != 64 {
29            return Err(StikkError::environment_msg(format!(
30                "object id must be 64 hex characters, got {}",
31                value.len()
32            )));
33        }
34        if !value
35            .bytes()
36            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
37        {
38            return Err(StikkError::environment_msg(
39                "object id must be lowercase hexadecimal".to_string(),
40            ));
41        }
42        Ok(Self(value.to_string()))
43    }
44
45    /// The full 64-character id.
46    #[must_use]
47    pub fn as_str(&self) -> &str {
48        &self.0
49    }
50
51    /// A short prefix for display; the UI pairs this with full-copy on focus (FR-124). Returns the
52    /// whole id if it is somehow shorter than `len` (it never is after construction).
53    #[must_use]
54    pub fn abbreviated(&self, len: usize) -> &str {
55        self.0.get(..len).unwrap_or(&self.0)
56    }
57}
58
59impl fmt::Display for ObjectId {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        f.write_str(&self.0)
62    }
63}
64
65/// A prikk reference name, such as `heads/main`, `tags/v1`, or `remotes/heads/main`.
66///
67/// stikk keeps a *client-side focused ref* (design FR-055) as one of these — a preference, never a
68/// HEAD. Validation here is deliberately light: prikk is the authority on whether a ref exists and
69/// what it points to, so this only rejects the shapes that could not be a ref name at all (empty,
70/// control characters), not the namespace policy prikk itself enforces.
71#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
72pub struct RefName(String);
73
74impl RefName {
75    /// Parse a ref name, rejecting only empty strings and control characters.
76    ///
77    /// # Errors
78    /// Returns [`StikkError::Environment`] for an empty or control-character-bearing string.
79    pub fn parse(value: &str) -> crate::error::Result<Self> {
80        if value.is_empty() {
81            return Err(StikkError::environment_msg("ref name must not be empty"));
82        }
83        if value.chars().any(char::is_control) {
84            return Err(StikkError::environment_msg(
85                "ref name must not contain control characters",
86            ));
87        }
88        Ok(Self(value.to_string()))
89    }
90
91    /// The full ref name.
92    #[must_use]
93    pub fn as_str(&self) -> &str {
94        &self.0
95    }
96
97    /// True when this is a received (`remotes/…`) ref — read-only in prikk until adopted, and
98    /// labelled distinctly by the UI (FR-014).
99    #[must_use]
100    pub fn is_received(&self) -> bool {
101        self.0.starts_with("remotes/")
102    }
103}
104
105impl fmt::Display for RefName {
106    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107        f.write_str(&self.0)
108    }
109}
110
111#[cfg(test)]
112mod tests;