1use crate::{Account, Error, FieldType, IndexValue, Principal, Subaccount, U256, Ulid, key};
5
6#[repr(transparent)]
16#[derive(serde::Serialize, serde::Deserialize)]
17#[serde(
18 transparent,
19 bound(
20 serialize = "E::Key: serde::Serialize",
21 deserialize = "E::Key: serde::Deserialize<'de>"
22 )
23)]
24pub struct Id<E: crate::Entity>(E::Key);
25
26impl<E: crate::Entity> Id<E> {
27 pub const fn from_key(key: E::Key) -> Self {
29 Self(key)
30 }
31
32 pub const fn key(self) -> E::Key {
34 self.0
35 }
36}
37impl<E: crate::Entity> Copy for Id<E> {}
38impl<E: crate::Entity> Clone for Id<E> {
39 fn clone(&self) -> Self {
40 *self
41 }
42}
43impl<E: crate::Entity> PartialEq for Id<E> {
44 fn eq(&self, other: &Self) -> bool {
45 self.0 == other.0
46 }
47}
48impl<E: crate::Entity> Eq for Id<E> {}
49impl<E: crate::Entity> std::fmt::Debug for Id<E>
50where
51 E::Key: std::fmt::Debug,
52{
53 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54 self.0.fmt(formatter)
55 }
56}
57
58mod sealed {
59 pub trait Sealed {}
60}
61pub trait PrimaryKey: sealed::Sealed + Copy + Eq {
64 #[doc(hidden)]
66 fn into_row_key(self) -> RowKey;
67
68 #[doc(hidden)]
70 fn from_row_key(key: RowKey) -> Result<Self, Error>;
71}
72macro_rules! primary_keys {
73 ($( $variant:ident($ty:ty) ),* $(,)?) => {
74 #[doc(hidden)]
76 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
77 pub enum RowKey { $( $variant($ty), )* }
78 $(
79 impl<E: crate::Entity<Key = $ty>> From<$ty> for Id<E> {
80 fn from(key: $ty) -> Self { Self::from_key(key) }
81 }
82 impl sealed::Sealed for $ty {}
83 impl PrimaryKey for $ty {
84 fn into_row_key(self) -> RowKey { RowKey::$variant(self) }
85
86 fn from_row_key(key: RowKey) -> Result<Self, Error> {
87 match key { RowKey::$variant(value) => Ok(value), _ => Err(Error::Corrupt) }
88 }
89 }
90 )*
91 impl RowKey {
92 pub(crate) fn supports(field: FieldType) -> bool {
93 matches!(field, $( FieldType::$variant )|*)
94 }
95
96 pub(crate) fn from_index(value: IndexValue<'_>) -> Result<Self, Error> {
97 match value { $( IndexValue::$variant(value) => Ok(Self::$variant(value)), )* _ => Err(Error::Corrupt) }
98 }
99
100 pub(crate) fn index_value(self) -> IndexValue<'static> {
101 match self { $( Self::$variant(value) => value.into(), )* }
102 }
103 }
104 };
105}
106primary_keys!(
107 Nat8(u8),
108 Nat16(u16),
109 Nat32(u32),
110 Nat64(u64),
111 Nat128(u128),
112 Int8(i8),
113 Int16(i16),
114 Int32(i32),
115 Int64(i64),
116 Int128(i128),
117 U256(U256),
118 Ulid(Ulid),
119 Principal(Principal),
120 Subaccount(Subaccount),
121 Account(Account)
122);
123impl RowKey {
124 pub(crate) fn decode(bytes: &[u8], field: FieldType) -> Result<Self, Error> {
125 match field {
126 FieldType::Nat8 => Ok(Self::Nat8(u8::from_be_bytes(
127 bytes.try_into().map_err(|_| Error::Corrupt)?,
128 ))),
129 FieldType::Nat16 => Ok(Self::Nat16(u16::from_be_bytes(
130 bytes.try_into().map_err(|_| Error::Corrupt)?,
131 ))),
132 FieldType::Nat32 => Ok(Self::Nat32(u32::from_be_bytes(
133 bytes.try_into().map_err(|_| Error::Corrupt)?,
134 ))),
135 FieldType::Nat64 => Ok(Self::Nat64(u64::from_be_bytes(
136 bytes.try_into().map_err(|_| Error::Corrupt)?,
137 ))),
138 FieldType::Nat128 => Ok(Self::Nat128(u128::from_be_bytes(
139 bytes.try_into().map_err(|_| Error::Corrupt)?,
140 ))),
141 FieldType::Int8 => {
142 let mut bytes: [u8; 1] = bytes.try_into().map_err(|_| Error::Corrupt)?;
143 bytes[0] ^= 0x80;
144
145 Ok(Self::Int8(i8::from_be_bytes(bytes)))
146 }
147 FieldType::Int16 => {
148 let mut bytes: [u8; 2] = bytes.try_into().map_err(|_| Error::Corrupt)?;
149 bytes[0] ^= 0x80;
150
151 Ok(Self::Int16(i16::from_be_bytes(bytes)))
152 }
153 FieldType::Int32 => {
154 let mut bytes: [u8; 4] = bytes.try_into().map_err(|_| Error::Corrupt)?;
155 bytes[0] ^= 0x80;
156
157 Ok(Self::Int32(i32::from_be_bytes(bytes)))
158 }
159 FieldType::Int64 => {
160 let mut bytes: [u8; 8] = bytes.try_into().map_err(|_| Error::Corrupt)?;
161 bytes[0] ^= 0x80;
162
163 Ok(Self::Int64(i64::from_be_bytes(bytes)))
164 }
165 FieldType::Int128 => {
166 let mut bytes: [u8; 16] = bytes.try_into().map_err(|_| Error::Corrupt)?;
167 bytes[0] ^= 0x80;
168
169 Ok(Self::Int128(i128::from_be_bytes(bytes)))
170 }
171 FieldType::U256 => Ok(Self::U256(U256::from_be_bytes(
172 bytes.try_into().map_err(|_| Error::Corrupt)?,
173 ))),
174 FieldType::Ulid => Ok(Self::Ulid(Ulid::from_bytes(
175 bytes.try_into().map_err(|_| Error::Corrupt)?,
176 ))),
177 FieldType::Subaccount => Ok(Self::Subaccount(
178 bytes.try_into().map_err(|_| Error::Corrupt)?,
179 )),
180 FieldType::Principal | FieldType::Account => {
181 let mut remaining = bytes;
182 key::consume(&mut remaining, field)?;
183 if !remaining.is_empty() {
184 return Err(Error::Corrupt);
185 }
186 let owner_end = 1 + usize::from(bytes[0]);
189 let owner =
190 Principal::try_from_slice(&bytes[1..owner_end]).map_err(|_| Error::Corrupt)?;
191
192 if field == FieldType::Principal {
193 Ok(Self::Principal(owner))
194 } else {
195 let subaccount: Subaccount =
196 bytes[owner_end..].try_into().map_err(|_| Error::Corrupt)?;
197
198 Ok(Self::Account(Account {
199 owner,
200 subaccount: (subaccount != [0; 32]).then_some(subaccount),
201 }))
202 }
203 }
204 _ => Err(Error::Corrupt),
205 }
206 }
207
208 pub(crate) fn append_to(self, bytes: &mut Vec<u8>) {
209 self.index_value().append_component(false, bytes);
210 }
211
212 pub(crate) fn encode(self) -> Vec<u8> {
213 let mut bytes = Vec::new();
214 self.append_to(&mut bytes);
215
216 bytes
217 }
218}