shape_ast/error/
conversions.rs1use super::types::{ShapeError, SourceLocation};
7use std::io;
8
9impl From<pest::error::Error<crate::parser::Rule>> for ShapeError {
11 fn from(err: pest::error::Error<crate::parser::Rule>) -> Self {
12 let location = match err.line_col {
13 pest::error::LineColLocation::Pos((line, col)) => Some(SourceLocation::new(line, col)),
14 pest::error::LineColLocation::Span((line, col), _) => {
15 Some(SourceLocation::new(line, col))
16 }
17 };
18
19 ShapeError::ParseError {
20 message: format!("{}", err),
21 location,
22 }
23 }
24}
25
26impl From<anyhow::Error> for ShapeError {
28 fn from(err: anyhow::Error) -> Self {
29 if let Some(shape_err) = err.downcast_ref::<ShapeError>() {
31 return shape_err.clone();
32 }
33
34 if let Some(io_err) = err.downcast_ref::<io::Error>() {
35 return ShapeError::IoError(io::Error::new(io_err.kind(), io_err.to_string()));
36 }
37
38 ShapeError::Custom(err.to_string())
40 }
41}
42
43impl From<serde_json::Error> for ShapeError {
45 fn from(err: serde_json::Error) -> Self {
46 ShapeError::Custom(format!("JSON error: {}", err))
47 }
48}