daemonbase 0.1.5

A library for providing the foundation for daemon processes.
Documentation
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
use crate::error::Failed;
use log::error;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::cell::RefCell;
use std::collections::HashMap;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::{env, fmt, fs, ops};
use toml_edit as toml;

//------------ ConfigFile ----------------------------------------------------

/// The content of a config file.
///
/// This is a thin wrapper around `toml::Table` to make dealing with it more
/// convenient.
#[derive(Clone, Debug)]
pub struct ConfigFile {
    /// The content of the file.
    content: toml::DocumentMut,

    /// The path to the config file.
    path: PathBuf,

    /// The directory we found the file in.
    ///
    /// This is used in relative paths.
    dir: PathBuf,
}

impl ConfigFile {
    /// Reads the config file at the given path.
    ///
    /// If there is no such file, returns `None`. If there is a file but it
    /// is broken, aborts.
    #[allow(clippy::verbose_file_reads)]
    pub fn read(path: &Path) -> Result<Option<Self>, Failed> {
        let mut file = match fs::File::open(path) {
            Ok(file) => file,
            Err(_) => return Ok(None),
        };
        let mut config = String::new();
        if let Err(err) = file.read_to_string(&mut config) {
            error!("Failed to read config file {}: {}", path.display(), err);
            return Err(Failed);
        }
        Self::parse(&config, path).map(Some)
    }

    /// Parses the content of the file from a string.
    pub fn parse(content: &str, path: &Path) -> Result<Self, Failed> {
        let content = match toml::DocumentMut::from_str(content) {
            Ok(content) => content,
            Err(err) => {
                eprintln!(
                    "Failed to parse config file {}: {}",
                    path.display(),
                    err
                );
                return Err(Failed);
            }
        };
        let dir =
            if path.is_relative() {
                path.join(match env::current_dir() {
                Ok(dir) => dir,
                Err(err) => {
                    error!(
                        "Fatal: Can't determine current directory: {err}.",
                    );
                    return Err(Failed);
                }
            }).parent().unwrap().into() // a file always has a parent
            } else {
                path.parent().unwrap().into()
            };
        Ok(ConfigFile {
            content,
            path: path.into(),
            dir,
        })
    }

    /// Returns a reference to the path of the config file.
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Takes a value from the from the config file if present.
    pub fn take_value(
        &mut self,
        key: &str,
    ) -> Result<Option<toml::Value>, Failed> {
        match self.content.remove(key) {
            Some(toml::Item::Value(value)) => Ok(Some(value)),
            Some(_) => {
                error!(
                    "Failed in config file {}: \
                     '{}' expected to be a value.",
                    self.path.display(),
                    key
                );
                Err(Failed)
            }
            None => Ok(None),
        }
    }

    /// Takes a boolean value from the config file.
    ///
    /// The value is taken from the given `key`. Returns `Ok(None)` if there
    /// is no such key. Returns an error if the key exists but the value
    /// isn’t a booelan.
    pub fn take_bool(&mut self, key: &str) -> Result<Option<bool>, Failed> {
        match self.take_value(key)? {
            Some(toml::Value::Boolean(res)) => Ok(Some(res.into_value())),
            Some(_) => {
                error!(
                    "Failed in config file {}: \
                     '{}' expected to be a boolean.",
                    self.path.display(),
                    key
                );
                Err(Failed)
            }
            None => Ok(None),
        }
    }

    /// Takes an unsigned integer value from the config file.
    ///
    /// The value is taken from the given `key`. Returns `Ok(None)` if there
    /// is no such key. Returns an error if the key exists but the value
    /// isn’t an integer or if it is negative.
    pub fn take_u64(&mut self, key: &str) -> Result<Option<u64>, Failed> {
        match self.take_value(key)? {
            Some(toml::Value::Integer(value)) => {
                match u64::try_from(value.into_value()) {
                    Ok(value) => Ok(Some(value)),
                    Err(_) => {
                        error!(
                            "Failed in config file {}: \
                            '{}' expected to be a positive integer.",
                            self.path.display(),
                            key
                        );
                        Err(Failed)
                    }
                }
            }
            Some(_) => {
                error!(
                    "Failed in config file {}: \
                     '{}' expected to be an integer.",
                    self.path.display(),
                    key
                );
                Err(Failed)
            }
            None => Ok(None),
        }
    }

