Form

Struct Form 

Source
pub struct Form { /* private fields */ }
Expand description

A configuration data structure that is kind-checked at runtime. Items are declared using the item member function, after which their value can be updated but their kind (bool, int, float, string) cannot change.

Implementations§

Source§

impl Form

Source

pub fn new() -> Form

Creates a blank form.

Source

pub fn item<T: Into<Value>>(self, key: &str, default: T, about: &str) -> Self

Declares a new config item. Any item already declared with that name is replaced.

§Arguments
  • key - The name of the config item
  • default - The default value
  • about - A description of the item for use in user reporting
Source

pub fn merge_value_map_freezing( self, items: &HashMap<String, Value>, to_freeze: &[&str], ) -> Result<Self, ConfigError>

Merges in the contents of a string-value map, and freeze any of those items which are named in the given vector of keys to be frozen.

§Arguments
  • items - A map of values to update the map with
  • to_freeze - A vector of keys to freeze, if the key is in items
Source

pub fn merge_value_map( self, items: &HashMap<String, Value>, ) -> Result<Self, ConfigError>

Merges in the contents of a string-value map. The result is an error if any of the new keys have not already been declared in the form, or if they were declared as a different type.

§Arguments
  • items - A map of values to update the map with
Source

pub fn merge_string_map( self, dict: &HashMap<String, String>, ) -> Result<Self, ConfigError>

Merges in the contents of a string-string map. The result is an error if any of the new keys have not already been declared in the form, or if any of the value strings do not parse to the declared type.

§Arguments
  • dict - A map of string to update the map with
Source

pub fn merge_string_args<T: IntoIterator<Item = U>, U: Into<String>>( self, args: T, ) -> Result<Self, ConfigError>

Merges in a sequence of “key=value” pairs. The result is an error if any of the new keys have not already been declared in the form, or if any of the value strings do not parse to the declared type.

§Arguments
  • args - Iterator of string to update the map with
§Example
let form = base.merge_string_args(std::env::args().skip(1)).unwrap();
Source

pub fn merge_string_args_allowing_duplicates<T: IntoIterator<Item = U>, U: Into<String>>( self, args: T, ) -> Result<Self, ConfigError>

Source

pub fn freeze(self, key: &str) -> Self

Freezes a parameter with the given name, if it exists, or otherwise panic.

Source

pub fn value_map(&self) -> HashMap<String, Value>

Returns a hash map of the (key, value) items, stripping out the about messages. If the HDF5 feature is enabled, the result can be written directly to an HDF5 group via io::write_to_hdf5.

Source

pub fn iter(&self) -> impl Iterator<Item = (&str, &Value)>

Source

pub fn len(&self) -> usize

Returns the number of items.

Source

pub fn is_empty(&self) -> bool

Returns whether the map is empty.

Source

pub fn sorted_keys(&self) -> Vec<String>

Returns a vector of the keys in this map, sorted alphabetically.

Source

pub fn get(&self, key: &str) -> &Value

Gets an item from the form. Panics if the item was not declared.

§Arguments
  • key - The key to get
§Example
let x: i64 = form.get("counter").into(); // fails unless "counter" is declared has kind i64
Source

pub fn about(&self, key: &str) -> &str

Source

pub fn is_frozen(&self, key: &str) -> bool

Trait Implementations§

Source§

impl Default for Form

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl IntoIterator for Form

Source§

type Item = <HashMap<String, Parameter> as IntoIterator>::Item

The type of the elements being iterated over.
Source§

type IntoIter = <HashMap<String, Parameter> as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl Freeze for Form

§

impl RefUnwindSafe for Form

§

impl Send for Form

§

impl Sync for Form

§

impl Unpin for Form

§

impl UnwindSafe for Form

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.