Skip to main content

ConfigView

Struct ConfigView 

Source
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>

Source

pub fn new(keyfile: &'a KeyFile) -> ConfigView<'a>

Returns the typed reads of keyfile.

Source

pub fn get<K>(&self) -> Result<OutputOf<K>, ConfigError>
where K: SingleKey + Key<<K as SingleKey>::Group>,

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
Source

pub fn file_value<K>(&self) -> Result<Option<ValueOf<K>>, ConfigError>
where K: SingleKey + Key<<K as SingleKey>::Group>,

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.

Source

pub fn lookup<K>(&self) -> Result<Option<Lookup<ValueOf<K>>>, ConfigError>
where K: SingleKey + Key<<K as SingleKey>::Group>,

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.

Source

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.

Source

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"].

Source

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'"
);

Trait Implementations§

Source§

impl<'a> Clone for ConfigView<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for ConfigView<'a>

Source§

impl<'a> Debug for ConfigView<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Forget for ConfigView<'a>

§

impl<'a> Freeze for ConfigView<'a>
where &'a KeyFile: Freeze,

§

impl<'a> Move for ConfigView<'a>

§

impl<'a> RefUnwindSafe for ConfigView<'a>

§

impl<'a> Send for ConfigView<'a>
where &'a KeyFile: Send,

§

impl<'a> Sync for ConfigView<'a>
where RawVecInner: Sync, RawTable<(String, usize)>: Sync,

§

impl<'a> Unpin for ConfigView<'a>
where &'a KeyFile: Unpin,

§

impl<'a> UnsafeUnpin for ConfigView<'a>
where &'a KeyFile: UnsafeUnpin,

§

impl<'a> UnwindSafe for ConfigView<'a>
where &'a KeyFile: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.