    /// Takes a limited unsigned 8-bit integer value from the config file.
    ///
    /// The value is taken from the given `key`. Returns `Ok(None)` if there
    /// is no such key. Returns an error if the key exists but the value
    /// isn’t an integer, is larger than `limit` or is negative.
    pub fn take_limited_u8(
        &mut self,
        key: &str,
        limit: u8,
    ) -> Result<Option<u8>, Failed> {
        match self.take_u64(key)? {
            Some(value) => match u8::try_from(value) {
                Ok(value) => {
                    if value > limit {
                        error!(
                            "Failed in config file {}: \
                                '{}' expected integer between 0 and {}.",
                            self.path.display(),
                            key,
                            limit,
                        );
                        Err(Failed)
                    } else {
                        Ok(Some(value))
                    }
                }
                Err(_) => {
                    error!(
                        "Failed in config file {}: \
                            '{}' expected integer between 0 and {}.",
                        self.path.display(),
                        key,
                        limit,
                    );
                    Err(Failed)
                }
            },
            None => Ok(None),
        }
    }

    /// Takes an unsigned integer value from the config file.
    ///
    /// The value is taken from the given `key`. Returns `Ok(None)` if there
    /// is no such key. Returns an error if the key exists but the value
    /// isn’t an integer or if it is negative.
    pub fn take_usize(&mut self, key: &str) -> Result<Option<usize>, Failed> {
        match self.take_u64(key)? {
            Some(value) => match usize::try_from(value) {
                Ok(value) => Ok(Some(value)),
                Err(_) => {
                    error!(
                        "Failed in config file {}: \
                            '{}' expected to be a positive integer.",
                        self.path.display(),
                        key
                    );
                    Err(Failed)
                }
            },
            None => Ok(None),
        }
    }

    /// Takes a small unsigned integer value from the config file.
    ///
    /// While the result is returned as an `usize`, it must be in the
    /// range of a `u16`.
    ///
    /// The value is taken from the given `key`. Returns `Ok(None)` if there
    /// is no such key. Returns an error if the key exists but the value
    /// isn’t an integer or if it is out of bounds.
    pub fn take_small_usize(
        &mut self,
        key: &str,
    ) -> Result<Option<usize>, Failed> {
        match self.take_usize(key)? {
            Some(value) => {
                if value > u16::MAX.into() {
                    error!(
                        "Failed in config file {}: \
                        value for '{}' is too large.",
                        self.path.display(),
                        key
                    );
                    Err(Failed)
                } else {
                    Ok(Some(value))
                }
            }
            None => Ok(None),
        }
    }

    /// Takes a string value from the config file.
    ///
    /// The value is taken from the given `key`. Returns `Ok(None)` if there
    /// is no such key. Returns an error if the key exists but the value
    /// isn’t a string.
    pub fn take_string(
        &mut self,
        key: &str,
    ) -> Result<Option<String>, Failed> {
        match self.take_value(key)? {
            Some(toml::Value::String(value)) => Ok(Some(value.into_value())),
            Some(_) => {
                error!(
                    "Failed in config file {}: \
                     '{}' expected to be a string.",
                    self.path.display(),
                    key
                );
                Err(Failed)
            }
            None => Ok(None),
        }
    }

    /// Takes a string encoded value from the config file.
    ///
    /// The value is taken from the given `key`. It is expected to be a
    /// string and will be converted to the final type via `FromStr::from_str`.
    ///
    /// Returns `Ok(None)` if the key doesn’t exist. Returns an error if the
    /// key exists but the value isn’t a string or conversion fails.
    pub fn take_from_str<T>(&mut self, key: &str) -> Result<Option<T>, Failed>
    where
        T: FromStr,
        T::Err: fmt::Display,
    {
        match self.take_string(key)? {
            Some(value) => match T::from_str(&value) {
                Ok(some) => Ok(Some(some)),
                Err(err) => {
                    error!(
                        "Failed in config file {}: \
                             illegal value in '{}': {}.",
                        self.path.display(),
                        key,
                        err
                    );
                    Err(Failed)
                }
            },
            None => Ok(None),
        }
    }

