xberg_libwpd/error.rs
1use thiserror::Error;
2
3/// Errors returned when extracting a WordPerfect document.
4#[derive(Error, Debug)]
5pub enum WpdError {
6 /// The input buffer was empty or a null pointer reached the shim.
7 #[error("invalid arguments passed to libwpd shim")]
8 InvalidArgs,
9
10 /// The buffer is not a WordPerfect document libwpd recognizes.
11 #[error("not a supported WordPerfect document")]
12 UnsupportedFormat,
13
14 /// libwpd recognized the format but failed to parse the document.
15 #[error("libwpd failed to parse the document")]
16 ParseError,
17
18 /// The shim could not allocate the output buffer.
19 #[error("out of memory while extracting text")]
20 OutOfMemory,
21
22 /// A C++ exception was caught at the FFI boundary.
23 #[error("libwpd raised an unexpected error")]
24 Internal,
25
26 /// The document is encrypted or password-protected; libwpd cannot parse
27 /// it without a matching password, which this crate never supplies. This
28 /// is distinct from `ParseError` so a caller can tell "needs a password"
29 /// from "the file is corrupt".
30 #[error("libwpd document is encrypted or password-protected")]
31 Encrypted,
32
33 /// The extracted text was not valid UTF-8.
34 #[error("libwpd returned invalid UTF-8")]
35 InvalidUtf8,
36
37 /// WordPerfect extraction is not available on this platform.
38 #[error("WordPerfect extraction is not supported on this platform")]
39 UnsupportedPlatform,
40}
41
42#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
43impl WpdError {
44 /// Map a shim result code (see `shim.cpp`) to an error. Code 0 is success
45 /// and has no error representation.
46 pub(crate) fn from_code(code: i32) -> Self {
47 match code {
48 1 => WpdError::InvalidArgs,
49 2 => WpdError::UnsupportedFormat,
50 3 => WpdError::ParseError,
51 4 => WpdError::OutOfMemory,
52 6 => WpdError::Encrypted,
53 _ => WpdError::Internal,
54 }
55 }
56}