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
/*
 * File: parser/error.rs
 * Date: 02.10.2018
 * Author: MarkAtk
 * 
 * MIT License
 * 
 * Copyright (c) 2018 MarkAtk
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

use std::fmt;
use std::error::Error as StdError;

#[derive(Debug, PartialEq)]
pub enum Error {
    UnknownError(u32, u32),
    ReadFileError,
    IllegalToken(String, u32, u32),
    MissingClosingParenthesis(String, u32, u32),
    MissingDirectionSeparator(u32, u32),
    MissingGroupIdentifier(u32, u32),
    MissingTestIdentifier(u32, u32),
    MissingOptionIdentifier(u32, u32),
    MissingOptionSeparator(u32, u32),
    MissingOptionValue(u32, u32),
    MissingContent(String, u32, u32),
    InvalidLineStart(u32, u32),
    InvalidOptionValue(String, u32, u32),
    InvalidOutputContent(String, u32, u32),
    UnknownTestOption(String, u32, u32),
    UnknownGroupOption(String, u32, u32)
}

impl fmt::Display for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::UnknownError(line, column) => formatter.write_fmt(format_args!("Unknown error at {}:{}", line, column)),
            Error::ReadFileError => formatter.write_str("Unable to read file"),
            Error::IllegalToken(ref value, line, column) => formatter.write_fmt(format_args!("Illegal token '{}' at {}:{}", value, line, column)),
            Error::MissingClosingParenthesis(ref value, line, column) => formatter.write_fmt(format_args!("Missing closing parenthesis '{}' at {}:{}", value, line, column)),
            Error::MissingDirectionSeparator(line, column) => formatter.write_fmt(format_args!("Missing direction separator at {}:{}", line, column)),
            Error::MissingGroupIdentifier(line, column) => formatter.write_fmt(format_args!("Missing group identifier at {}:{}", line, column)),
            Error::MissingTestIdentifier(line, column) => formatter.write_fmt(format_args!("Missing test identifier at {}:{}", line, column)),
            Error::MissingOptionIdentifier(line, column) => formatter.write_fmt(format_args!("Missing option identifier at {}:{}", line, column)),
            Error::MissingOptionSeparator(line, column) => formatter.write_fmt(format_args!("Missing option separator '=' at {}:{}", line, column)),
            Error::MissingOptionValue(line, column) => formatter.write_fmt(format_args!("Missing option value at {}:{}", line, column)),
            Error::MissingContent(ref content_type, line, column) => formatter.write_fmt(format_args!("Missing test {} at {}:{}", content_type, line, column)),
            Error::InvalidLineStart(line, column) => formatter.write_fmt(format_args!("Invalid line start at {}:{}", line, column)),
            Error::InvalidOptionValue(ref expected_type, line, column) => formatter.write_fmt(format_args!("Invalid option type at {}:{}. {} type expected", line, column, expected_type)),
            Error::InvalidOutputContent(ref content, line, column) => formatter.write_fmt(format_args!("Invalid output content '{}' at {}:{}", content, line, column)),
            Error::UnknownTestOption(ref name, line, column) => formatter.write_fmt(format_args!("Unknown test option '{}' at {}:{}", name, line, column)),
            Error::UnknownGroupOption(ref name, line, column) => formatter.write_fmt(format_args!("Unknown group option '{}' at {}:{}", name, line, column))
        }
    }
}

impl StdError for Error {
    fn description(&self) -> &str {
        match *self {
            Error::UnknownError(_, _) => "Unknown error",
            Error::ReadFileError => "File file error",
            Error::IllegalToken(_, _, _) => "Illegal token",
            Error::MissingClosingParenthesis(_, _, _) => "Missing closing parenthesis",
            Error::MissingDirectionSeparator(_, _) => "Missing direction separator",
            Error::MissingGroupIdentifier(_, _) => "Missing group identifier",
            Error::MissingTestIdentifier(_, _) => "Missing test identifier",
            Error::MissingOptionIdentifier(_, _) => "Missing option identifier",
            Error::MissingOptionSeparator(_, _) => "Missing option separator",
            Error::MissingOptionValue(_, _) => "Missing option value",
            Error::MissingContent(_, _, _) => "Missing test content",
            Error::InvalidLineStart(_, _) => "Invalid line start",
            Error::InvalidOptionValue(_, _, _) => "Invalid option value",
            Error::InvalidOutputContent(_, _, _) => "Invalid output content",
            Error::UnknownTestOption(_, _, _) => "Unknown test option",
            Error::UnknownGroupOption(_, _, _) => "Unknown group option"
        }
    }
}