Skip to content

Commit 7b30b90

Browse files
author
Nicklas Warming Jacobsen
committed
Remove trait ToPlaintext and call to_owned on protected attributes in the Encryptable derive macro
1 parent f199160 commit 7b30b90

File tree

5 files changed

+15
-48
lines changed

5 files changed

+15
-48
lines changed

cryptonamo-derive/src/encryptable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub(crate) fn derive_encryptable(input: DeriveInput) -> Result<TokenStream, syn:
3636
let attr_ident = format_ident!("{attr}");
3737

3838
quote! {
39-
.add_protected(#attr, |x| cryptonamo::traits::Plaintext::from(&x.#attr_ident))
39+
.add_protected(#attr, |x| cryptonamo::traits::Plaintext::from(x.#attr_ident.to_owned()))
4040
}
4141
})
4242
.chain(plaintext_attributes.iter().map(|attr| {

src/traits/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub use cipherstash_client::encryption::{
44
compound_indexer::{
55
ComposableIndex, ComposablePlaintext, CompoundIndex, ExactIndex, PrefixIndex,
66
},
7-
Plaintext, PlaintextNullVariant, ToPlaintext, TryFromPlaintext,
7+
Plaintext, PlaintextNullVariant, TryFromPlaintext,
88
};
99

1010
mod primary_key;

vendor/cipherstash-client/src/encryption/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use vitur_client::{EncryptPayload, EncryptedRecord};
2727
// Re-exports
2828
pub use self::{
2929
errors::{EncryptionError, TypeParseError},
30-
plaintext::{Plaintext, PlaintextNullVariant, ToPlaintext, TryFromPlaintext},
30+
plaintext::{Plaintext, PlaintextNullVariant, TryFromPlaintext},
3131
};
3232

3333
pub struct Encryption<C: Credentials<Token = ViturToken> = ViturCredentials> {

vendor/cipherstash-client/src/encryption/plaintext/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use zeroize::Zeroize;
77
mod from_conversion;
88
mod to_conversion;
99
pub use from_conversion::TryFromPlaintext;
10-
pub use to_conversion::ToPlaintext;
1110

1211
const VERSION: u8 = 1;
1312

vendor/cipherstash-client/src/encryption/plaintext/to_conversion.rs

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@ use super::{Plaintext, PlaintextNullVariant};
22
use chrono::{DateTime, NaiveDate, Utc};
33
use rust_decimal::Decimal;
44

5-
/// Trait for converting `Self` into `Plaintext`
6-
pub trait ToPlaintext: super::PlaintextNullVariant {
7-
/// Converts `Self` into `Plaintext`
8-
fn to_plaintext(self) -> Plaintext;
9-
}
10-
115
macro_rules! impl_from {
126
($($ty:ty => $variant:ident),*) => {
137
$(
14-
impl ToPlaintext for $ty {
15-
fn to_plaintext(self) -> Plaintext {
16-
Plaintext::$variant(Some(self as _))
8+
impl From<$ty> for Plaintext {
9+
fn from(value: $ty) -> Self {
10+
Plaintext::$variant(Some(value as _))
1711
}
1812
}
1913
)*
@@ -33,51 +27,25 @@ impl_from! {
3327
u64 => BigUInt
3428
}
3529

36-
impl ToPlaintext for &str {
37-
fn to_plaintext(self) -> Plaintext {
38-
self.to_owned().to_plaintext()
39-
}
40-
}
41-
42-
/// Blanket implementation for all references
43-
/// where the referenced type is clonable and implements `ToPlaintext`.
44-
impl<T> ToPlaintext for &T
45-
where
46-
T: ToPlaintext + Clone,
47-
for<'r> &'r T: super::PlaintextNullVariant,
48-
{
49-
fn to_plaintext(self) -> Plaintext {
50-
self.clone().to_plaintext()
30+
impl From<&str> for Plaintext {
31+
fn from(value: &str) -> Self {
32+
Plaintext::Utf8Str(Some(value.to_owned()))
5133
}
5234
}
5335

54-
/// Blanket implementation for `Option<T>` where
55-
/// `T` implements `ToPlaintext`.
56-
impl<T> ToPlaintext for Option<T>
36+
impl<T> From<Option<T>> for Plaintext
5737
where
58-
T: ToPlaintext,
59-
Option<T>: PlaintextNullVariant,
38+
T: Into<Plaintext> + PlaintextNullVariant,
6039
{
61-
fn to_plaintext(self) -> Plaintext {
62-
if let Some(value) = self {
63-
value.to_plaintext()
40+
fn from(value: Option<T>) -> Self {
41+
if let Some(value) = value {
42+
value.into()
6443
} else {
65-
Self::null()
44+
T::null()
6645
}
6746
}
6847
}
6948

70-
/// Blanket implementation of `From<T>` for all
71-
/// `T` that implements `ToPlaintext`
72-
impl<T> From<T> for Plaintext
73-
where
74-
T: ToPlaintext,
75-
{
76-
fn from(value: T) -> Self {
77-
value.to_plaintext()
78-
}
79-
}
80-
8149
#[cfg(test)]
8250
mod test {
8351
use super::*;

0 commit comments

Comments
 (0)