use std::fmt;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum AadError {
#[error("integer out of range: {value} exceeds maximum safe value {max}")]
IntegerOutOfRange {
value: u64,
max: u64,
},
#[error("negative integer not allowed: {value}")]
NegativeInteger {
value: i64,
},
#[error("field key cannot be empty")]
EmptyFieldKey,
#[error("invalid field key '{key}': {reason}")]
InvalidFieldKey {
key: String,
reason: String,
},
#[error("reserved key '{key}' cannot be used as extension field")]
ReservedKeyAsExtension {
key: String,
},
#[error("invalid extension key format '{key}': expected pattern {expected_pattern}")]
InvalidExtensionKeyFormat {
key: String,
expected_pattern: &'static str,
},
#[error("field '{field}' too short: minimum {min_bytes} bytes, got {actual_bytes}")]
FieldTooShort {
field: &'static str,
min_bytes: usize,
actual_bytes: usize,
},
#[error("field '{field}' too long: maximum {max_bytes} bytes, got {actual_bytes}")]
FieldTooLong {
field: &'static str,
max_bytes: usize,
actual_bytes: usize,
},
#[error("field '{field}' contains NUL byte (0x00)")]
NulByteInValue {
field: &'static str,
},
#[error("missing required field: {field}")]
MissingRequiredField {
field: &'static str,
},
#[error("duplicate key: '{key}'")]
DuplicateKey {
key: String,
},
#[error("unknown field '{field}' for schema version {version}")]
UnknownField {
field: String,
version: u64,
},
#[error("unsupported schema version: {version}")]
UnsupportedVersion {
version: u64,
},
#[error("field '{field}' has wrong type: expected {expected}, got {actual}")]
WrongFieldType {
field: &'static str,
expected: &'static str,
actual: JsonType,
},
#[error("serialized AAD too large: maximum {max_bytes} bytes, got {actual_bytes}")]
SerializedTooLarge {
max_bytes: usize,
actual_bytes: usize,
},
#[error("invalid JSON: {message}")]
InvalidJson {
message: String,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JsonType {
Null,
Bool,
Number,
String,
Array,
Object,
}
impl fmt::Display for JsonType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Null => write!(f, "null"),
Self::Bool => write!(f, "boolean"),
Self::Number => write!(f, "number"),
Self::String => write!(f, "string"),
Self::Array => write!(f, "array"),
Self::Object => write!(f, "object"),
}
}
}
impl From<&serde_json::Value> for JsonType {
fn from(value: &serde_json::Value) -> Self {
match value {
serde_json::Value::Null => Self::Null,
serde_json::Value::Bool(_) => Self::Bool,
serde_json::Value::Number(_) => Self::Number,
serde_json::Value::String(_) => Self::String,
serde_json::Value::Array(_) => Self::Array,
serde_json::Value::Object(_) => Self::Object,
}
}
}