1 /* -*- Mode: rust; rust-indent-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 /// Helper macro to create an Error that knows which file and line it occurred
9 /// on. Can optionally have some extra information as a String.
11 macro_rules! error_here {
12 ($error_type:expr) => {
13 Error::new($error_type, file!(), line!(), None)
15 ($error_type:expr, $info:expr) => {
16 Error::new($error_type, file!(), line!(), Some($info))
20 /// Error type for identifying errors in this crate. Use the error_here! macro
31 pub fn new(typ: ErrorType, file: &'static str, line: u32, info: Option<String>) -> Error {
41 impl fmt::Display for Error {
42 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43 if let Some(info) = &self.info {
44 write!(f, "{} at {}:{} ({})", self.typ, self.file, self.line, info)
46 write!(f, "{} at {}:{}", self.typ, self.file, self.line)
51 impl Clone for Error {
52 fn clone(&self) -> Self {
57 info: self.info.as_ref().cloned(),
61 fn clone_from(&mut self, source: &Self) {
62 self.typ = source.typ;
63 self.file = source.file;
64 self.line = source.line;
65 self.info = source.info.as_ref().cloned();
69 #[derive(Copy, Clone, Debug)]
71 /// An error in an external library or resource.
73 /// Unexpected extra input (e.g. in an ASN.1 encoding).
77 /// Invalid data input.
79 /// An internal library failure (e.g. an expected invariant failed).
81 /// Truncated input (e.g. in an ASN.1 encoding).
83 /// Unsupported input.
85 /// A given value could not be represented in the type used for it.
89 impl fmt::Display for ErrorType {
90 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
91 let error_type_str = match self {
92 ErrorType::ExternalError => "ExternalError",
93 ErrorType::ExtraInput => "ExtraInput",
94 ErrorType::InvalidArgument => "InvalidArgument",
95 ErrorType::InvalidInput => "InvalidInput",
96 ErrorType::LibraryFailure => "LibraryFailure",
97 ErrorType::TruncatedInput => "TruncatedInput",
98 ErrorType::UnsupportedInput => "UnsupportedInput",
99 ErrorType::ValueTooLarge => "ValueTooLarge",
101 write!(f, "{}", error_type_str)