Skip to main content

implicit_predicates_gen/
error.rs

1//! Errors reported while reading a predicate definition.
2
3use std::fmt;
4
5use proc_macro2::Span;
6
7use crate::parse::Position;
8
9/// An error found in a definition file, with the position of the offending
10/// token.
11#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct Error {
13    /// Name of the file, as given by the caller.
14    pub file: String,
15    /// Line of the offending token, starting at 1.
16    pub line: usize,
17    /// Column of the offending token, starting at 1.
18    pub column: usize,
19    /// What is wrong.
20    pub message: String,
21}
22
23impl Error {
24    /// The error `message` at `position` of `file`.
25    pub(crate) fn at(file: &str, position: Position, message: impl Into<String>) -> Self {
26        Error {
27            file: file.to_string(),
28            line: position.line,
29            column: position.column,
30            message: message.into(),
31        }
32    }
33}
34
35impl fmt::Display for Error {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        write!(
38            f,
39            "{}:{}:{}: {}",
40            self.file, self.line, self.column, self.message
41        )
42    }
43}
44
45impl std::error::Error for Error {}
46
47/// A problem at a position of the source, before the file name is known.
48pub(crate) struct Diag {
49    pub(crate) line: usize,
50    pub(crate) column: usize,
51    pub(crate) message: String,
52}
53
54pub(crate) type Res<T> = Result<T, Diag>;
55
56/// Reports `message` at the first token of `span`.
57pub(crate) fn diag(span: Span, message: impl Into<String>) -> Diag {
58    let start = span.start();
59    Diag {
60        line: start.line,
61        column: start.column + 1,
62        message: message.into(),
63    }
64}