forked from SpaceManiac/SpacemanDMM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.rs
More file actions
231 lines (201 loc) · 6.33 KB
/
config.rs
File metadata and controls
231 lines (201 loc) · 6.33 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//! Configuration file for diagnostics.
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::collections::HashMap;
use serde::Deserialize;
use crate::error::Severity;
use crate::DMError;
/// Struct for deserializing from a config TOML
#[derive(Deserialize, Default, Debug, Clone)]
#[serde(default)]
pub struct Config {
pub environment: Option<PathBuf>,
// diagnostic configuration
display: WarningDisplay,
diagnostics: HashMap<String, WarningLevel>,
pub code_standards: CodeStandards,
// tool-specific configuration
pub langserver: Langserver,
pub dmdoc: DMDoc,
pub debugger: Debugger,
pub map_renderer: MapRenderer,
}
/// General error display options
#[derive(Deserialize, Default, Debug, Clone)]
pub struct WarningDisplay {
#[serde(default)]
error_level: WarningLevel,
}
/// Langserver config options
#[derive(Deserialize, Default, Debug, Clone)]
pub struct Langserver {
pub dreamchecker: bool,
}
/// Extremely opinionated linter config options
#[derive(Deserialize, Default, Debug, Clone)]
#[serde(default)]
pub struct CodeStandards {
pub disallow_relative_proc_definitions: bool,
pub disallow_relative_type_definitions: bool,
}
/// DMDoc config options
#[derive(Deserialize, Default, Debug, Clone)]
#[serde(default)]
pub struct DMDoc {
pub use_typepath_names: bool,
pub index_file: Option<String>,
pub module_directories: Vec<String>,
}
// Debugger config options
#[derive(Deserialize, Default, Debug, Clone)]
pub struct Debugger {
#[serde(default)]
pub engine: DebugEngine,
}
/// Severity overrides from configuration
#[derive(Debug, Deserialize, Clone, Copy, PartialEq)]
#[serde(rename_all(deserialize = "lowercase"))]
pub enum WarningLevel {
#[serde(alias = "errors")]
Error = 1,
#[serde(alias = "warnings")]
Warning = 2,
#[serde(alias = "infos")]
Info = 3,
#[serde(alias = "hints")]
Hint = 4,
#[serde(alias = "false", alias = "off")]
Disabled = 5,
Unset = 6,
}
/// Available debug engines.
#[derive(Debug, Deserialize, Clone, Copy, PartialEq)]
pub enum DebugEngine {
#[serde(alias = "extools")]
Extools,
#[serde(alias = "auxtools")]
Auxtools,
}
/// Config for the map renderer.
#[derive(Debug, Default, Deserialize, Clone)]
#[serde(default)]
pub struct MapRenderer {
/// Map from render pass name to whether it should be enabled/disabled.
///
/// Priority is: CLI arguments > config > defaults.
pub render_passes: HashMap<String, bool>,
/// Map from typepath to layer number.
pub fancy_layers: HashMap<String, f32>,
/// List of typepath to just hide
pub hide_invisible: Vec<String>,
}
impl Config {
/// Read a config TOML and generate a [`Config`] struct
///
/// [`Config`]: struct.Config.html
pub fn read_toml(path: &Path) -> Result<Config, Error> {
let mut file = File::open(path)?;
let mut config_toml = String::new();
file.read_to_string(&mut config_toml)?;
Ok(toml::from_str(&config_toml)?)
}
fn config_warninglevel(&self, error: &DMError) -> Option<&WarningLevel> {
if let Some(errortype) = error.errortype() {
return self.diagnostics.get(errortype)
}
None
}
/// Return a new [`DMError`] with the configured [`Severity`] or [`None`] if disabled
///
/// [`DMError`]: ../struct.DMError.html
/// [`Severity`]: ../enum.Severity.html
/// [`None`]: ../../std/option/enum.Option.html#variant.None
pub fn set_configured_severity(&self, error: DMError) -> Option<DMError> {
Some(match self.config_warninglevel(&error) {
Some(WarningLevel::Error) => error.set_severity(Severity::Error),
Some(WarningLevel::Warning) => error.set_severity(Severity::Warning),
Some(WarningLevel::Info) => error.set_severity(Severity::Info),
Some(WarningLevel::Hint) => error.set_severity(Severity::Hint),
Some(WarningLevel::Disabled) => return None,
Some(WarningLevel::Unset) | None => error,
})
}
/// Test the error against the configured error level threshold
pub fn registerable_error(&self, error: &DMError) -> bool {
self.display.error_level.applies_to(error.severity())
}
}
impl WarningLevel {
fn applies_to(self, severity: Severity) -> bool {
match self {
WarningLevel::Disabled => false,
WarningLevel::Error => severity <= Severity::Error,
WarningLevel::Warning => severity <= Severity::Warning,
WarningLevel::Info => severity <= Severity::Info,
WarningLevel::Hint => severity <= Severity::Hint,
WarningLevel::Unset => true,
}
}
}
impl Default for WarningLevel {
fn default() -> WarningLevel {
WarningLevel::Unset
}
}
impl From<Severity> for WarningLevel {
fn from(severity: Severity) -> Self {
match severity {
Severity::Error => WarningLevel::Error,
Severity::Warning => WarningLevel::Warning,
Severity::Info => WarningLevel::Info,
Severity::Hint => WarningLevel::Hint,
}
}
}
impl PartialEq<Severity> for WarningLevel {
fn eq(&self, other: &Severity) -> bool {
match (self, other) {
(WarningLevel::Error, Severity::Error) => true,
(WarningLevel::Warning, Severity::Warning) => true,
(WarningLevel::Info, Severity::Info) => true,
(WarningLevel::Hint, Severity::Hint) => true,
_ => false,
}
}
}
impl Default for DebugEngine {
fn default() -> Self {
Self::Extools
}
}
/// Config parse error
#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
Toml(toml::de::Error),
}
impl Error {
pub fn line_col(&self) -> Option<(u32, u16)> {
match self {
Error::Io(_) => None,
Error::Toml(toml) => toml.line_col().map(|(l, c)| (l as u32 + 1, c as u16 + 1)),
}
}
pub fn into_boxed_error(self) -> Box<dyn std::error::Error + Send + Sync> {
match self {
Error::Io(err) => Box::new(err),
Error::Toml(err) => Box::new(err),
}
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Error {
Error::Io(err)
}
}
impl From<toml::de::Error> for Error {
fn from(err: toml::de::Error) -> Error {
Error::Toml(err)
}
}