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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! dfconfig is a lib for parsing and manipulating Dwarf Fortress' `init.txt` and `d_init.txt` config files (and possibly many others using the same format).
//! This lib's functionality has been specifically tailored to behave similar as DF internal parser, which implies:
//!
//! * [`Config::get`] returns the last occurrence value, if config specifies the key more than once.
//! * Whitespaces are not allowed at the start of lines, any line not starting with `[` character is treated as a comment.
//!
//! Other notable functionality is that the parser preserves all of the parsed string content, including blank lines and comments.
//!
//! # Examples
//!
//! ```no_run
//! # use std::io;
//! use std::fs::{read_to_string, write};
//! use dfconfig::Config;
//!
//! // Parse existing config
//! let path = r"/path/to/df/data/init/init.txt";
//! let mut conf = Config::read_str(read_to_string(path)?);
//!
//! // Read some value
//! let sound = conf.get("SOUND");
//!
//! // Modify and save the config
//! conf.set("VOLUME", "128");
//! write(path, conf.print())?;
//! # Ok::<(), io::Error>(())
//! ```

#[macro_use]
extern crate lazy_static;

use std::collections::HashMap;

use regex::Regex;

#[derive(Clone, Debug)]
enum Line {
    Blank,
    Comment(String),
    Entry(Entry),
}

#[derive(Clone, Debug)]
struct Entry {
    key: String,
    value: String,
}

impl Entry {
    pub fn new(key: String, value: String) -> Self {
        Self { key, value }
    }

    pub fn get_value(&self) -> &str {
        &self.value
    }

    pub fn get_key(&self) -> &str {
        &self.key
    }

    pub fn set_value(&mut self, value: String) {
        self.value = value;
    }
}

/// The main struct of this crate. Represents DF config file, while also providing functions to parse and manipulate the data.
/// See crate doc for example usage.
#[doc(inline)]
#[derive(Clone, Debug)]
pub struct Config {
    lines: Vec<Line>,
}

impl Config {
    /// Creates an empty config.
    pub fn new() -> Self {
        Self { lines: vec![] }
    }

    /// Parse the config from a string.
    pub fn read_str<T: AsRef<str>>(input: T) -> Self {
        lazy_static! {
            static ref RE: Regex = Regex::new(r"^\[([\w\d]+):([\w\d:]+)\]$").unwrap();
        }
        let mut lines = Vec::<Line>::new();
        for l in input.as_ref().lines() {
            let lt = l.trim_end();

            if lt.is_empty() {
                lines.push(Line::Blank);
                continue;
            }

            let captures = RE.captures(lt);
            match captures {
                Some(c) => lines.push(Line::Entry(Entry::new(
                    c.get(1).unwrap().as_str().to_owned(),
                    c.get(2).unwrap().as_str().to_owned(),
                ))),
                None => lines.push(Line::Comment(l.to_owned())),
            };
        }

        Self { lines }
    }

    /// Tries to retrieve the value for `key`.
    /// If the key is defined more than once, returns the value of the last occurrence.
    pub fn get<T: AsRef<str>>(&self, key: T) -> Option<&str> {
        self.lines.iter().rev().find_map(|x| match x {
            Line::Entry(entry) => {
                if entry.get_key() == key.as_ref() {
                    Some(entry.get_value())
                } else {
                    None
                }
            }
            _ => None,
        })
    }

    /// Sets all the occurrences of `key` to `value`
    ///
    /// # Panics
    ///
    /// Panics if `key` or `value` is either empty or non-alphanumeric.
    pub fn set<T: AsRef<str>, U: Into<String>>(&mut self, key: T, value: U) {
        let key = key.as_ref();
        let value = value.into();
        if key.is_empty()
            || !key.chars().all(|x| x.is_alphanumeric())
            || value.is_empty()
            || !value.chars().all(|x| x.is_alphanumeric())
        {
            panic!("Both key and value have to be non-empty alphanumeric strings!")
        }
        let mut n = 0;
        for e in self.lines.iter_mut() {
            if let Line::Entry(entry) = e {
                if entry.get_key() == key {
                    entry.set_value(value.clone());
                    n += 1;
                }
            }
        }

        if n == 0 {
            self.lines
                .push(Line::Entry(Entry::new(key.to_string(), value)));
        }
    }

