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
//! Error mapping helpers.

use crate::*;
use errmap::{ErrorDescription, ErrorMapping};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// IDL error mapping.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct IDLErrorMapping {
    /// The set of error categories and their descriptions
    pub error_categories: BTreeMap<u64, IDLError>,
    /// The set of modules, and the module-specific errors
    pub module_error_maps: BTreeMap<ModuleIdData, BTreeMap<u64, IDLError>>,
}

impl From<ErrorMapping> for IDLErrorMapping {
    fn from(errmap: ErrorMapping) -> Self {
        IDLErrorMapping {
            error_categories: errmap
                .error_categories
                .into_iter()
                .map(|(k, v)| (k, IDLError::from(v)))
                .collect(),
            module_error_maps: errmap
                .module_error_maps
                .into_iter()
                .map(|(k, v)| {
                    (
                        k,
                        v.into_iter().map(|(k, v)| (k, IDLError::from(v))).collect(),
                    )
                })
                .collect(),
        }
    }
}

/// IDL error.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct IDLError {
    /// The constant name of error e.g., ECANT_PAY_DEPOSIT
    pub name: String,
    /// The code description. This is generated from the doc comments on the constant.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub doc: Option<String>,
}

impl From<ErrorDescription> for IDLError {
    fn from(desc: ErrorDescription) -> Self {
        IDLError {
            name: desc.code_name,
            doc: if desc.code_description.is_empty() {
                None
            } else {
                Some(desc.code_description)
            },
        }
    }
}