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
use std::fmt::{Debug, Formatter, Result as FormatResult};
use std::{any::type_name, path::PathBuf};
use tokens::{ChangeToken, NeverChangeToken};
use crate::Value;
/// Defines the possible load errors.
#[derive(PartialEq, Clone)]
pub enum LoadError {
/// Indicates a generic load error with an error message.
Generic(String),
/// Indicates a file load error.
File {
/// Gets the error message.
message: String,
/// Gets the path of the file being loaded.
path: PathBuf,
},
}
impl LoadError {
/// Gets the load error message.
pub fn message(&self) -> &str {
match self {
Self::Generic(message) => message,
Self::File { message, .. } => message,
}
}
}
impl Debug for LoadError {
fn fmt(&self, f: &mut Formatter<'_>) -> FormatResult {
match self {
Self::Generic(message) => f.write_str(message),
Self::File { message, .. } => f.write_str(message),
}
}
}
/// Represents a configuration load result.
pub type LoadResult = std::result::Result<(), LoadError>;
/// Defines the behavior of an object that provides configuration key/values for an application.
pub trait ConfigurationProvider {
/// Gets the name of the provider.
fn name(&self) -> &str {
type_name::<Self>()
}
/// Attempts to get a configuration value with the specified key.
///
/// # Arguments
///
/// * `key` - The key of the value to retrieve
fn get(&self, key: &str) -> Option<Value>;
/// Returns a [`ChangeToken`](tokens::ChangeToken) if this provider supports change tracking.
fn reload_token(&self) -> Box<dyn ChangeToken> {
Box::new(NeverChangeToken::new())
}
/// Loads the configuration values from the implemented source.
fn load(&mut self) -> LoadResult {
Ok(())
}
/// Gets the immediate descendent configuration keys for a given parent path based
/// on this [`ConfigurationProvider`] and the set of keys returned by all of the
/// preceding [`ConfigurationProvider`].
///
/// # Arguments
///
/// * `earlier_keys` - The sequence of keys returned by preceding provider for the same parent path
/// * `parent_path` - The optional parent path to evaluate
fn child_keys(&self, earlier_keys: &mut Vec<String>, parent_path: Option<&str>);
}