    /// Removes all occurrences of `key` from this `Config`. Returns the number of keys removed.
    pub fn remove<T: AsRef<str>>(&mut self, key: T) -> usize {
        let mut n: usize = 0;
        loop {
            let to_remove = self.lines.iter().enumerate().find_map(|(i, x)| {
                if let Line::Entry(entry) = x {
                    if entry.get_key() == key.as_ref() {
                        return Some(i);
                    }
                }
                None
            });
            match to_remove {
                None => break,
                Some(i) => {
                    self.lines.remove(i);
                    n += 1;
                }
            };
        }
        n
    }

    /// Returns number of configuration entries present in this `Config`.
    pub fn len(&self) -> usize {
        self.lines
            .iter()
            .filter(|&x| matches!(x, Line::Entry(_)))
            .count()
    }

    /// Returns true if there are no entries defined in this `Config`.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns an iterator over the `key` strings.
    pub fn keys_iter(&self) -> impl Iterator<Item = &str> + '_ {
        self.lines.iter().filter_map(|x| {
            if let Line::Entry(entry) = x {
                Some(entry.get_key())
            } else {
                None
            }
        })
    }

    /// Returns an iterator over (`key`, `value`) tuples.
    pub fn keys_values_iter(&self) -> impl Iterator<Item = (&str, &str)> + '_ {
        self.lines.iter().filter_map(|x| {
            if let Line::Entry(entry) = x {
                Some((entry.get_key(), entry.get_value()))
            } else {
                None
            }
        })
    }

    /// Returns the string representing the configuration in its current state (aka what you'd write to the file usually).
    pub fn print(&self) -> String {
        let mut buff = Vec::<String>::with_capacity(self.lines.len());
        for l in self.lines.iter() {
            match l {
                Line::Blank => buff.push("".to_string()),
                Line::Comment(x) => buff.push(x.to_string()),
                Line::Entry(entry) => {
                    buff.push(format!("[{}:{}]", entry.get_key(), entry.get_value()))
                }
            }
        }

        buff.join("\r\n")
    }
}

impl From<Config> for HashMap<String, String> {
    fn from(conf: Config) -> Self {
        let mut output = HashMap::new();
        conf.keys_values_iter().for_each(|(key, value)| {
            output.insert(key.to_owned(), value.to_owned());
        });
        output
    }
}

impl Default for Config {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use rand::distributions::Alphanumeric;
    use rand::{thread_rng, Rng};
    use std::fs::read_to_string;
    use std::iter;

    use super::*;

    fn random_alphanumeric() -> String {
        let mut rng = thread_rng();
        iter::repeat(())
            .map(|()| rng.sample(Alphanumeric))
            .map(char::from)
            .take(thread_rng().gen_range(1..50))
            .collect()
    }

    #[test]
    fn test_basic_parse() {
        let key = random_alphanumeric();
        let key2 = random_alphanumeric();
        let value: String = random_alphanumeric();
        let c = Config::read_str(format!("[{}:{}]", key, value));
        assert_eq!(c.get(key).unwrap(), value);
        assert_eq!(c.get(key2), None);
    }

    #[test]
    fn test_multi_value() {
        let key = random_alphanumeric();
        let value: String = format!("{}:{}", random_alphanumeric(), random_alphanumeric());
        let c = Config::read_str(format!("[{}:{}]", key, value));
        assert_eq!(c.get(key).unwrap(), value);
    }

    #[test]
    fn test_basic_set() {
        let key = random_alphanumeric();
        let value: String = random_alphanumeric();
        let mut c = Config::new();
        c.set(&key, &value);
        assert_eq!(c.get(key).unwrap(), value);
    }

