higher_graphen_core/
error.rs1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4pub type Result<T> = std::result::Result<T, CoreError>;
6
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(tag = "code", rename_all = "snake_case")]
10pub enum CoreError {
11 InvalidId {
13 value: String,
15 reason: String,
17 },
18 InvalidConfidence {
20 value: String,
22 reason: String,
24 },
25 InvalidSourceKind {
27 value: String,
29 reason: String,
31 },
32 MalformedField {
34 field: String,
36 reason: String,
38 },
39 ParseFailure {
41 target: String,
43 value: String,
45 reason: String,
47 },
48 UnsupportedVersion {
50 version: String,
52 supported: String,
54 },
55}
56
57impl CoreError {
58 pub fn code(&self) -> &'static str {
60 match self {
61 Self::InvalidId { .. } => "invalid_id",
62 Self::InvalidConfidence { .. } => "invalid_confidence",
63 Self::InvalidSourceKind { .. } => "invalid_source_kind",
64 Self::MalformedField { .. } => "malformed_field",
65 Self::ParseFailure { .. } => "parse_failure",
66 Self::UnsupportedVersion { .. } => "unsupported_version",
67 }
68 }
69
70 pub(crate) fn invalid_id(value: impl Into<String>, reason: impl Into<String>) -> Self {
71 Self::InvalidId {
72 value: value.into(),
73 reason: reason.into(),
74 }
75 }
76
77 pub(crate) fn invalid_confidence(value: f64, reason: impl Into<String>) -> Self {
78 Self::InvalidConfidence {
79 value: value.to_string(),
80 reason: reason.into(),
81 }
82 }
83
84 pub(crate) fn invalid_source_kind(value: impl Into<String>, reason: impl Into<String>) -> Self {
85 Self::InvalidSourceKind {
86 value: value.into(),
87 reason: reason.into(),
88 }
89 }
90
91 pub(crate) fn malformed_field(field: impl Into<String>, reason: impl Into<String>) -> Self {
92 Self::MalformedField {
93 field: field.into(),
94 reason: reason.into(),
95 }
96 }
97
98 pub(crate) fn parse_failure(
99 target: impl Into<String>,
100 value: impl Into<String>,
101 reason: impl Into<String>,
102 ) -> Self {
103 Self::ParseFailure {
104 target: target.into(),
105 value: value.into(),
106 reason: reason.into(),
107 }
108 }
109}
110
111impl fmt::Display for CoreError {
112 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
113 match self {
114 Self::InvalidId { value, reason } => {
115 write!(formatter, "{}: invalid id {value:?}: {reason}", self.code())
116 }
117 Self::InvalidConfidence { value, reason } => write!(
118 formatter,
119 "{}: invalid confidence {value:?}: {reason}",
120 self.code()
121 ),
122 Self::InvalidSourceKind { value, reason } => write!(
123 formatter,
124 "{}: invalid source kind {value:?}: {reason}",
125 self.code()
126 ),
127 Self::MalformedField { field, reason } => {
128 write!(
129 formatter,
130 "{}: malformed field {field:?}: {reason}",
131 self.code()
132 )
133 }
134 Self::ParseFailure {
135 target,
136 value,
137 reason,
138 } => write!(
139 formatter,
140 "{}: could not parse {value:?} as {target}: {reason}",
141 self.code()
142 ),
143 Self::UnsupportedVersion { version, supported } => write!(
144 formatter,
145 "{}: unsupported version {version:?}; supported {supported}",
146 self.code()
147 ),
148 }
149 }
150}
151
152impl std::error::Error for CoreError {}