Skip to main content

sas/
error.rs

1use std::fmt;
2
3/// Returned when a SAS document fails to parse.
4/// Always includes the line number of the offending token.
5#[derive(Debug, Clone, PartialEq)]
6pub struct ParseError {
7    pub line: usize,
8    pub message: String,
9}
10
11impl ParseError {
12    pub fn new(line: usize, message: impl Into<String>) -> Self {
13        Self { line, message: message.into() }
14    }
15}
16
17impl fmt::Display for ParseError {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        write!(f, "[Line {}] {}", self.line, self.message)
20    }
21}
22
23impl std::error::Error for ParseError {}
24
25/// Returned when a value cannot be serialized to SAS.
26#[derive(Debug, Clone, PartialEq)]
27pub struct ConvertError {
28    pub path: Option<String>,
29    pub message: String,
30}
31
32impl ConvertError {
33    pub fn new(message: impl Into<String>) -> Self {
34        Self { path: None, message: message.into() }
35    }
36    pub fn at(path: impl Into<String>, message: impl Into<String>) -> Self {
37        Self { path: Some(path.into()), message: message.into() }
38    }
39}
40
41impl fmt::Display for ConvertError {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match &self.path {
44            Some(p) => write!(f, "at \"{}\": {}", p, self.message),
45            None    => write!(f, "{}", self.message),
46        }
47    }
48}
49
50impl std::error::Error for ConvertError {}