otx_format/
error.rs

1use derive_more::Display;
2use molecule::error::VerificationError;
3use serde::{Deserialize, Serialize};
4
5use std::fmt::{Debug, Display};
6
7pub trait OtxError: Debug + Display {
8    fn err_code(&self) -> i64;
9    fn message(&self) -> String;
10}
11
12#[derive(Serialize, Deserialize, Clone, Debug, Display, Hash, PartialEq, Eq)]
13pub enum OtxFormatError {
14    #[display(fmt = "version {} is not supported", _0)]
15    VersionNotSupported(String),
16
17    #[display(fmt = "{} map has duplicate keypairs", _0)]
18    OtxMapHasDuplicateKeypair(String),
19
20    #[display(fmt = "map parse missing field {}", _0)]
21    OtxMapParseMissingField(String),
22
23    #[display(fmt = "map parse failed: {}", _0)]
24    OtxMapParseFailed(String),
25
26    #[display(fmt = "locate input cell failed: {}", _0)]
27    LocateInputFailed(String),
28}
29
30impl OtxError for OtxFormatError {
31    fn err_code(&self) -> i64 {
32        match self {
33            OtxFormatError::VersionNotSupported(_) => -13010,
34            OtxFormatError::OtxMapHasDuplicateKeypair(_) => -13011,
35            OtxFormatError::OtxMapParseMissingField(_) => -13012,
36            OtxFormatError::OtxMapParseFailed(_) => -13013,
37            OtxFormatError::LocateInputFailed(_) => -13014,
38        }
39    }
40
41    fn message(&self) -> String {
42        self.to_string()
43    }
44}
45
46impl OtxError for VerificationError {
47    fn err_code(&self) -> i64 {
48        match self {
49            VerificationError::TotalSizeNotMatch(_, _, _) => -13000,
50            VerificationError::HeaderIsBroken(_, _, _) => -13001,
51            VerificationError::UnknownItem(_, _, _) => -13002,
52            VerificationError::OffsetsNotMatch(_) => -13003,
53            VerificationError::FieldCountNotMatch(_, _, _) => 13004,
54        }
55    }
56
57    fn message(&self) -> String {
58        self.to_string()
59    }
60}