pub struct ConfigView<'a> { /* private fields */ }Expand description
The typed reads of a key file in the form of config.
The view borrows the key file and holds nothing else. It has no
override, so the value in effect of a key is the value of the file or
the default of the key, and a lookup gives Lookup::File or
Lookup::Default. Each read parses the value of the file each time
that it is called.
In the ostrya crate, RepoConfig gives the same reads of the
repository config, with the overrides of the repository handle and the
remotes directory.
§Examples
use ostrya_core::KeyFile;
use ostrya_core::config::{ConfigError, ConfigView, Lookup, repo};
ostrya_core::config_group! {
pub group image = named "ex-image" {
Arch: String = "arch", required;
}
}
let kf = KeyFile::parse(
"[core]\nrepo_version=1\nmode=bare\nfsync=false\n\
[ex-image \"base\"]\narch=x86\n",
)?;
let view = ConfigView::new(&kf);
assert!(!view.get::<repo::Fsync>()?);
assert_eq!(view.lookup::<repo::Locking>()?, Some(Lookup::Default(true)));
let base = view.section::<image::Group>("base").unwrap();
assert_eq!(base.get::<image::Arch>()?, "x86");Implementations§
Source§impl<'a> ConfigView<'a>
impl<'a> ConfigView<'a>
Sourcepub fn new(keyfile: &'a KeyFile) -> ConfigView<'a>
pub fn new(keyfile: &'a KeyFile) -> ConfigView<'a>
Returns the typed reads of keyfile.
Sourcepub fn get<K>(&self) -> Result<OutputOf<K>, ConfigError>
pub fn get<K>(&self) -> Result<OutputOf<K>, ConfigError>
Returns the value in effect of the key K of a single group: the
value of the file, or the default of the key if the file does not set
it.
The result type is Key::Output. It is Option<V> for a key with
no default that is not required.
§Errors
- The error of the value type if the value does not parse.
ValueErrorstates the text. ConfigError::InvalidFormatif the key is required and absent.
Sourcepub fn file_value<K>(&self) -> Result<Option<ValueOf<K>>, ConfigError>
pub fn file_value<K>(&self) -> Result<Option<ValueOf<K>>, ConfigError>
Returns the value of the key K of a single group in the file, or
None if the file does not set it. The default of the key does not
apply.
§Errors
The error of the value type if the value does not parse.
ValueError states the text.
Sourcepub fn lookup<K>(&self) -> Result<Option<Lookup<ValueOf<K>>>, ConfigError>
pub fn lookup<K>(&self) -> Result<Option<Lookup<ValueOf<K>>>, ConfigError>
Returns the value in effect of the key K of a single group and its
source, or None if the file does not set the key and the key has no
default.
The view has no override, so the source is Lookup::File or
Lookup::Default.
§Errors
The error of the value type if the value does not parse.
ValueError states the text.
Sourcepub fn section<G: Group>(&self, name: &str) -> Option<Section<'a, G>>
pub fn section<G: Group>(&self, name: &str) -> Option<Section<'a, G>>
Returns the section name of the named group G, or None if the
file has no such section.
The call gives None for an empty name and for a name that holds
". sections skips the headers of such
sections, so they are not sections of the group.
Sourcepub fn sections<G: Group>(
&self,
) -> impl Iterator<Item = Section<'a, G>> + use<'a, G>
pub fn sections<G: Group>( &self, ) -> impl Iterator<Item = Section<'a, G>> + use<'a, G>
Returns each section of the named group G, in the order of the
file.
The call skips a header of the group whose section name is empty or
holds ", for example [NAME ""] or [NAME "a" "b"].
Sourcepub fn refuse_unknown_keys<G: Group>(&self) -> Result<(), ConfigError>
pub fn refuse_unknown_keys<G: Group>(&self) -> Result<(), ConfigError>
Refuses a key of the group G that the closed group does not list.
For a single group, the call checks the group [NAME]. For a named
group, it checks each section of the group, in the order of the file,
as Section::refuse_unknown_keys does. The call checks nothing if
Group::CLOSED of G is false. It checks each key against the
key list of G alone, and gives the unknown-key text for a key
outside the list. It names the first such key in the order of the
file.
§Errors
ConfigError::InvalidFormat with the text [HEADER] holds the unknown key 'KEY', where HEADER is the header of the group.
§Examples
use ostrya_core::KeyFile;
use ostrya_core::config::{ConfigError, ConfigView};
ostrya_core::config_group! {
pub group tool = "ex-tool", closed {
Jobs: Option<u32> = "jobs";
}
}
let kf = KeyFile::parse("[ex-tool]\njobs=4\n")?;
ConfigView::new(&kf).refuse_unknown_keys::<tool::Group>()?;
let kf = KeyFile::parse("[ex-tool]\nzzz=1\n")?;
assert_eq!(
ConfigView::new(&kf)
.refuse_unknown_keys::<tool::Group>()
.unwrap_err()
.to_string(),
"invalid format: [ex-tool] holds the unknown key 'zzz'"
);