summary refs log tree commit diff stats
path: root/tests/common/errorfunc.rs
blob: 5fb5fb608b0eca9c2ba3abc7623a6e5bc1f236ca (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2021 Soni L.
//
// Licensed under the MIT license.
// Documentation and comments licensed under CC BY-SA 4.0.

use ::std::marker::PhantomData;
use ::std::ops::Bound;
use ::std::ops::RangeBounds;

use ::iosonism::error::RangeError;
use ::iosonism::error::ReadError;
use ::iosonism::strcursor::StringReader;

/// An error callback.
pub trait ErrorFunc<'a, C: StringReader<'a>> {
    fn call<'b>(context: &C, ty: ErrorType<'b>);
}

/// An implementation of various Iosonism errors that calls T.
pub struct ErrorCall<T>(PhantomData<T>);

#[non_exhaustive]
#[derive(PartialEq, Debug)]
pub enum ErrorType<'a> {
    InvalidInteger(&'a str),
    ExpectedInteger,
    InvalidFloat(&'a str),
    ExpectedFloat,
    InvalidBool(&'a str),
    ExpectedBool,
    ExpectedStartOfQuote,
    ExpectedEndOfQuote,
    InvalidEscape(&'a str),
    ExpectedSymbol(&'a str),
    RangeErrori32(RangeErrorT<i32>),
    RangeErrori64(RangeErrorT<i64>),
    RangeErrorf32(RangeErrorT<f32>),
    RangeErrorf64(RangeErrorT<f64>),
}

#[derive(PartialEq, Debug)]
pub struct RangeErrorT<T> {
    pub value: T,
    pub start: Bound<T>,
    pub end: Bound<T>,
}

impl<T> ::std::fmt::Display for ErrorCall<T> {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "error!")
    }
}

impl<T> ::std::fmt::Debug for ErrorCall<T> {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "ErrorCall")
    }
}

impl<T> ::std::error::Error for ErrorCall<T> {
}

impl<'a, C, R, T> RangeError<'a, C, i32, R> for ErrorCall<T>
where C: StringReader<'a>, R: RangeBounds<i32>, T: ErrorFunc<'a, C> {
    fn value_not_in_range(context: &C, from: &i32, range: &R) -> Self {
        T::call(context, ErrorType::RangeErrori32(RangeErrorT {
            value: *from,
            start: range.start_bound().cloned(),
            end: range.end_bound().cloned(),
        }));
        Self(PhantomData)
    }
}

impl<'a, C, R, T> RangeError<'a, C, i64, R> for ErrorCall<T>
where C: StringReader<'a>, R: RangeBounds<i64>, T: ErrorFunc<'a, C> {
    fn value_not_in_range(context: &C, from: &i64, range: &R) -> Self {
        T::call(context, ErrorType::RangeErrori64(RangeErrorT {
            value: *from,
            start: range.start_bound().cloned(),
            end: range.end_bound().cloned(),
        }));
        Self(PhantomData)
    }
}

impl<'a, C, R, T> RangeError<'a, C, f32, R> for ErrorCall<T>
where C: StringReader<'a>, R: RangeBounds<f32>, T: ErrorFunc<'a, C> {
    fn value_not_in_range(context: &C, from: &f32, range: &R) -> Self {
        T::call(context, ErrorType::RangeErrorf32(RangeErrorT {
            value: *from,
            start: range.start_bound().cloned(),
            end: range.end_bound().cloned(),
        }));
        Self(PhantomData)
    }
}

impl<'a, C, R, T> RangeError<'a, C, f64, R> for ErrorCall<T>
where C: StringReader<'a>, R: RangeBounds<f64>, T: ErrorFunc<'a, C> {
    fn value_not_in_range(context: &C, from: &f64, range: &R) -> Self {
        T::call(context, ErrorType::RangeErrorf64(RangeErrorT {
            value: *from,
            start: range.start_bound().cloned(),
            end: range.end_bound().cloned(),
        }));
        Self(PhantomData)
    }
}

impl<'a, C: StringReader<'a>, T> ReadError<'a, C> for ErrorCall<T>
where T: ErrorFunc<'a, C> {
    fn invalid_integer(context: &C, from: &str) -> Self {
        T::call(context, ErrorType::InvalidInteger(from));
        Self(PhantomData)
    }
    fn expected_integer(context: &C) -> Self {
        T::call(context, ErrorType::ExpectedInteger);
        Self(PhantomData)
    }
    fn invalid_float(context: &C, from: &str) -> Self {
        T::call(context, ErrorType::InvalidFloat(from));
        Self(PhantomData)
    }
    fn expected_float(context: &C) -> Self {
        T::call(context, ErrorType::ExpectedFloat);
        Self(PhantomData)
    }
    fn invalid_bool(context: &C, from: &str) -> Self {
        T::call(context, ErrorType::InvalidBool(from));
        Self(PhantomData)
    }
    fn expected_bool(context: &C) -> Self {
        T::call(context, ErrorType::ExpectedBool);
        Self(PhantomData)
    }
    fn expected_start_of_quote(context: &C) -> Self {
        T::call(context, ErrorType::ExpectedStartOfQuote);
        Self(PhantomData)
    }
    fn expected_end_of_quote(context: &C) -> Self {
        T::call(context, ErrorType::ExpectedEndOfQuote);
        Self(PhantomData)
    }
    fn invalid_escape(context: &C, from: &str) -> Self {
        T::call(context, ErrorType::InvalidEscape(from));
        Self(PhantomData)
    }
    fn expected_symbol(context: &C, from: &str) -> Self {
        T::call(context, ErrorType::ExpectedSymbol(from));
        Self(PhantomData)
    }
}