| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
use serde::Serialize; |
| 21 |
|
| 22 |
#[derive(Debug)] |
| 23 |
pub enum CanonicalError { |
| 24 |
Serde(serde_json::Error), |
| 25 |
} |
| 26 |
|
| 27 |
impl core::fmt::Display for CanonicalError { |
| 28 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 29 |
match self { |
| 30 |
Self::Serde(e) => write!(f, "canonical encoding failed: {e}"), |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
impl std::error::Error for CanonicalError { |
| 36 |
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 37 |
match self { |
| 38 |
Self::Serde(e) => Some(e), |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
impl From<serde_json::Error> for CanonicalError { |
| 44 |
fn from(e: serde_json::Error) -> Self { |
| 45 |
Self::Serde(e) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
pub fn canonical_json<T: Serialize>(payload: &T) -> Result<String, CanonicalError> { |
| 54 |
let value = serde_json::to_value(payload)?; |
| 55 |
Ok(serde_json::to_string(&value)?) |
| 56 |
} |
| 57 |
|