Macro throwing::define_error
source · define_error!() { /* proc-macro */ }Expand description
Creates a new composite error type with a given name.
This macro works almost exactly like #[throws],
except that it isn’t attached to a function.
Since there is no way to infer a name for the type,
it must be specified explicitly with type SomeError = ....
Unlike #[throws], this macro also accepts a visibility declaration,
since it cannot be copied from the function.
Examples
// Defines an error with a single variant
// ParseIntListError will implement From<ParseIntError>.
define_error!(type ParseIntListError = ParseIntError);
// Defines an error with a multiple variants
define_error!(type DownloadFileError = HttpError | io::Error);
// Defines an error with a suberror
// DownloadDirError will implement From<DownloadFileError>.
define_error!(type DownloadDirError = HttpError | io::Error | ZipError | break DownloadFileError);
// Defines a public error
define_error!(pub type PublicError = io::Error);
// Defines a error crate-wide error
define_error!(pub(crate) type CrateWideError = io::Error);pub struct Id(u64);
define_error!(pub type ParseIdError = ParseIntError);
impl FromStr for Id {
type Err = ParseIdError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let value = s.parse()?;
Ok(value)
}
}