    #[test]
    fn test_read_modify() {
        let key_a = random_alphanumeric();
        let value_b: String = random_alphanumeric();
        let value_c: String = random_alphanumeric();
        let key_d = random_alphanumeric();
        let value_e: String = random_alphanumeric();
        let mut c = Config::read_str(format!("[{}:{}]", key_a, value_b));
        assert_eq!(c.get(&key_a).unwrap(), value_b);
        c.set(&key_a, &value_c);
        assert_eq!(c.get(&key_a).unwrap(), value_c);
        c.set(&key_d, &value_e);
        assert_eq!(c.get(&key_a).unwrap(), value_c);
        assert_eq!(c.get(&key_d).unwrap(), value_e);
    }

    #[test]
    fn test_read_file_smoke() {
        let s = read_to_string("test-data/test.init").unwrap();
        let c = Config::read_str(&s);
        s.lines()
            .zip(c.print().lines())
            .for_each(|(a, b)| assert_eq!(a, b));
    }

    #[test]
    fn test_len() {
        let a: String = random_alphanumeric();
        let b: String = random_alphanumeric();
        let c: String = random_alphanumeric();
        let d: String = random_alphanumeric();
        let e: String = random_alphanumeric();
        let f: String = random_alphanumeric();
        let g: String = random_alphanumeric();
        let mut conf = Config::read_str(format!("[{}:{}]\r\nfoo bar\r\n[{}:{}]", a, b, c, d));
        assert_eq!(conf.len(), 2);
        conf.set(&e, &f);
        assert_eq!(conf.len(), 3);
        conf.set(&e, &g);
        assert_eq!(conf.len(), 3);
    }

    #[test]
    #[should_panic]
    fn panic_on_empty_set() {
        let mut c = Config::new();
        c.set("", "");
    }

    #[test]
    #[should_panic]
    fn panic_on_non_alphanumeric_set() {
        let mut c = Config::new();
        c.set("\r", "\n");
    }

    #[test]
    fn test_keys_iter() {
        let a: String = random_alphanumeric();
        let b: String = random_alphanumeric();
        let mut conf = Config::new();
        conf.set(&a, "foo");
        conf.set(&b, "bar");
        let mut iter = conf.keys_iter();
        assert_eq!(Some(a.as_ref()), iter.next());
        assert_eq!(Some(b.as_ref()), iter.next());
        assert_eq!(None, iter.next());
    }

    #[test]
    fn test_remove() {
        let a: String = random_alphanumeric();
        let b: String = random_alphanumeric();
        let c: String = random_alphanumeric();
        let d: String = random_alphanumeric();
        let mut conf = Config::read_str(format!(
            "[{}:foo]\r\n[{}:bar]\r\n[{}:bar2]\r\n[{}:foobar]\r\n[{}:foobar2]",
            a, b, b, c, d
        ));
        assert_eq!(conf.len(), 5);
        assert_eq!(conf.remove(&b), 2);
        assert_eq!(conf.len(), 3);
        assert_eq!(conf.get(&b), None);
        assert_eq!(conf.remove(&a), 1);
        assert_eq!(conf.len(), 2);
        assert_eq!(conf.get(&a), None);
        assert_eq!(conf.remove(random_alphanumeric()), 0);
    }

    #[test]
    fn test_keys_values_iter() {
        let a: String = random_alphanumeric();
        let b: String = random_alphanumeric();
        let mut conf = Config::new();
        conf.set(&a, "foo");
        conf.set(&b, "bar");
        let mut iter = conf.keys_values_iter();
        assert_eq!(Some((a.as_ref(), "foo")), iter.next());
        assert_eq!(Some((b.as_ref(), "bar")), iter.next());
        assert_eq!(None, iter.next());
    }

    #[test]
    fn test_hashmap() {
        let a: String = random_alphanumeric();
        let b: String = random_alphanumeric();
        let mut conf = Config::new();
        conf.set(&a, "foo");
        conf.set(&b, "bar");
        let hash_map_owned: HashMap<String, String> = conf.into();
        assert_eq!(hash_map_owned.len(), 2);
        assert_eq!(hash_map_owned.get(&a).unwrap(), "foo");
        assert_eq!(hash_map_owned.get(&b).unwrap(), "bar");
    }
}