rusty_handlebars_parser/
error.rs1use std::{error::Error, fmt::Display};
7use crate::expression::Expression;
8
9#[derive(Debug)]
14pub struct ParseError{
15 pub(crate) message: String
16}
17
18pub(crate) fn rcap<'a>(src: &'a str) -> &'a str{
20 static CAP_AT: usize = 32;
21
22 if src.len() > CAP_AT{
23 &src[src.len() - CAP_AT ..]
24 } else {
25 src
26 }
27}
28
29impl ParseError{
30 pub(crate) fn new(message: &str, expression: &Expression<'_>) -> Self{
32 Self{
33 message: format!("{} near \"{}\"", message, expression.around())
34 }
35 }
36
37 pub(crate) fn unclosed(preffix: &str) -> Self{
39 Self{
40 message: format!("unclosed block near {}", rcap(preffix))
41 }
42 }
43}
44
45impl Display for ParseError{
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47 f.write_str(&self.message)
48 }
49}
50
51impl From<std::io::Error> for ParseError{
52 fn from(err: std::io::Error) -> Self {
53 Self{ message: err.to_string()}
54 }
55}
56
57impl Error for ParseError{}
58
59pub type Result<T> = std::result::Result<T, ParseError>;