-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.rs
246 lines (206 loc) · 7.2 KB
/
config.rs
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use std::path::Path;
use check::CheckName;
pub trait Valid {
fn validate(&self) -> Result<(), &'static str>;
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum FileType {
SAV,
DTA,
SAS7BDAT,
CSV,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Setting<T> {
pub setting: T,
pub desc: String,
}
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct Config {
pub metadata_only: Option<bool>,
pub progress: Option<bool>,
pub basic_file_checks: BasicFileChecks,
pub metadata: Metadata,
pub data_integrity: DataIntegrity,
pub disclosure_risk: DisclosureRisk,
}
impl Config {
pub fn get_desc(&self, check_name: &CheckName) -> &str {
if let Some(ref setting) = self.config_for_check(check_name) {
return &setting.desc;
}
return "";//.to_string();
}
pub fn get_dictionaries(&self, check_name: &CheckName) -> Vec<String> {
let mut result: Vec<String> = vec![];
if let Some(ref paths) = self.config_for_check(check_name) {
for spath in paths.setting.iter() {
let path = Path::new(spath);
if path.is_file() {
result.push(
path.to_str()
.expect("Failed to convert path to str.")
.to_string(),
);
}
}
}
return result;
}
pub fn config_for_check(&self, check_name: &CheckName) -> &Option<Setting<Vec<String>>> {
return match check_name {
CheckName::VariableLabelSpellcheck => &self.metadata.variable_label_spellcheck,
CheckName::ValueLabelSpellcheck => &self.metadata.value_label_spellcheck,
CheckName::StringValueSpellcheck => &self.data_integrity.string_value_spellcheck,
CheckName::StringValueStopword => &self.disclosure_risk.string_value_stopword,
_ => &None,
};
}
}
impl Valid for Config {
fn validate(&self) -> Result<(), &'static str> {
self.basic_file_checks.validate()?;
self.metadata.validate()?;
self.data_integrity.validate()?;
self.disclosure_risk.validate()?;
Ok(())
}
}
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct BasicFileChecks {
pub bad_filename: Option<Setting<String>>,
}
impl Valid for BasicFileChecks {
fn validate(&self) -> Result<(), &'static str> {
match self.bad_filename {
None => (),
Some(ref pattern) => {
if pattern.setting.len() < 1 {
return Err("bad_filename cannot be an empty string");
}
}
}
Ok(())
}
}
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct Metadata {
pub primary_variable: Option<Setting<String>>,
pub missing_variable_labels: Option<Setting<bool>>,
pub variable_odd_characters: Option<Setting<Vec<String>>>,
pub variable_label_max_length: Option<Setting<i32>>,
pub variable_label_spellcheck: Option<Setting<Vec<String>>>,
pub value_label_odd_characters: Option<Setting<Vec<String>>>,
pub value_label_max_length: Option<Setting<i32>>,
pub value_label_spellcheck: Option<Setting<Vec<String>>>,
pub value_defined_missing_no_label: Option<Setting<bool>>, // SPSS only. E.g. -9 is Defined missing but has no label
}
impl Valid for Metadata {
fn validate(&self) -> Result<(), &'static str> {
match self.primary_variable {
None => (),
Some(ref primary_variable) => {
if primary_variable.setting.len() < 1 {
return Err("metadata.primary_variable cannot be an empty string");
}
}
}
match self.variable_odd_characters {
None => (),
Some(ref odd_characters) => {
if odd_characters.setting.len() < 1 {
return Err("metadata.variable_odd_characters cannot be empty");
}
}
}
match self.variable_label_max_length {
None => (),
Some(ref label_max_length) => {
if label_max_length.setting < 0 {
return Err("metadata.variable_label_max_length cannot be negative");
}
}
}
match self.value_label_odd_characters {
None => (),
Some(ref odd_characters) => {
if odd_characters.setting.len() < 1 {
return Err("metadata.value_label_odd_characters cannot be empty");
}
}
}
match self.value_label_max_length {
None => (),
Some(ref label_max_length) => {
if label_max_length.setting < 0 {
return Err("metadata.value_label_max_length cannot be negative");
}
}
}
Ok(())
}
}
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct DataIntegrity {
pub duplicate_values: Option<Setting<Vec<String>>>,
pub string_value_odd_characters: Option<Setting<Vec<String>>>,
pub string_value_spellcheck: Option<Setting<Vec<String>>>,
pub system_missing_value_threshold: Option<Setting<i32>>,
}
impl Valid for DataIntegrity {
fn validate(&self) -> Result<(), &'static str> {
match self.duplicate_values {
None => (),
Some(ref variables) => {
if variables.setting.len() < 1 {
return Err("data_integrity.duplicate_values cannot be empty");
}
}
}
match self.string_value_odd_characters {
None => (),
Some(ref odd_characters) => {
if odd_characters.setting.len() < 1 {
return Err("data_integrity.string_value_odd_characters cannot be empty");
}
}
}
match self.system_missing_value_threshold {
None => (),
Some(ref threshold) => {
if !(threshold.setting > 0 && threshold.setting <= 100) {
return Err("data_integrity.system_missing_value_threshold out of bounds, must be between 1 and 100 inclusive");
}
}
}
Ok(())
}
}
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct DisclosureRisk {
pub date_format: Option<Setting<Vec<String>>>,
pub regex_patterns: Option<Setting<Vec<String>>>,
pub unique_values: Option<Setting<i32>>,
pub string_value_stopword: Option<Setting<Vec<String>>>,
}
impl Valid for DisclosureRisk {
fn validate(&self) -> Result<(), &'static str> {
match self.regex_patterns {
None => (),
Some(ref patterns) => {
if patterns.setting.len() < 1 {
return Err("data_integrity.regex_patterns cannot be empty");
}
}
}
match self.unique_values {
None => (),
Some(ref threshold) => {
if !(threshold.setting > 0 && threshold.setting <= 100) {
return Err("threshold out of bounds");
}
}
}
Ok(())
}
}