syslog_ng_common/proxies/parser/
option_error.rs

1// Copyright (c) 2016 Tibor Benke <ihrwein@gmail.com>
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9#[derive(Debug, PartialEq, Eq)]
10pub enum OptionError {
11    MissingRequiredOption(String),
12    InvalidValue {
13        option_name: String,
14        value: String,
15        expected_value: String,
16    },
17    VerbatimError(String)
18}
19
20impl OptionError {
21    pub fn missing_required_option<S: Into<String>>(option_name: S) -> OptionError {
22        OptionError::MissingRequiredOption(option_name.into())
23    }
24
25    pub fn invalid_value<S: Into<String>>(option_name: S,
26                                          value: S,
27                                          expected_value: S)
28                                          -> OptionError {
29        OptionError::InvalidValue {
30            option_name: option_name.into(),
31            value: value.into(),
32            expected_value: expected_value.into(),
33        }
34    }
35    pub fn verbatim_error(error_msg: String) -> OptionError {
36        OptionError::VerbatimError(error_msg)
37    }
38}
39
40use std::fmt::{Display, Error, Formatter};
41
42impl Display for OptionError {
43    fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
44        match *self {
45            OptionError::MissingRequiredOption(ref name) => {
46                formatter.write_fmt(format_args!("At least one required option is missing. \
47                                                  option_name={}",
48                                                 name))
49            }
50            OptionError::InvalidValue{ref option_name, ref value, ref expected_value} => {
51                formatter.write_fmt(format_args!("Invalid value in option. option_name={} \
52                                                  value={} expected_value={}",
53                                                 option_name,
54                                                 value,
55                                                 expected_value))
56            },
57            OptionError::VerbatimError(ref error_msg) => formatter.write_str(error_msg)
58        }
59    }
60}
61
62impl ::std::error::Error for OptionError {
63    fn description(&self) -> &str {
64        match *self {
65            OptionError::MissingRequiredOption(_) => "At least one required option is missing.",
66            OptionError::InvalidValue{..} => "Invalid value in option.",
67            OptionError::VerbatimError(..) => "Invalid value in option.",
68        }
69    }
70
71    fn cause(&self) -> Option<&::std::error::Error> {
72        match *self {
73            OptionError::MissingRequiredOption(_) => None,
74            OptionError::InvalidValue{..} => None,
75            OptionError::VerbatimError(..) => None,
76        }
77    }
78}