onenote_parser/
errors.rs

1//! OneNote parsing error handling.
2
3#[cfg(feature = "backtrace")]
4use std::backtrace::Backtrace;
5use std::borrow::Cow;
6use std::{io, string};
7use thiserror::Error;
8
9/// The result of parsing a OneNote file.
10pub type Result<T> = std::result::Result<T, Error>;
11
12/// A parsing error.
13///
14/// If the crate is compiled with the `backtrace` feature enabled, the
15/// parsing error struct will contain a backtrace of the location where
16/// the error occured. The backtrace can be accessed using
17/// [`std::error::Error::backtrace()`].
18#[derive(Error, Debug)]
19#[error("{kind}")]
20pub struct Error {
21    kind: ErrorKind,
22
23    #[cfg(feature = "backtrace")]
24    backtrace: Backtrace,
25}
26
27impl From<ErrorKind> for Error {
28    #[cfg(feature = "backtrace")]
29    fn from(kind: ErrorKind) -> Self {
30        Error {
31            kind,
32            backtrace: Backtrace::capture(),
33        }
34    }
35
36    #[cfg(not(feature = "backtrace"))]
37    fn from(kind: ErrorKind) -> Self {
38        Error { kind }
39    }
40}
41
42impl From<std::io::Error> for Error {
43    fn from(err: std::io::Error) -> Self {
44        ErrorKind::from(err).into()
45    }
46}
47
48impl From<std::string::FromUtf16Error> for Error {
49    fn from(err: std::string::FromUtf16Error) -> Self {
50        ErrorKind::from(err).into()
51    }
52}
53
54impl From<widestring::MissingNulError<u16>> for Error {
55    fn from(err: widestring::MissingNulError<u16>) -> Self {
56        ErrorKind::from(err).into()
57    }
58}
59
60impl From<uuid::Error> for Error {
61    fn from(err: uuid::Error) -> Self {
62        ErrorKind::from(err).into()
63    }
64}
65
66/// Details about a parsing error
67#[allow(missing_docs)]
68#[derive(Error, Debug)]
69pub enum ErrorKind {
70    /// Hit the end of the OneNote file before it was expected.
71    #[error("Unexpected end of file")]
72    UnexpectedEof,
73
74    /// The parser was asked to process a table-of-contents file that turned out not to be one.
75    #[error("Not a table of contents file: {file}")]
76    NotATocFile { file: String },
77
78    /// The parser was asked to process a section file that turned out not to be one.
79    #[error("Not a section file: {file}")]
80    NotASectionFile { file: String },
81
82    /// When parsing a section group the table-of-contents file for this group was found to be missing.
83    #[error("Table of contents file is missing in dir {dir}")]
84    TocFileMissing { dir: String },
85
86    /// Malformed data was encountered when parsing the OneNote file.
87    #[error("Malformed data: {0}")]
88    MalformedData(Cow<'static, str>),
89
90    /// Malformed data was encountered when parsing the OneNote data.
91    #[error("Malformed OneNote data: {0}")]
92    MalformedOneNoteData(Cow<'static, str>),
93
94    /// Malformed data was encountered when parsing the OneNote file contents.
95    #[error("Malformed OneNote file data: {0}")]
96    MalformedOneNoteFileData(Cow<'static, str>),
97
98    /// Malformed data was encountered when parsing the OneStore data.
99    #[error("Malformed OneStore data: {0}")]
100    MalformedOneStoreData(Cow<'static, str>),
101
102    /// Malformed data was encountered when parsing the FSSHTTPB data.
103    #[error("Malformed FSSHTTPB data: {0}")]
104    MalformedFssHttpBData(Cow<'static, str>),
105
106    /// A malformed UUID was encountered
107    #[error("Invalid UUID: {err}")]
108    InvalidUuid {
109        #[from]
110        err: uuid::Error,
111    },
112
113    /// An I/O failure was encountered during parsing.
114    #[error("I/O failure: {err}")]
115    IO {
116        #[from]
117        err: io::Error,
118    },
119
120    /// A malformed UTF-16 string was encountered during parsing.
121    #[error("Malformed UTF-16 string: {err}")]
122    Utf16Error {
123        #[from]
124        err: string::FromUtf16Error,
125    },
126
127    /// A UTF-16 string without a null terminator was encountered during parsing.
128    #[error("UTF-16 string is missing null terminator: {err}")]
129    Utf16MissingNull {
130        #[from]
131        err: widestring::MissingNulError<u16>,
132    },
133}