ssi_data_integrity_core/
decode.rs

1use serde::de::DeserializeOwned;
2use ssi_claims_core::ProofPreparationError;
3use ssi_json_ld::JsonLdNodeObject;
4
5use crate::{suite::DeserializeCryptographicSuiteOwned, DataIntegrity};
6
7#[derive(Debug, thiserror::Error)]
8pub enum DecodeError {
9    #[error("syntax error: {0}")]
10    Syntax(#[from] serde_json::Error),
11
12    #[error("proof preparation failed: {0}")]
13    ProofPreparation(#[from] ProofPreparationError),
14}
15
16/// Decodes a Data-Integrity credential or presentation from its JSON binary
17/// representation.
18pub fn from_json_slice<T, S>(json: &[u8]) -> Result<DataIntegrity<T, S>, DecodeError>
19where
20    T: DeserializeOwned + JsonLdNodeObject,
21    S: DeserializeCryptographicSuiteOwned,
22{
23    serde_json::from_slice::<DataIntegrity<T, S>>(json).map_err(Into::into)
24}
25
26/// Decodes a Data-Integrity credential or presentation from its JSON textual
27/// representation.
28pub fn from_json_str<T, S>(json: &str) -> Result<DataIntegrity<T, S>, DecodeError>
29where
30    T: DeserializeOwned + JsonLdNodeObject,
31    S: DeserializeCryptographicSuiteOwned,
32{
33    from_json_slice(json.as_bytes())
34}