1use std::{
2 marker::PhantomData,
3 ops::{Deref, DerefMut},
4 string::FromUtf8Error,
5};
6
7pub use typelock_derive::LockSchema;
8
9#[cfg(feature = "postcard-codec")]
10pub use typelock_derive::{FromBytes, ToBytes};
11
12mod postcard_codec;
13
14#[derive(Debug)]
15pub enum Error {
16 ByteConversion(String),
17 Postcard(String),
18}
19
20impl From<FromUtf8Error> for Error {
21 fn from(value: FromUtf8Error) -> Self {
22 Error::ByteConversion(value.to_string())
23 }
24}
25
26pub trait CryptProvider {
27 fn encrypt(&self, data: &[u8]) -> Vec<u8>;
28 fn decrypt(&self, data: &[u8]) -> Vec<u8>;
29}
30
31#[derive(Debug, PartialEq, Eq)]
32pub struct Encrypted<T> {
33 pub bytes: Vec<u8>,
34 _marker: PhantomData<T>,
35}
36
37impl<T> Encrypted<T> {
38 pub fn new(bytes: Vec<u8>) -> Self {
39 Self {
40 bytes,
41 _marker: PhantomData::<T>,
42 }
43 }
44}
45
46impl<T> Deref for Encrypted<T> {
47 type Target = Vec<u8>;
48
49 fn deref(&self) -> &Self::Target {
50 &self.bytes
51 }
52}
53
54#[derive(Debug, PartialEq, Eq)]
55pub struct Decrypted<T> {
56 pub data: T,
57}
58
59impl<T> Decrypted<T> {
60 pub fn new(data: T) -> Self {
61 Self { data }
62 }
63}
64
65impl<T> Deref for Decrypted<T> {
66 type Target = T;
67
68 fn deref(&self) -> &Self::Target {
69 &self.data
70 }
71}
72
73impl<T> DerefMut for Decrypted<T> {
74 fn deref_mut(&mut self) -> &mut Self::Target {
75 &mut self.data
76 }
77}
78
79pub trait HashingProvider {
80 fn hash(&self, data: &[u8]) -> Vec<u8>;
81}
82
83#[derive(Debug, PartialEq, Eq)]
84pub struct Hashed<T> {
85 pub bytes: Vec<u8>,
86 _marker: PhantomData<T>,
87}
88
89impl<T> Hashed<T> {
90 pub fn new(bytes: Vec<u8>) -> Self {
91 Self {
92 bytes: bytes.to_vec(),
93 _marker: PhantomData::<T>,
94 }
95 }
96}
97
98impl<T> Deref for Hashed<T> {
99 type Target = Vec<u8>;
100
101 fn deref(&self) -> &Self::Target {
102 &self.bytes
103 }
104}
105
106pub trait Lockable<P> {
107 type Output;
108
109 fn lock(self, provider: &P) -> Result<Self::Output, crate::Error>;
110}
111
112impl<P, T> Lockable<P> for Vec<T>
113where
114 T: Lockable<P>,
115{
116 type Output = Vec<T::Output>;
117
118 #[inline]
119 fn lock(self, provider: &P) -> Result<Self::Output, crate::Error> {
120 self.into_iter().map(|m| m.lock(provider)).collect()
121 }
122}
123
124pub trait Unlockable<P> {
125 type Output;
126
127 fn unlock(self, provider: &P) -> Result<Self::Output, crate::Error>;
128}
129
130impl<P, T> Unlockable<P> for Vec<T>
131where
132 T: Unlockable<P>,
133{
134 type Output = Vec<T::Output>;
135
136 #[inline]
137 fn unlock(self, provider: &P) -> Result<Self::Output, crate::Error> {
138 self.into_iter().map(|m| m.unlock(provider)).collect()
139 }
140}
141
142pub trait ToBytes {
143 fn to_bytes(&self) -> Result<Vec<u8>, crate::Error>;
144}
145
146impl ToBytes for String {
147 fn to_bytes(&self) -> Result<Vec<u8>, crate::Error> {
148 Ok(self.as_bytes().to_vec())
149 }
150}
151
152impl<T> ToBytes for Decrypted<T>
153where
154 T: ToBytes,
155{
156 fn to_bytes(&self) -> Result<Vec<u8>, crate::Error> {
157 self.deref().to_bytes()
158 }
159}
160
161pub trait FromBytes: Sized {
162 fn from_bytes(bytes: &[u8]) -> Result<Self, crate::Error>;
163}
164
165impl FromBytes for String {
166 fn from_bytes(bytes: &[u8]) -> Result<Self, crate::Error> {
167 Ok(String::from_utf8(bytes.to_vec())?)
168 }
169}