File size: 4,370 Bytes
f14b4e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::fmt;

/// Errors that can occur during parser initialization.
#[derive(Debug, Clone, PartialEq)]
pub enum LanguageError {
    IncompatibleLanguageVersion,
    LanguageNotInitialized,
}

impl fmt::Display for LanguageError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            LanguageError::IncompatibleLanguageVersion => write!(
                f,
                "Tree-sitter language version is incompatible with parser"
            ),
            LanguageError::LanguageNotInitialized => {
                write!(f, "Parser was not initialized with the language")
            }
        }
    }
}

/// Errors that can occur when parsing values from DSL source.
#[derive(Debug, Clone, PartialEq)]
pub enum ValueParseError {
    UnknownValueKind,
    MissingValue,
    MissingParseMethod,
    InvalidBoolean(String),
    InvalidInteger(String),
    InvalidFloat(String),
    InvalidCurrencyFormat {
        source: String,
        parts_count: usize,
    },
    InvalidCurrencyAmount(String),
    InvalidCurrencyCode(String),
    InvalidReferenceFormat {
        source: String,
        parts_count: usize,
    },
    InvalidDate(String),
    InvalidDateTime(String),
    InvalidTimezone(String),
    HeterogeneousList {
        expected_type: String,
        found_type: String,
        index: usize,
    },
}

impl fmt::Display for ValueParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ValueParseError::UnknownValueKind => {
                write!(
                    f,
                    "Value type could not be determined from tree-sitter node"
                )
            }
            ValueParseError::MissingValue => {
                write!(f, "Field is missing its value assignment")
            }
            ValueParseError::MissingParseMethod => {
                write!(f, "No parsing method implemented for this value type")
            }
            ValueParseError::InvalidBoolean(value) => {
                write!(f, "Boolean value could not be parsed: '{}'", value)
            }
            ValueParseError::InvalidInteger(value) => {
                write!(f, "Integer value could not be parsed: '{}'", value)
            }
            ValueParseError::InvalidFloat(value) => {
                write!(f, "Float value could not be parsed: '{}'", value)
            }
            ValueParseError::InvalidCurrencyFormat {
                source,
                parts_count,
            } => {
                write!(
                    f,
                    "Currency format is invalid (expected 'amount currency'): '{}' has {} part(s)",
                    source, parts_count
                )
            }
            ValueParseError::InvalidCurrencyAmount(amount) => {
                write!(
                    f,
                    "Currency amount could not be parsed as decimal: '{}'",
                    amount
                )
            }
            ValueParseError::InvalidCurrencyCode(code) => {
                write!(f, "Currency code is not recognized: '{}'", code)
            }
            ValueParseError::InvalidReferenceFormat {
                source,
                parts_count,
            } => {
                write!(
                    f,
                    "Reference format is invalid (expected 2 or 3 dot-separated parts): '{}' has {} part(s)",
                    source, parts_count
                )
            }
            ValueParseError::InvalidDate(date) => {
                write!(f, "Date value could not be parsed: '{}'", date)
            }
            ValueParseError::InvalidDateTime(datetime) => {
                write!(f, "DateTime value could not be parsed: '{}'", datetime)
            }
            ValueParseError::InvalidTimezone(timezone) => {
                write!(f, "Timezone offset could not be parsed: '{}'", timezone)
            }
            ValueParseError::HeterogeneousList {
                expected_type,
                found_type,
                index,
            } => {
                write!(
                    f,
                    "List contains values of different types (but must be homogeneous): expected {}, found {} at index {}",
                    expected_type, found_type, index
                )
            }
        }
    }
}