mmdb_writer/error.rs
1//! The crate's error type.
2
3use crate::record_size::RecordSize;
4
5/// Errors returned while building or writing a database.
6///
7/// This type implements [`std::error::Error`] and is `Send + Sync + 'static`, so it composes
8/// with [`Box<dyn Error>`](std::error::Error) and error libraries such as `anyhow`.
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum Error {
12 /// A value could not be represented in MMDB's type system.
13 ///
14 /// The most common cause is an `i64`/`isize` field (MMDB has no signed 64-bit type), a
15 /// non-string map key, or a top-level `Option::None`.
16 #[error("value cannot be represented in MMDB: {0}")]
17 UnsupportedValue(&'static str),
18
19 /// A `serde::Serialize` value failed to serialize.
20 #[cfg(feature = "serde")]
21 #[error("serialization failed: {0}")]
22 Serialize(String),
23
24 /// An IPv6 network was inserted into an IPv4-only (`ip_version = 4`) database.
25 #[error("cannot insert IPv6 network {0} into an IPv4 database")]
26 Ipv6InIpv4Tree(ipnet::Ipv6Net),
27
28 /// A range passed to [`Writer::insert_range`](crate::Writer::insert_range) was invalid —
29 /// the endpoints are different IP families, or the start is above the end.
30 #[error("invalid IP range: {0}")]
31 InvalidRange(&'static str),
32
33 /// An insert targeted a network reserved for IPv4 aliasing
34 /// (`::ffff:0:0/96`, `2001::/32`, or `2002::/16`).
35 #[error("cannot insert into aliased network {0}")]
36 AliasedNetwork(ipnet::IpNet),
37
38 /// An insert targeted a reserved network while reserved networks were excluded (the
39 /// default). Enable [`ReservedNetworks::Included`] to write into reserved space.
40 ///
41 /// [`ReservedNetworks::Included`]: crate::ReservedNetworks::Included
42 #[error("cannot insert into reserved network {0}")]
43 ReservedNetwork(ipnet::IpNet),
44
45 /// The tree (plus data section) grew past what the chosen [`RecordSize`] can address.
46 ///
47 /// When the record size is chosen automatically this cannot happen below the 32-bit
48 /// ceiling; it only surfaces when a smaller size was pinned explicitly, or for a
49 /// genuinely enormous database.
50 #[error("tree has {node_count} nodes but {record_size:?} can address at most {max}")]
51 TreeTooLarge {
52 /// Number of nodes in the (compacted) tree.
53 node_count: usize,
54 /// Maximum record value the chosen size can encode.
55 max: u64,
56 /// The record size that was too small.
57 record_size: RecordSize,
58 },
59
60 /// Writing to the destination [`std::io::Write`] failed.
61 #[error(transparent)]
62 Io(#[from] std::io::Error),
63
64 /// Reading an existing database with [`Writer::load`](crate::Writer::load) failed.
65 #[cfg(feature = "load")]
66 #[error("failed to load database: {0}")]
67 Load(String),
68}