zksync_error/
packed.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// AUTOGENERATED BASED ON A SET OF JSON FILES, DO NOT EDIT MANUALLY
//
use crate::{
    error::{ICustomError, IError, IUnifiedError},
    identifier::Identifier,
    serialized::SerializedError,
};
use std::fmt::Debug;
#[derive(Clone, Debug, Eq, PartialEq, serde :: Deserialize, serde :: Serialize)]
pub struct PackedError<U>
where
    U: Clone + Debug,
{
    pub identifier: Identifier,
    pub message: String,
    pub data: U,
}
impl<T> IError<T> for PackedError<T>
where
    T: Clone + Debug + serde::Serialize + for<'de> serde::Deserialize<'de>,
{
    fn get_identifier(&self) -> Identifier {
        self.identifier.clone()
    }
    fn get_message(&self) -> String {
        self.message.clone()
    }
    fn get_data(&self) -> T {
        self.data.clone()
    }
}
pub fn pack_unified<T, C>(s: T) -> Result<PackedError<T>, serde_json::Error>
where
    T: serde::Serialize + IUnifiedError<C> + Clone + Debug,
    C: Clone,
{
    Ok(PackedError {
        identifier: s.get_identifier(),
        message: s.get_message(),
        data: s,
    })
}
pub fn pack<T, U, C>(s: T) -> PackedError<U>
where
    T: serde::Serialize + ICustomError<U, C> + Clone,
    U: IUnifiedError<C> + Clone + Debug,
    C: Clone,
{
    pack_unified(s.to_unified()).expect("Serialization error")
}
pub fn serialized<T, C>(p: PackedError<T>) -> SerializedError
where
    T: Clone + Debug + serde::Serialize + IUnifiedError<C>,
    C: Clone,
{
    let data = serde_json::value::to_value(&p.data).expect("Serialization error");
    SerializedError {
        code: p.identifier.encode(),
        message: p.message,
        data,
    }
}
pub fn serialized_ref<T, C>(p: &PackedError<T>) -> SerializedError
where
    T: Clone + Debug + serde::Serialize + IUnifiedError<C>,
    C: Clone,
{
    let data = serde_json::value::to_value(&p.data).expect("Serialization error");
    SerializedError {
        code: p.identifier.encode(),
        message: p.message.clone(),
        data,
    }
}
impl<T> std::fmt::Display for PackedError<T>
where
    T: Clone + Debug + serde::Serialize,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let value = serde_json::value::to_value(&self.data).expect("Serialization error");
        let value_pretty = serde_json::to_string(&value).expect("Serialization error");
        let code = self.identifier.encode();
        let message = &self.message;
        let data = value_pretty;
        f.write_fmt(format_args!(
            r#"{{ code: {code} ; message: "{message}"; data: {data} }}"#,
        ))
    }
}
impl<T> std::error::Error for PackedError<T> where T: serde::Serialize + Debug + Clone {}