zhold_core/identity/
id.rs1use std::{
2 fmt::{Display, Formatter},
3 str::FromStr,
4};
5
6use serde::{Deserialize, Serialize};
7
8use super::{digest::digest, parse::validate_identity};
9
10use crate::ParseIdentityError;
11
12macro_rules! identity_type {
13 ($name:ident, $namespace:literal, $description:literal) => {
14 #[doc = $description]
15 #[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
16 #[serde(transparent)]
17 pub struct $name(String);
18
19 impl $name {
20 pub fn derive(description: &str) -> Self {
22 Self(digest($namespace, &[description]))
23 }
24
25 pub fn as_str(&self) -> &str {
27 &self.0
28 }
29 }
30
31 impl Display for $name {
32 fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
33 formatter.write_str(&self.0)
34 }
35 }
36
37 impl FromStr for $name {
38 type Err = ParseIdentityError;
39
40 fn from_str(value: &str) -> Result<Self, Self::Err> {
41 validate_identity(value)?;
42 Ok(Self(value.to_owned()))
43 }
44 }
45 };
46}
47
48identity_type!(
49 RepositoryId,
50 "zhold.repository.v1",
51 "Stable identity for one local Git common directory."
52);
53identity_type!(
54 WorktreeId,
55 "zhold.worktree.v1",
56 "Stable identity for one canonical Git worktree root."
57);
58identity_type!(
59 WorkspaceId,
60 "zhold.workspace.v1",
61 "Stable identity for one canonical Cargo workspace root."
62);
63identity_type!(
64 ToolchainId,
65 "zhold.toolchain.v1",
66 "Stable identity for a complete Rust toolchain description."
67);
68
69#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
71#[serde(transparent)]
72pub struct WorktreeKey(String);
73
74impl WorktreeKey {
75 pub fn derive(repository: &RepositoryId, worktree: &WorktreeId) -> Self {
77 Self(digest(
78 "zhold.worktree-key.v1",
79 &[repository.as_str(), worktree.as_str()],
80 ))
81 }
82
83 pub fn as_str(&self) -> &str {
85 &self.0
86 }
87}
88
89impl Display for WorktreeKey {
90 fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
91 formatter.write_str(&self.0)
92 }
93}
94
95impl FromStr for WorktreeKey {
96 type Err = ParseIdentityError;
97
98 fn from_str(value: &str) -> Result<Self, Self::Err> {
99 validate_identity(value)?;
100 Ok(Self(value.to_owned()))
101 }
102}
103
104#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
106#[serde(transparent)]
107pub struct ArenaId(String);
108
109impl ArenaId {
110 pub fn derive(
112 repository: &RepositoryId,
113 worktree: &WorktreeId,
114 workspace: &WorkspaceId,
115 toolchain: &ToolchainId,
116 ) -> Self {
117 Self(digest(
118 "zhold.arena.v1",
119 &[
120 repository.as_str(),
121 worktree.as_str(),
122 workspace.as_str(),
123 toolchain.as_str(),
124 ],
125 ))
126 }
127
128 pub fn as_str(&self) -> &str {
130 &self.0
131 }
132}
133
134impl Display for ArenaId {
135 fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
136 formatter.write_str(&self.0)
137 }
138}
139
140impl FromStr for ArenaId {
141 type Err = ParseIdentityError;
142
143 fn from_str(value: &str) -> Result<Self, Self::Err> {
144 validate_identity(value)?;
145 Ok(Self(value.to_owned()))
146 }
147}