fire_postgres/
impl_crypto.rs

1#[cfg(feature = "crypto-cipher")]
2mod cipher {
3
4	use crate::table::column::{
5		ColumnData, ColumnKind, ColumnType, FromDataError,
6	};
7	use crypto::cipher::{Keypair, PublicKey};
8	use std::str::FromStr;
9
10	impl ColumnType for Keypair {
11		fn column_kind() -> ColumnKind {
12			ColumnKind::FixedText(43)
13		}
14
15		fn to_data(&self) -> ColumnData<'_> {
16			ColumnData::Text(self.to_string().into())
17		}
18
19		fn from_data(data: ColumnData<'_>) -> Result<Self, FromDataError> {
20			match data {
21				ColumnData::Text(t) => {
22					Self::from_str(t.as_str()).map_err(|_| {
23						FromDataError::Custom(
24							"could not derive keypair from string",
25						)
26					})
27				}
28				_ => Err(FromDataError::ExpectedType("Keypair text")),
29			}
30		}
31	}
32
33	impl ColumnType for PublicKey {
34		fn column_kind() -> ColumnKind {
35			ColumnKind::FixedText(43)
36		}
37
38		fn to_data(&self) -> ColumnData<'_> {
39			ColumnData::Text(self.to_string().into())
40		}
41
42		fn from_data(data: ColumnData<'_>) -> Result<Self, FromDataError> {
43			match data {
44				ColumnData::Text(t) => {
45					Self::from_str(t.as_str()).map_err(|_| {
46						FromDataError::Custom(
47							"could not derive publickey from string",
48						)
49					})
50				}
51				_ => Err(FromDataError::ExpectedType("PublicKey text")),
52			}
53		}
54	}
55}
56
57#[cfg(feature = "crypto-signature")]
58mod signature {
59
60	use crate::table::column::{
61		ColumnData, ColumnKind, ColumnType, FromDataError,
62	};
63	use crypto::signature::{Keypair, PublicKey, Signature};
64	use std::str::FromStr;
65
66	impl ColumnType for Keypair {
67		fn column_kind() -> ColumnKind {
68			ColumnKind::FixedText(43)
69		}
70
71		fn to_data(&self) -> ColumnData<'_> {
72			ColumnData::Text(self.to_string().into())
73		}
74
75		fn from_data(data: ColumnData<'_>) -> Result<Self, FromDataError> {
76			match data {
77				ColumnData::Text(t) => {
78					Self::from_str(t.as_str()).map_err(|_| {
79						FromDataError::Custom(
80							"could not derive keypair from string",
81						)
82					})
83				}
84				_ => Err(FromDataError::ExpectedType("Keypair text")),
85			}
86		}
87	}
88
89	impl ColumnType for PublicKey {
90		fn column_kind() -> ColumnKind {
91			ColumnKind::FixedText(43)
92		}
93
94		fn to_data(&self) -> ColumnData<'_> {
95			ColumnData::Text(self.to_string().into())
96		}
97
98		fn from_data(data: ColumnData<'_>) -> Result<Self, FromDataError> {
99			match data {
100				ColumnData::Text(t) => {
101					Self::from_str(t.as_str()).map_err(|_| {
102						FromDataError::Custom(
103							"could not derive publickey from string",
104						)
105					})
106				}
107				_ => Err(FromDataError::ExpectedType("PublicKey text")),
108			}
109		}
110	}
111
112	impl ColumnType for Signature {
113		fn column_kind() -> ColumnKind {
114			ColumnKind::FixedText(86)
115		}
116
117		fn to_data(&self) -> ColumnData<'_> {
118			ColumnData::Text(self.to_string().into())
119		}
120
121		fn from_data(data: ColumnData<'_>) -> Result<Self, FromDataError> {
122			match data {
123				ColumnData::Text(t) => {
124					Self::from_str(t.as_str()).map_err(|_| {
125						FromDataError::Custom(
126							"could not derive publickey from string",
127						)
128					})
129				}
130				_ => Err(FromDataError::ExpectedType("PublicKey text")),
131			}
132		}
133	}
134}
135
136#[cfg(feature = "crypto-token")]
137mod token {
138
139	use crate::table::column::{
140		ColumnData, ColumnKind, ColumnType, FromDataError,
141	};
142	use crypto::token::Token;
143	use std::str::FromStr;
144
145	impl<const S: usize> ColumnType for Token<S> {
146		fn column_kind() -> ColumnKind {
147			ColumnKind::FixedText(Self::STR_LEN)
148		}
149
150		fn to_data(&self) -> ColumnData<'_> {
151			ColumnData::Text(self.to_string().into())
152		}
153
154		fn from_data(data: ColumnData<'_>) -> Result<Self, FromDataError> {
155			match data {
156				ColumnData::Text(t) => {
157					Self::from_str(t.as_str()).map_err(|_| {
158						FromDataError::Custom(
159							"could not derive keypair from string",
160						)
161					})
162				}
163				_ => Err(FromDataError::ExpectedType("Keypair text")),
164			}
165		}
166	}
167}