arff/
error.rs

1// Copyright 2018 Martin Billinger
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use std;
10use std::fmt::{self, Display};
11use std::string::FromUtf8Error;
12
13use serde::{de, ser};
14
15use parser::TextPos;
16
17pub type Result<T> = std::result::Result<T, Error>;
18
19#[derive(Clone, Debug, PartialEq)]
20pub enum Error {
21    Message(String),
22
23    // Serializer
24    UnexpectedType,
25    InconsistentType { row: usize, column: usize },
26
27    // Deserializer
28    Eof,
29    Expected(TextPos, &'static str),
30    ExpectedString(TextPos, String),
31    UnexpectedChar(TextPos, char, char),
32    ExpectedSequenceType,
33    ExpectedUnsignedValue(TextPos),
34    ExpectedIntegerValue(TextPos),
35    ExpectedFloatValue(TextPos),
36    NumericRange(TextPos, i64, i64),
37    NumericOverflow(TextPos),
38    Utf8Error(std::str::Utf8Error),
39
40    InvalidColumnType(TextPos, String),
41    WrongNominalValue(TextPos, String),
42    UnsupportedColumnType(TextPos, String),
43
44    ConversionError,
45    UnexpectedMissingValue,
46}
47
48impl ser::Error for Error {
49    fn custom<T: Display>(msg: T) -> Self {
50        Error::Message(msg.to_string())
51    }
52}
53
54impl de::Error for Error {
55    fn custom<T: Display>(msg: T) -> Self {
56        Error::Message(msg.to_string())
57    }
58}
59
60impl Display for Error {
61    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
62        formatter.write_str(std::error::Error::description(self))
63    }
64}
65
66impl std::error::Error for Error {
67    fn description(&self) -> &str {
68        match *self {
69            Error::Message(ref msg) => msg,
70            Error::UnexpectedType => "unexpected data type",
71            Error::InconsistentType { .. } => "inconsistent data type",
72            Error::Eof => "unexpected end of input",
73            Error::Expected(_, ref what) => what,
74            Error::ExpectedString(_, ref what) => what,
75            Error::UnexpectedChar(_, _, _) => "unexpected character",
76            Error::ExpectedUnsignedValue(_) => "expected unsigned integer value",
77            Error::ExpectedIntegerValue(_) => "expected integer value",
78            Error::NumericRange(_, _, _) => "value outside numeric range",
79            Error::NumericOverflow(_) => "value too large for u64",
80            Error::ExpectedSequenceType => "attempt to parse data set as a non-sequence type",
81            Error::ExpectedFloatValue(_) => "invalid floating point number",
82            Error::Utf8Error(_) => "invalid UTF-8 string",
83            Error::InvalidColumnType(_, _) => "column type not understood",
84            Error::UnsupportedColumnType(_, _) => "column type not supported",
85            Error::WrongNominalValue(_, _) => "wrong nominal value",
86            Error::ConversionError => "conversion error",
87            Error::UnexpectedMissingValue => "unexpected missing value",
88        }
89    }
90}
91
92impl From<FromUtf8Error> for Error {
93    fn from(e: FromUtf8Error) -> Error {
94        Error::Utf8Error(e.utf8_error())
95    }
96}