summary refs log tree commit diff stats
path: root/tests/common/errorpanic.rs
blob: 6b4254bf57b0a5e2f390dc5f18a2bf87cf136c31 (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
// Copyright (c) 2021 Soni L.
//
// Licensed under the MIT license.
// Documentation and comments licensed under CC BY-SA 4.0.

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

/// An implementation of various Iosonism errors that just panics.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ErrorPanic {
    // uninhabitable!
}

impl ::std::fmt::Display for ErrorPanic {
    fn fmt(&self, _: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        match *self {
        }
    }
}

impl ::std::error::Error for ErrorPanic {
}

impl<'a, C, T, R> RangeError<'a, C, T, R> for ErrorPanic
where C: StringReader<'a>, T: ::std::fmt::Display, R: ::std::fmt::Debug {
    fn value_not_in_range(context: &C, from: &T, range: &R) -> Self {
        if !context.get_remaining().is_empty() {
            panic!(
                "value ({}) not in range: {:?} at ...{}",
                from,
                range,
                context.get_remaining(),
            );
        } else {
            panic!("value ({}) not in range: {:?}", from, range);
        }
    }
}

impl<'a, C: StringReader<'a>> ReadError<'a, C> for ErrorPanic {
    fn invalid_integer(context: &C, from: &str) -> Self {
        if !context.get_remaining().is_empty() {
            panic!(
                "invalid integer: {} at ...{}",
                from,
                context.get_remaining(),
            );
        } else {
            panic!("invalid integer: {}", from);
        }
    }
    fn expected_integer(context: &C) -> Self {
        if !context.get_remaining().is_empty() {
            panic!("expected integer at ...{}", context.get_remaining());
        } else {
            panic!("expected integer");
        }
    }
    fn invalid_float(context: &C, from: &str) -> Self {
        if !context.get_remaining().is_empty() {
            panic!(
                "invalid float: {} at ...{}",
                from,
                context.get_remaining(),
            );
        } else {
            panic!("invalid float: {}", from);
        }
    }
    fn expected_float(context: &C) -> Self {
        if !context.get_remaining().is_empty() {
            panic!("expected float at ...{}", context.get_remaining());
        } else {
            panic!("expected float");
        }
    }
    fn invalid_bool(context: &C, from: &str) -> Self {
        if !context.get_remaining().is_empty() {
            panic!(
                "invalid bool: {} at ...{}",
                from,
                context.get_remaining(),
            );
        } else {
            panic!("invalid bool: {}", from);
        }
    }
    fn expected_bool(context: &C) -> Self {
        if !context.get_remaining().is_empty() {
            panic!("expected bool at ...{}", context.get_remaining());
        } else {
            panic!("expected bool");
        }
    }
    fn expected_start_of_quote(context: &C) -> Self {
        if !context.get_remaining().is_empty() {
            panic!("expected start of quote at ...{}", context.get_remaining());
        } else {
            panic!("expected start of quote");
        }
    }
    fn expected_end_of_quote(context: &C) -> Self {
        if !context.get_remaining().is_empty() {
            panic!("expected end of quote at ...{}", context.get_remaining());
        } else {
            panic!("expected end of quote");
        }
    }
    fn invalid_escape(context: &C, from: &str) -> Self {
        if !context.get_remaining().is_empty() {
            panic!(
                "invalid escape: {} at ...{}",
                from,
                context.get_remaining(),
            );
        } else {
            panic!("invalid escape: {}", from);
        }
    }
    fn expected_symbol(context: &C, from: &str) -> Self {
        if !context.get_remaining().is_empty() {
            panic!(
                "expected symbol: {} at ...{}",
                from,
                context.get_remaining(),
            );
        } else {
            panic!("expected symbol: {}", from);
        }
    }
}