1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
use std::{
error::Error,
fmt::{self, Display},
};
use proc_macro2::Span;
use crate::chomp::Name;
use super::{Call, Parameter};
#[derive(Debug)]
pub enum SubstituteError {
FreeParameter {
param: Parameter,
span: Option<Span>,
name: Option<Name>,
},
WrongArgCount {
call: Call,
expected: usize,
span: Option<Span>,
},
}
impl From<SubstituteError> for syn::Error {
fn from(e: SubstituteError) -> Self {
let msg = e.to_string();
let span = match e {
SubstituteError::FreeParameter { span, .. }
| SubstituteError::WrongArgCount { span, .. } => span,
};
Self::new(span.unwrap_or_else(Span::call_site), msg)
}
}
impl Display for SubstituteError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::FreeParameter { param, name, .. } => {
if let Some(name) = name {
write!(f, "unbound parameter: `{}`", name)
} else {
write!(f, "unbound parameter: '{}", param.index)
}
}
Self::WrongArgCount { call, expected, .. } => {
if call.args.len() == 1 {
write!(
f,
"this function takes {} arguments but 1 was supplied",
expected
)
} else {
write!(
f,
"this function takes {} arguments but {} were supplied",
expected,
call.args.len()
)
}
}
}
}
}
impl Error for SubstituteError {}
|