1use std::fmt;
4use thiserror::Error;
5
6#[derive(Debug, Error)]
8pub enum AadError {
9 #[error("field '{field}' integer out of range: {value} exceeds maximum safe value {max}")]
11 IntegerOutOfRange {
12 field: String,
14 value: u64,
16 max: u64,
18 },
19
20 #[error("field '{field}' negative integer not allowed: {value}")]
22 NegativeInteger {
23 field: String,
25 value: i64,
27 },
28
29 #[error("field key cannot be empty")]
31 EmptyFieldKey,
32
33 #[error("invalid field key '{key}': {reason}")]
35 InvalidFieldKey {
36 key: String,
38 reason: String,
40 },
41
42 #[error("reserved key '{key}' cannot be used as extension field")]
44 ReservedKeyAsExtension {
45 key: String,
47 },
48
49 #[error("invalid extension key format '{key}': expected pattern {expected_pattern}")]
51 InvalidExtensionKeyFormat {
52 key: String,
54 expected_pattern: &'static str,
56 },
57
58 #[error("field '{field}' too short: minimum {min_bytes} bytes, got {actual_bytes}")]
60 FieldTooShort {
61 field: &'static str,
63 min_bytes: usize,
65 actual_bytes: usize,
67 },
68
69 #[error("field '{field}' too long: maximum {max_bytes} bytes, got {actual_bytes}")]
71 FieldTooLong {
72 field: &'static str,
74 max_bytes: usize,
76 actual_bytes: usize,
78 },
79
80 #[error("field '{field}' contains NUL byte (0x00)")]
82 NulByteInValue {
83 field: &'static str,
85 },
86
87 #[error("missing required field: {field}")]
89 MissingRequiredField {
90 field: &'static str,
92 },
93
94 #[error("duplicate key: '{key}'")]
96 DuplicateKey {
97 key: String,
99 },
100
101 #[error("unknown field '{field}' for schema version {version}")]
103 UnknownField {
104 field: String,
106 version: u64,
108 },
109
110 #[error("unsupported schema version: {version}")]
112 UnsupportedVersion {
113 version: u64,
115 },
116
117 #[error("field '{field}' has wrong type: expected {expected}, got {actual}")]
119 WrongFieldType {
120 field: &'static str,
122 expected: &'static str,
124 actual: JsonType,
126 },
127
128 #[error("serialized AAD too large: maximum {max_bytes} bytes, got {actual_bytes}")]
130 SerializedTooLarge {
131 max_bytes: usize,
133 actual_bytes: usize,
135 },
136
137 #[error("invalid JSON: {message}")]
139 InvalidJson {
140 message: String,
142 },
143
144 #[error("field '{field}' is not a valid integer: {reason}")]
146 InvalidFloat {
147 field: String,
149 reason: &'static str,
151 },
152}
153
154#[derive(Debug, Clone, Copy, PartialEq, Eq)]
156pub enum JsonType {
157 Null,
159 Bool,
161 Number,
163 String,
165 Array,
167 Object,
169}
170
171impl fmt::Display for JsonType {
172 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
173 match self {
174 Self::Null => write!(f, "null"),
175 Self::Bool => write!(f, "boolean"),
176 Self::Number => write!(f, "number"),
177 Self::String => write!(f, "string"),
178 Self::Array => write!(f, "array"),
179 Self::Object => write!(f, "object"),
180 }
181 }
182}
183
184impl From<&serde_json::Value> for JsonType {
185 fn from(value: &serde_json::Value) -> Self {
186 match value {
187 serde_json::Value::Null => Self::Null,
188 serde_json::Value::Bool(_) => Self::Bool,
189 serde_json::Value::Number(_) => Self::Number,
190 serde_json::Value::String(_) => Self::String,
191 serde_json::Value::Array(_) => Self::Array,
192 serde_json::Value::Object(_) => Self::Object,
193 }
194 }
195}