1use std::fmt;
3use std::fmt::{Display, Formatter};
4
5pub type Result<T> = std::result::Result<T, Error>;
6
7#[derive(Debug)]
9pub struct Error {
10 kind: ErrorType,
11}
12
13impl Error {
14 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#[derive(Clone, Debug)]
30#[non_exhaustive]
31pub enum ErrorType {
32 AnnotatedBuiltinType,
37 AnnotatedOpaqueType,
41 AnnotatedArray,
45 SizeOverflow,
47 PowerOfTwoAlignment,
49 SubByteAlignment,
51 SubByteSize,
53 MultiplePragmaPackedAnnotations,
55 NamedZeroSizeBitField,
59 UnnamedRegularField,
63 OversizedBitfield,
65 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}