cranelift_reader/
error.rs1#![macro_use]
4
5use std::fmt;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9pub struct Location {
10 pub line_number: usize,
13}
14
15#[derive(Debug)]
17pub struct ParseError {
18 pub location: Location,
20 pub message: String,
22 pub is_warning: bool,
24}
25
26impl fmt::Display for ParseError {
27 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28 if self.location.line_number == 0 {
29 write!(f, "command-line arguments: {}", self.message)
30 } else {
31 write!(f, "{}: {}", self.location.line_number, self.message)
32 }
33 }
34}
35
36impl std::error::Error for ParseError {}
37
38pub type ParseResult<T> = Result<T, ParseError>;
40
41macro_rules! err {
43 ( $loc:expr, $msg:expr ) => {
44 Err($crate::ParseError {
45 location: $loc.clone(),
46 message: $msg.to_string(),
47 is_warning: false,
48 })
49 };
50
51 ( $loc:expr, $fmt:expr, $( $arg:expr ),+ ) => {
52 Err($crate::ParseError {
53 location: $loc.clone(),
54 message: format!( $fmt, $( $arg ),+ ),
55 is_warning: false,
56 })
57 };
58}
59
60macro_rules! warn {
61 ( $loc:expr, $fmt:expr, $( $arg:expr ),+ ) => {
62 Err($crate::ParseError {
63 location: $loc.clone(),
64 message: format!($fmt, $( $arg ),+ ),
65 is_warning: true,
66 })
67 };
68}