-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0333694
commit 06ef51e
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//! Runtime errors for the [`rustpostal`] crate. | ||
use std::error; | ||
use std::ffi::NulError; | ||
use std::fmt; | ||
|
||
/// An error indicating failure in setting up required `libpostal` resources. | ||
/// Returned by [`setup`](`LibModules.setup`) method on [`LibModules`]. | ||
#[derive(Debug, Clone)] | ||
pub struct SetupError; | ||
|
||
impl fmt::Display for SetupError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "setup of libpostal resources failed") | ||
} | ||
} | ||
|
||
impl error::Error for SetupError {} | ||
|
||
/// Error indicating possible runtime failures. | ||
#[derive(Debug, Clone)] | ||
pub enum RuntimeError { | ||
FailedSetup(SetupError), | ||
InvalidAddress(NulError), | ||
} | ||
|
||
impl fmt::Display for RuntimeError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match *self { | ||
RuntimeError::FailedSetup(ref err) => err.fmt(f), | ||
RuntimeError::InvalidAddress(_) => { | ||
write!(f, "input address possibly contains internal null byte") | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl error::Error for RuntimeError { | ||
fn source(&self) -> Option<&(dyn error::Error + 'static)> { | ||
match *self { | ||
RuntimeError::FailedSetup(ref err) => Some(err), | ||
RuntimeError::InvalidAddress(ref err) => Some(err), | ||
} | ||
} | ||
} | ||
|
||
impl From<SetupError> for RuntimeError { | ||
/// Create a new [`RuntimeError`] consuming a [`SetupError`]. | ||
fn from(err: SetupError) -> Self { | ||
RuntimeError::FailedSetup(err) | ||
} | ||
} | ||
|
||
impl From<NulError> for RuntimeError { | ||
/// Create a new [`RuntimeError`] consuming a [`NulError`]. | ||
fn from(err: NulError) -> Self { | ||
RuntimeError::InvalidAddress(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters