radicle-crypto: add Serialize and Deserialize for Signature

Add Serialize and Deserialize for Signature using the the `into` and
`try_from` serde attributes. To achieve this `From` and `TryFrom`
implementations were added between `Signature` and `String`.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
Fintan Halpenny 2022-10-28 13:16:38 +01:00
parent dc79130988
commit 3af5a7084d
No known key found for this signature in database
GPG Key ID: 2552FB6F64066CB7
1 changed files with 16 additions and 1 deletions

View File

@ -63,7 +63,8 @@ where
}
/// Cryptographic signature.
#[derive(PartialEq, Eq, Copy, Clone)]
#[derive(PartialEq, Eq, Copy, Clone, Serialize, Deserialize)]
#[serde(into = "String", try_from = "String")]
pub struct Signature(pub ed25519::Signature);
impl fmt::Display for Signature {
@ -126,6 +127,20 @@ impl TryFrom<&[u8]> for Signature {
}
}
impl From<Signature> for String {
fn from(s: Signature) -> Self {
s.to_string()
}
}
impl TryFrom<String> for Signature {
type Error = SignatureError;
fn try_from(s: String) -> Result<Self, Self::Error> {
Self::from_str(&s)
}
}
/// The public/verification key.
#[derive(Serialize, Deserialize, Eq, Copy, Clone)]
#[serde(into = "String", try_from = "String")]