From 6eee4b0b65cfa778665d5a9eb4e2637f1281daeb Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Sat, 16 Jan 2021 00:43:41 +0900 Subject: [PATCH] Define custom errors --- plume-common/src/activity_pub/request.rs | 15 +++++++++------ plume-common/src/activity_pub/sign.rs | 9 ++++++--- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/plume-common/src/activity_pub/request.rs b/plume-common/src/activity_pub/request.rs index 29201180..35bc0059 100644 --- a/plume-common/src/activity_pub/request.rs +++ b/plume-common/src/activity_pub/request.rs @@ -9,6 +9,9 @@ use crate::activity_pub::{ap_accept_header, AP_CONTENT_TYPE}; const PLUME_USER_AGENT: &str = concat!("Plume/", env!("CARGO_PKG_VERSION")); +#[derive(Debug)] +pub struct Error(); + pub struct Digest(String); impl Digest { @@ -61,16 +64,16 @@ impl Digest { base64::decode(&self.0[pos..]).expect("Digest::value: invalid encoding error") } - pub fn from_header(dig: &str) -> Result { + pub fn from_header(dig: &str) -> Result { if let Some(pos) = dig.find('=') { let pos = pos + 1; if base64::decode(&dig[pos..]).is_ok() { Ok(Digest(dig.to_owned())) } else { - Err(()) + Err(Error()) } } else { - Err(()) + Err(Error()) } } @@ -109,7 +112,7 @@ pub fn headers() -> HeaderMap { headers } -pub fn signature(signer: &S, headers: &HeaderMap) -> Result { +pub fn signature(signer: &S, headers: &HeaderMap) -> Result { let signed_string = headers .iter() .map(|(h, v)| { @@ -129,7 +132,7 @@ pub fn signature(signer: &S, headers: &HeaderMap) -> Result(signer: &S, headers: &HeaderMap) -> Result (Vec, Vec) { ) } +#[derive(Debug)] +pub struct Error(); + pub trait Signer { type Error; @@ -29,7 +32,7 @@ pub trait Signer { } pub trait Signable { - fn sign(&mut self, creator: &T) -> Result<&mut Self, ()> + fn sign(&mut self, creator: &T) -> Result<&mut Self, Error> where T: Signer; fn verify(self, creator: &T) -> bool @@ -43,7 +46,7 @@ pub trait Signable { } impl Signable for serde_json::Value { - fn sign(&mut self, creator: &T) -> Result<&mut serde_json::Value, ()> { + fn sign(&mut self, creator: &T) -> Result<&mut serde_json::Value, Error> { let creation_date = Utc::now().to_rfc3339(); let mut options = json!({ "type": "RsaSignature2017", @@ -61,7 +64,7 @@ impl Signable for serde_json::Value { let document_hash = Self::hash(&self.to_string()); let to_be_signed = options_hash + &document_hash; - let signature = base64::encode(&creator.sign(&to_be_signed).map_err(|_| ())?); + let signature = base64::encode(&creator.sign(&to_be_signed).map_err(|_| Error())?); options["signatureValue"] = serde_json::Value::String(signature); self["signature"] = options;