rusqlite_gpkg/
error.rs

1use std::error::Error;
2use std::fmt;
3
4/// Crate error type for GeoPackage operations.
5#[derive(Debug)]
6pub enum GpkgError {
7    Sql(rusqlite::Error),
8    Wkb(wkb::error::WkbError),
9    UnsupportedGeometryType(String),
10    InvalidDimension { z: i8, m: i8 },
11    InvalidPropertyCount { expected: usize, got: usize },
12    InvalidGpkgGeometryFlags(u8),
13    ReadOnly,
14    Message(String),
15}
16
17impl fmt::Display for GpkgError {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        match self {
20            Self::Sql(err) => write!(f, "{err}"),
21            Self::Wkb(err) => write!(f, "{err}"),
22            Self::UnsupportedGeometryType(ty) => write!(f, "unsupported geometry type: {ty}"),
23            Self::InvalidDimension { z, m } => {
24                write!(f, "invalid or mixed geometry dimension (z={z}, m={m})")
25            }
26            Self::InvalidPropertyCount { expected, got } => {
27                write!(f, "invalid property count: expected {expected}, got {got}")
28            }
29            Self::InvalidGpkgGeometryFlags(flags) => {
30                write!(f, "invalid gpkg geometry flags: {flags:#04x}")
31            }
32            Self::ReadOnly => write!(f, "operation not allowed on read-only connection"),
33            Self::Message(message) => write!(f, "{message}"),
34        }
35    }
36}
37
38impl Error for GpkgError {
39    fn source(&self) -> Option<&(dyn Error + 'static)> {
40        match self {
41            Self::Sql(err) => Some(err),
42            Self::Wkb(err) => Some(err),
43            _ => None,
44        }
45    }
46}
47
48impl From<rusqlite::Error> for GpkgError {
49    fn from(err: rusqlite::Error) -> Self {
50        Self::Sql(err)
51    }
52}
53
54impl From<wkb::error::WkbError> for GpkgError {
55    fn from(err: wkb::error::WkbError) -> Self {
56        Self::Wkb(err)
57    }
58}
59
60impl From<String> for GpkgError {
61    fn from(message: String) -> Self {
62        Self::Message(message)
63    }
64}
65
66impl From<&str> for GpkgError {
67    fn from(message: &str) -> Self {
68        Self::Message(message.to_string())
69    }
70}
71
72pub type Result<T> = std::result::Result<T, GpkgError>;
73
74#[cfg(feature = "arrow")]
75impl From<GpkgError> for arrow_schema::ArrowError {
76    fn from(value: GpkgError) -> Self {
77        arrow_schema::ArrowError::ExternalError(value.into())
78    }
79}
80
81#[cfg(feature = "arrow")]
82impl From<arrow_schema::ArrowError> for GpkgError {
83    fn from(value: arrow_schema::ArrowError) -> Self {
84        // TODO
85        GpkgError::Message(format!("{value:?}"))
86    }
87}