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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::path::PathBuf;
use std::error::Error;
use std::fmt;
use super::Environment;
use self::ConfigError::*;
use term_painter::Color::White;
use term_painter::ToStyle;
#[derive(Debug, PartialEq, Clone)]
pub struct ParsingError {
pub byte_range: (usize, usize),
pub start: (usize, usize),
pub end: (usize, usize),
pub desc: String,
}
#[derive(Debug, PartialEq, Clone)]
pub enum ConfigError {
BadCWD,
NotFound,
IOError,
BadFilePath(PathBuf, &'static str),
BadEnv(String),
BadEntry(String, PathBuf),
BadType(String, &'static str, &'static str, PathBuf),
ParseError(String, PathBuf, Vec<ParsingError>),
BadEnvVal(String, String, &'static str),
}
impl ConfigError {
pub fn pretty_print(&self) {
let valid_envs = Environment::valid();
match *self {
BadCWD => error!("couldn't get current working directory"),
NotFound => error!("config file was not found"),
IOError => error!("failed reading the config file: IO error"),
BadFilePath(ref path, reason) => {
error!("configuration file path '{:?}' is invalid", path);
info_!("{}", reason);
}
BadEntry(ref name, ref filename) => {
let valid_entries = format!("{}, and global", valid_envs);
error!("[{}] is not a known configuration environment", name);
info_!("in {:?}", White.paint(filename));
info_!("valid environments are: {}", White.paint(valid_entries));
}
BadEnv(ref name) => {
error!("'{}' is not a valid ROCKET_ENV value", name);
info_!("valid environments are: {}", White.paint(valid_envs));
}
BadType(ref name, expected, actual, ref filename) => {
error!("'{}' key could not be parsed", name);
info_!("in {:?}", White.paint(filename));
info_!("expected value to be {}, but found {}",
White.paint(expected), White.paint(actual));
}
ParseError(ref source, ref filename, ref errors) => {
for error in errors {
let (lo, hi) = error.byte_range;
let (line, col) = error.start;
let error_source = &source[lo..hi];
error!("config file could not be parsed as TOML");
info_!("at {:?}:{}:{}", White.paint(filename), line + 1, col + 1);
trace_!("'{}' - {}", error_source, White.paint(&error.desc));
}
}
BadEnvVal(ref key, ref value, ref expected) => {
error!("environment variable '{}={}' could not be parsed",
White.paint(key), White.paint(value));
info_!("value for {:?} must be {}",
White.paint(key), White.paint(expected))
}
}
}
#[inline(always)]
pub fn is_not_found(&self) -> bool {
match *self {
NotFound => true,
_ => false
}
}
}
impl fmt::Display for ConfigError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
BadCWD => write!(f, "couldn't get current working directory"),
NotFound => write!(f, "config file was not found"),
IOError => write!(f, "I/O error while reading the config file"),
BadFilePath(ref p, _) => write!(f, "{:?} is not a valid config path", p),
BadEnv(ref e) => write!(f, "{:?} is not a valid `ROCKET_ENV` value", e),
ParseError(..) => write!(f, "the config file contains invalid TOML"),
BadEntry(ref e, _) => {
write!(f, "{:?} is not a valid `[environment]` entry", e)
}
BadType(ref n, e, a, _) => {
write!(f, "type mismatch for '{}'. expected {}, found {}", n, e, a)
}
BadEnvVal(ref k, ref v, _) => {
write!(f, "environment variable '{}={}' could not be parsed", k, v)
}
}
}
}
impl Error for ConfigError {
fn description(&self) -> &str {
match *self {
BadCWD => "the current working directory could not be determined",
NotFound => "config file was not found",
IOError => "there was an I/O error while reading the config file",
BadFilePath(..) => "the config file path is invalid",
BadEntry(..) => "an environment specified as `[environment]` is invalid",
BadEnv(..) => "the environment specified in `ROCKET_ENV` is invalid",
ParseError(..) => "the config file contains invalid TOML",
BadType(..) => "a key was specified with a value of the wrong type",
BadEnvVal(..) => "an environment variable could not be parsed",
}
}
}