nt_string/
error.rs

1// Copyright 2023 Colin Finck <colin@reactos.org>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use displaydoc::Display;
5
6/// Central result type of nt-string.
7pub type Result<T, E = NtStringError> = core::result::Result<T, E>;
8
9/// Central error type of nt-string.
10#[derive(Clone, Debug, Display, Eq, PartialEq)]
11#[non_exhaustive]
12pub enum NtStringError {
13    /// The required buffer size exceeds an unsigned 16-bit integer (65535 bytes).
14    BufferSizeExceedsU16,
15    /// No NUL character could be found.
16    NulNotFound,
17    /// The buffer could not be decoded as UTF-16 due to the unpaired surrogate {unpaired_surrogate:#06X}.
18    UnpairedUtf16Surrogate {
19        /// Numeric value of the unpaired surrogate
20        unpaired_surrogate: u16,
21    },
22}
23
24impl From<widestring::error::DecodeUtf16Error> for NtStringError {
25    fn from(e: widestring::error::DecodeUtf16Error) -> Self {
26        Self::UnpairedUtf16Surrogate {
27            unpaired_surrogate: e.unpaired_surrogate(),
28        }
29    }
30}
31
32#[cfg(feature = "std")]
33#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
34impl std::error::Error for NtStringError {}