libes/key/
conversion.rs

1use super::generics::Key;
2use crate::KeyError;
3
4/// A value -> `PublicKey` conversion that consumes the input value. The opposite of [IntoPublicKey].
5pub trait PublicKeyFrom<T>: Key + Sized {
6    fn pk_from(x: T) -> Self;
7}
8
9impl<T: Key> PublicKeyFrom<T> for T {
10    fn pk_from(x: T) -> Self {
11        x
12    }
13}
14
15/// A value -> `PublicKey` conversion that consumes the input value. The opposite of [PublicKeyFrom].
16pub trait IntoPublicKey<U: Key> {
17    fn into_pk(self) -> U;
18}
19
20impl<T, U> IntoPublicKey<U> for T
21where
22    U: PublicKeyFrom<T>,
23{
24    fn into_pk(self) -> U {
25        U::pk_from(self)
26    }
27}
28
29/// Attempt a value -> `PublicKey` conversion that consumes the input value. The opposite of [TryIntoPublicKey].
30pub trait TryPublicKeyFrom<T>: Key + Sized {
31    fn try_pk_from(x: T) -> Result<Self, KeyError>;
32}
33
34/// Attempt a value -> `PublicKey` conversion that consumes the input value. The opposite of [TryPublicKeyFrom].
35pub trait TryIntoPublicKey<U: Key> {
36    fn try_into_pk(self) -> Result<U, KeyError>;
37}
38
39impl<T, U> TryIntoPublicKey<U> for T
40where
41    U: TryPublicKeyFrom<T>,
42{
43    fn try_into_pk(self) -> Result<U, KeyError> {
44        U::try_pk_from(self)
45    }
46}
47
48/// A value -> `SecretKey` conversion that consumes the input value. The opposite of [IntoSecretKey].
49pub trait SecretKeyFrom<T>: Key {
50    fn sk_from(x: T) -> Self::SecretKey;
51}
52
53/// A value -> `SecretKey` conversion that consumes the input value. The opposite of [SecretKeyFrom].
54pub trait IntoSecretKey<U: Key> {
55    fn into_sk(self) -> U::SecretKey;
56}
57
58impl<T, U> IntoSecretKey<U> for T
59where
60    U: SecretKeyFrom<T>,
61{
62    fn into_sk(self) -> U::SecretKey {
63        U::sk_from(self)
64    }
65}
66
67/// A &value -> &`SecretKey` conversion that consumes the input value. The opposite of [IntoSecretKey].
68pub trait SecretKeyFromRef<T>: Key {
69    fn sk_from_ref(x: &T) -> &Self::SecretKey;
70}
71
72/// A &value -> &`SecretKey` conversion that consumes the input value. The opposite of [SecretKeyFrom].
73pub trait IntoSecretKeyRef<U: Key> {
74    fn into_sk_ref(&self) -> &U::SecretKey;
75}
76
77impl<T, U> IntoSecretKeyRef<U> for T
78    where
79        U: SecretKeyFromRef<T>,
80{
81    fn into_sk_ref(&self) -> &U::SecretKey {
82        U::sk_from_ref(self)
83    }
84}
85
86/// Attempt a value -> `SecretKey` conversion that consumes the input value. The opposite of [TryIntoSecretKey].
87pub trait TrySecretKeyFrom<T>: Key {
88    fn try_sk_from(x: T) -> Result<Self::SecretKey, KeyError>;
89}
90
91/// Attempt a value -> `SecretKey` conversion that consumes the input value. The opposite of [TrySecretKeyFrom].
92pub trait TryIntoSecretKey<U: Key> {
93    fn try_into_sk(self) -> Result<U::SecretKey, KeyError>;
94}
95
96impl<T, U> TryIntoSecretKey<U> for T
97where
98    U: TrySecretKeyFrom<T>,
99{
100    fn try_into_sk(self) -> Result<U::SecretKey, KeyError> {
101        U::try_sk_from(self)
102    }
103}