    /// Takes a path value from the config file.
    ///
    /// The path is taken from the given `key`. It must be a string value.
    /// It is treated as relative to the directory of the config file. If it
    /// is indeed a relative path, it is expanded accordingly and an absolute
    /// path is returned.
    ///
    /// Returns `Ok(None)` if the key does not exist. Returns an error if the
    /// key exists but the value isn’t a string.
    pub fn take_path(
        &mut self,
        key: &str,
    ) -> Result<Option<ConfigPath>, Failed> {
        self.take_string(key)
            .map(|opt| opt.map(|path| self.dir.join(path).into()))
    }

    /// Takes a mandatory path value from the config file.
    ///
    /// This is the pretty much the same as [`take_path`] but also returns
    /// an error if the key does not exist.
    ///
    /// [`take_path`]: #method.take_path
    pub fn take_mandatory_path(
        &mut self,
        key: &str,
    ) -> Result<ConfigPath, Failed> {
        match self.take_path(key)? {
            Some(res) => Ok(res),
            None => {
                error!(
                    "Failed in config file {}: missing required '{}'.",
                    self.path.display(),
                    key
                );
                Err(Failed)
            }
        }
    }

    /// Takes an array of strings from the config file.
    ///
    /// The value is taken from the entry with the given `key` and, if
    /// present, the entry is removed. The value must be an array of strings.
    /// If the key is not present, returns `Ok(None)`. If the entry is present
    /// but not an array of strings, returns an error.
    pub fn take_string_array(
        &mut self,
        key: &str,
    ) -> Result<Option<Vec<String>>, Failed> {
        match self.take_value(key)? {
            Some(toml::Value::Array(vec)) => {
                let mut res = Vec::new();
                for value in vec.into_iter() {
                    if let toml::Value::String(value) = value {
                        res.push(value.into_value())
                    } else {
                        error!(
                            "Failed in config file {}: \
                            '{}' expected to be a array of strings.",
                            self.path.display(),
                            key
                        );
                        return Err(Failed);
                    }
                }
                Ok(Some(res))
            }
            Some(_) => {
                error!(
                    "Failed in config file {}: \
                     '{}' expected to be a array of strings.",
                    self.path.display(),
                    key
                );
                Err(Failed)
            }
            None => Ok(None),
        }
    }

    /// Takes an array of string encoded values from the config file.
    ///
    /// The value is taken from the entry with the given `key` and, if
    /// present, the entry is removed. The value must be an array of strings.
    /// Each string is converted to the output type via `FromStr::from_str`.
    ///
    /// If the key is not present, returns `Ok(None)`. If the entry is present
    /// but not an array of strings or if converting any of the strings fails,
    /// returns an error.
    pub fn take_from_str_array<T>(
        &mut self,
        key: &str,
    ) -> Result<Option<Vec<T>>, Failed>
    where
        T: FromStr,
        T::Err: fmt::Display,
    {
        match self.take_value(key)? {
            Some(toml::Value::Array(vec)) => {
                let mut res = Vec::new();
                for value in vec.into_iter() {
                    if let toml::Value::String(value) = value {
                        match T::from_str(value.value()) {
                            Ok(value) => res.push(value),
                            Err(err) => {
                                error!(
                                    "Failed in config file {}: \
                                     Invalid value in '{}': {}",
                                    self.path.display(),
                                    key,
                                    err
                                );
                                return Err(Failed);
                            }
                        }
                    } else {
                        error!(
                            "Failed in config file {}: \
                            '{}' expected to be a array of strings.",
                            self.path.display(),
                            key
                        );
                        return Err(Failed);
                    }
                }
                Ok(Some(res))
            }
            Some(_) => {
                error!(
                    "Failed in config file {}: \
                     '{}' expected to be a array of strings.",
                    self.path.display(),
                    key
                );
                Err(Failed)
            }
            None => Ok(None),
        }
    }

