repc_impl/
result.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2use std::fmt;
3use std::fmt::{Display, Formatter};
4
5pub type Result<T> = std::result::Result<T, Error>;
6
7/// An error produced by this crate.
8#[derive(Debug)]
9pub struct Error {
10    kind: ErrorType,
11}
12
13impl Error {
14    /// Returns the type of the error.
15    pub fn kind(&self) -> ErrorType {
16        self.kind.clone()
17    }
18}
19
20impl Display for Error {
21    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
22        Display::fmt(&self.kind, f)
23    }
24}
25
26impl std::error::Error for Error {}
27
28/// The type of an error produced by this crate.
29#[derive(Clone, Debug)]
30#[non_exhaustive]
31pub enum ErrorType {
32    /// A builtin type was annotated.
33    ///
34    /// Builtin types cannot be annotated. You probably want to annotate a typedef of the
35    /// builtin type.
36    AnnotatedBuiltinType,
37    /// An opaque type was annotated.
38    ///
39    /// Opaque types cannot be annotated.
40    AnnotatedOpaqueType,
41    /// An array was annotated.
42    ///
43    /// Arrays cannot be annotated. You probably want to annotate a typedef of the array.
44    AnnotatedArray,
45    /// The size of the type cannot be represented in `u64`.
46    SizeOverflow,
47    /// One of the alignments given in the input is not a power of two.
48    PowerOfTwoAlignment,
49    /// One of the alignments given in the input is not at least 8.
50    SubByteAlignment,
51    /// The size of an opaque type is not a multiple of 8.
52    SubByteSize,
53    /// A type has multiple `PragmaPack` annotations.
54    MultiplePragmaPackedAnnotations,
55    /// A zero-sized bit-field is named.
56    ///
57    /// Zero-sized bitfields must be unnamed.
58    NamedZeroSizeBitField,
59    /// A regular field is unnamed.
60    ///
61    /// Only bit-fields can be unnamed.
62    UnnamedRegularField,
63    /// One of the bit-fields in the input has a width larger than the size of its type.
64    OversizedBitfield,
65    /// A field has a `PragmaPack` annotation.
66    ///
67    /// Fields cannot have `PragmaPack` annotations.
68    PragmaPackedField,
69}
70
71impl Display for ErrorType {
72    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
73        use ErrorType::*;
74        let s = match self {
75            AnnotatedBuiltinType => "Builtin types cannot have annotations",
76            AnnotatedOpaqueType => "Opaque types cannot have annotations",
77            AnnotatedArray => "Arrays cannot have annotations",
78            SizeOverflow => "The object size in bits overflows u64",
79            PowerOfTwoAlignment => "Alignments must be a power of two",
80            SubByteAlignment => "Alignments must be at least 8",
81            SubByteSize => "Sizes must be a multiple of 8",
82            PragmaPackedField => "Fields cannot have pragma_pack annotations",
83            MultiplePragmaPackedAnnotations => {
84                "A type/field can have at most one packed annotation"
85            }
86            NamedZeroSizeBitField => "A zero-sized bit-field cannot be named",
87            UnnamedRegularField => "Regular fields must be named",
88            OversizedBitfield => {
89                "The width of a bit-field cannot be larger than the width of the underlying type"
90            }
91        };
92        f.write_str(s)
93    }
94}
95
96pub(crate) fn err(kind: ErrorType) -> Error {
97    Error { kind }
98}