use std::fmt;
pub const METADATA_MARKER: &[u8] = b"\xAB\xCD\xEFMaxMind.com";
#[derive(Debug, Clone)]
pub enum MmdbError {
InvalidFormat(String),
MetadataNotFound,
InvalidMetadata(String),
DecodeError(String),
IoError(String),
InvalidIpAddress(String),
LookupError(String),
}
impl fmt::Display for MmdbError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidFormat(msg) => write!(f, "Invalid MMDB format: {msg}"),
Self::MetadataNotFound => write!(f, "MMDB metadata marker not found"),
Self::InvalidMetadata(msg) => write!(f, "Invalid metadata: {msg}"),
Self::DecodeError(msg) => write!(f, "Data decode error: {msg}"),
Self::IoError(msg) => write!(f, "IO error: {msg}"),
Self::InvalidIpAddress(msg) => write!(f, "Invalid IP address: {msg}"),
Self::LookupError(msg) => write!(f, "Lookup error: {msg}"),
}
}
}
impl std::error::Error for MmdbError {}
impl From<String> for MmdbError {
fn from(msg: String) -> Self {
Self::DecodeError(msg)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IpVersion {
V4,
V6,
}
pub use matchy_ip_trie::RecordSize;
pub fn record_size_from_bits(bits: u16) -> Result<RecordSize, MmdbError> {
match bits {
24 => Ok(RecordSize::Bits24),
28 => Ok(RecordSize::Bits28),
32 => Ok(RecordSize::Bits32),
_ => Err(MmdbError::InvalidFormat(format!(
"Invalid record size: {bits} bits"
))),
}
}