    /// Takes an array of paths from the config file.
    ///
    /// The values are taken from the given `key` which must be an array of
    /// strings. Each path is treated as relative to the directory of the
    /// config file. All paths are expanded if necessary and are returned as
    /// absolute paths.
    ///
    /// Returns `Ok(None)` if the key does not exist. Returns an error if the
    /// key exists but the value isn’t an array of string.
    pub fn take_path_array(
        &mut self,
        key: &str,
    ) -> Result<Option<Vec<ConfigPath>>, Failed> {
        match self.take_value(key)? {
            Some(toml::Value::String(value)) => {
                Ok(Some(vec![self.dir.join(value.into_value()).into()]))
            }
            Some(toml::Value::Array(vec)) => {
                let mut res = Vec::new();
                for value in vec.into_iter() {
                    if let toml::Value::String(value) = value {
                        res.push(self.dir.join(value.into_value()).into())
                    } else {
                        error!(
                            "Failed in config file {}: \
                            '{}' expected to be a array of paths.",
                            self.path.display(),
                            key
                        );
                        return Err(Failed);
                    }
                }
                Ok(Some(res))
            }
            Some(_) => {
                error!(
                    "Failed in config file {}: \
                     '{}' expected to be a array of paths.",
                    self.path.display(),
                    key
                );
                Err(Failed)
            }
            None => Ok(None),
        }
    }

    /// Takes a string-to-string hashmap from the config file.
    pub fn take_string_map(
        &mut self,
        key: &str,
    ) -> Result<Option<HashMap<String, String>>, Failed> {
        match self.take_value(key)? {
            Some(toml::Value::Array(vec)) => {
                let mut res = HashMap::new();
                for value in vec.into_iter() {
                    let mut pair = match value {
                        toml::Value::Array(pair) => pair.into_iter(),
                        _ => {
                            error!(
                                "Failed in config file {}: \
                                '{}' expected to be a array of string pairs.",
                                self.path.display(),
                                key
                            );
                            return Err(Failed);
                        }
                    };
                    let left = match pair.next() {
                        Some(toml::Value::String(value)) => value,
                        _ => {
                            error!(
                                "Failed in config file {}: \
                                '{}' expected to be a array of string pairs.",
                                self.path.display(),
                                key
                            );
                            return Err(Failed);
                        }
                    };
                    let right = match pair.next() {
                        Some(toml::Value::String(value)) => value,
                        _ => {
                            error!(
                                "Failed in config file {}: \
                                '{}' expected to be a array of string pairs.",
                                self.path.display(),
                                key
                            );
                            return Err(Failed);
                        }
                    };
                    if pair.next().is_some() {
                        error!(
                            "Failed in config file {}: \
                            '{}' expected to be a array of string pairs.",
                            self.path.display(),
                            key
                        );
                        return Err(Failed);
                    }
                    if res
                        .insert(left.into_value(), right.into_value())
                        .is_some()
                    {
                        error!(
                            "Failed in config file {}: \
                            'duplicate item in '{}'.",
                            self.path.display(),
                            key
                        );
                        return Err(Failed);
                    }
                }
                Ok(Some(res))
            }
            Some(_) => {
                error!(
                    "Failed in config file {}: \
                     '{}' expected to be a array of string pairs.",
                    self.path.display(),
                    key
                );
                Err(Failed)
            }
            None => Ok(None),
        }
    }

    /// Checks whether the config file is now empty.
    ///
    /// If it isn’t, logs a complaint and returns an error.
    pub fn check_exhausted(&self) -> Result<(), Failed> {
        if !self.content.is_empty() {
            print!(
                "Failed in config file {}: Unknown settings ",
                self.path.display()
            );
            let mut first = true;
            for (key, _) in self.content.iter() {
                if !first {
                    print!(",");
                } else {
                    first = false
                }
                print!("{key}");
            }
            error!(".");
            Err(Failed)
        } else {
            Ok(())
        }
    }

    /// Inserts a string value.
    pub fn insert_string(&mut self, key: &str, value: impl ToString) {
        self.content.insert(
            key,
            toml::Item::Value(toml::Value::String(toml::Formatted::new(
                value.to_string(),
            ))),
        );
    }

    /// Insert a path value.
    pub fn insert_path(&mut self, key: &str, path: &Path) {
        let path = match path.strip_prefix(&self.dir) {
            Ok(path) => path,
            Err(_) => path,
        };
        self.insert_string(key, path.display())
    }
}

