1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum Error {
5 #[error("{message:?} (Line {line:?})")]
6 Semantics { line: u32, message: String },
7
8 #[error("error converting integer")]
9 IntegerConversion(#[from] std::num::TryFromIntError),
10
11 #[error("syntax error")]
12 Syntax(#[from] peginator::ParseError),
13
14 #[error("failed to add btf type")]
15 BtfTypeConversion(#[from] btf::Error),
16
17 #[error("type conversion not implemented")]
18 NoConversion,
19
20 #[error("no type with that id")]
21 InvalidTypeId,
22
23 #[error("no type with that name")]
24 InvalidTypeName,
25
26 #[error("internal error occurred that shouldn't be possible")]
27 InternalError,
28}
29
30pub type Result<T> = std::result::Result<T, Error>;
31
32pub trait SemanticsErrorContext {
33 type InnerType;
34
35 fn context(&self, line: u32, message: &str) -> Result<Self::InnerType>;
36}
37
38impl<T: Copy> SemanticsErrorContext for std::option::Option<T> {
39 type InnerType = T;
40
41 fn context(&self, line: u32, message: &str) -> Result<T> {
42 self.ok_or(Error::Semantics {
43 line,
44 message: message.to_string(),
45 })
46 }
47}
48
49impl<T: Copy, E: Copy> SemanticsErrorContext for std::result::Result<T, E> {
50 type InnerType = T;
51
52 fn context(&self, line: u32, message: &str) -> Result<T> {
53 self.or(Err(Error::Semantics {
54 line,
55 message: message.to_string(),
56 }))
57 }
58}