//------------ ConfigPath ----------------------------------------------------

/// A path encountered in a config file.
///
/// This is a basically a `PathBuf` that, when, deserialized or used as a
/// command line argument resolves all relative paths into absolute paths.
///
/// When used as a command line argument with clap, it will use the current
/// working directory as the base path.
///
/// When used with serde, the base path can be through
/// [`set_base_path`][Self::set_base_path] before deserialization or
/// serialization and cleared afterwards with
/// [`clear_base_path`.][Self::clear_base_path].
///
/// Under the hood, this uses a thread local variable, so (de-) serializers
/// that somehow spawn threads may not work as expected.
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ConfigPath(PathBuf);

impl ConfigPath {
    thread_local!(
        static BASE_PATH: RefCell<Option<PathBuf>> = const {
            RefCell::new(None)
        }
    );

    pub fn set_base_path(path: PathBuf) {
        Self::BASE_PATH.with(|base_path| {
            base_path.replace(Some(path));
        })
    }

    pub fn clear_base_path() {
        Self::BASE_PATH.with(|base_path| {
            base_path.replace(None);
        })
    }

    fn construct(path: PathBuf) -> Self {
        Self::BASE_PATH.with(|base_path| {
            Self(match base_path.borrow().as_ref() {
                Some(base_path) => base_path.join(&path),
                None => path,
            })
        })
    }

    fn deconstruct(&self) -> &Path {
        Self::BASE_PATH.with(|base_path| match base_path.borrow().as_ref() {
            Some(base_path) => match self.0.strip_prefix(base_path) {
                Ok(path) => path,
                Err(_) => self.0.as_ref(),
            },
            None => self.0.as_ref(),
        })
    }
}

impl ConfigPath {
    /// Returns the reference to the actual path.
    pub fn as_path(&self) -> &Path {
        self.0.as_ref()
    }
}

impl From<PathBuf> for ConfigPath {
    fn from(path: PathBuf) -> Self {
        Self::construct(path)
    }
}

impl From<String> for ConfigPath {
    fn from(path: String) -> Self {
        Self::construct(path.into())
    }
}

impl From<ConfigPath> for PathBuf {
    fn from(path: ConfigPath) -> Self {
        path.0
    }
}

impl ops::Deref for ConfigPath {
    type Target = Path;

    fn deref(&self) -> &Self::Target {
        self.0.as_ref()
    }
}

impl AsRef<Path> for ConfigPath {
    fn as_ref(&self) -> &Path {
        self.0.as_ref()
    }
}

impl<'de> Deserialize<'de> for ConfigPath {
    fn deserialize<D: Deserializer<'de>>(
        deserializer: D,
    ) -> Result<Self, D::Error> {
        Ok(Self::construct(PathBuf::deserialize(deserializer)?))
    }
}

impl Serialize for ConfigPath {
    fn serialize<S: Serializer>(
        &self,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        self.deconstruct().serialize(serializer)
    }
}

impl clap::builder::ValueParserFactory for ConfigPath {
    type Parser = ConfigPathParser;

    fn value_parser() -> Self::Parser {
        ConfigPathParser
    }
}

//------------ ConfigPathParser ----------------------------------------------

#[derive(Clone)]
pub struct ConfigPathParser;

impl clap::builder::TypedValueParser for ConfigPathParser {
    type Value = ConfigPath;

    fn parse_ref(
        &self,
        cmd: &clap::Command,
        arg: Option<&clap::Arg>,
        value: &std::ffi::OsStr,
    ) -> Result<Self::Value, clap::Error> {
        let path = clap::builder::PathBufValueParser::new()
            .parse_ref(cmd, arg, value)?;
        if path.is_absolute() {
            return Ok(ConfigPath(path));
        }
        let cur_dir = match env::current_dir() {
            Ok(dir) => dir,
            Err(err) => {
                let mut res = clap::Error::new(clap::error::ErrorKind::Io)
                    .with_cmd(cmd);
                res.insert(
                    clap::error::ContextKind::Custom,
                    clap::error::ContextValue::String(format!(
                        "Failed to get current directory: {err}"
                    )),
                );
                return Err(res);
            }
        };
        Ok(ConfigPath(cur_dir.join(path)))
    }
}