Trait justconfig::item::ValueExtractor
source · [−]pub trait ValueExtractor<T: FromStr> {
fn try_value(self) -> Result<Option<T>, ConfigError>;
fn value(self) -> Result<T, ConfigError>;
fn values<R: RangeBounds<usize>>(
self,
range: R
) -> Result<Vec<T>, ConfigError>;
}
Expand description
Trait implemented for TypedItem
and StringItem
to allow retrieval of the
stored config value.
This Trait is implemented for Result<TypedItem<T>, ConfigError>
and
Result<StringItem, ConfigError>
. This makes sure that the methods can be
called on the raw StringItems
and on the TypedItems
returned by
validators.
The Implementation for StringItem
will do the same conversion that is
normally done when calling a validator.
Required Methods
fn try_value(self) -> Result<Option<T>, ConfigError>
fn try_value(self) -> Result<Option<T>, ConfigError>
Returns a configuration value if it exists or ´None´ otherwise.
An error is only returned if one of the following occures:
- The value of the configuration item could not be converted into the required data type.
- A processor or validator returned an error.
- There is more that one value available.
This method should be used to return optional configuration values.
A default value can be provided by using unwrap_or
.
Example
let myvalue: u32 = conf.get(ConfPath::from(&["myvalue"])).try_value().expect("Error").unwrap_or(0);
fn value(self) -> Result<T, ConfigError>
fn value(self) -> Result<T, ConfigError>
Returns a configuration value or raises an error if it does not exists.
This method works like try_value()
but returns an error if the
configuration item does not exist.
This method should be used to return mandatory configuration values that should result in an error if they are not found.
Example
let myvalue: u32 = conf.get(ConfPath::from(&["myvalue"])).value().expect("Error or not found");
fn values<R: RangeBounds<usize>>(self, range: R) -> Result<Vec<T>, ConfigError>
fn values<R: RangeBounds<usize>>(self, range: R) -> Result<Vec<T>, ConfigError>
Returns all configuration values for a configuration item.
This is the only method that allows more than one configuration value to exist. Use this method to read multi value items. If the configuration item does not exist, an empty array is returned.
The method accepts a range to specify how many configuration values can
be set for this item. If any number of configuration values are
acceptible, just specify ..
. If the number of values should be
limited, specify a range.
Example
let myvalue: Vec<u32> = conf.get(ConfPath::from(&["myvalue"])).values(..).expect("Error");
If there must be at least one instance of myvalue
specify a range of
1..
:
let myvalue: Vec<u32> = conf.get(ConfPath::from(&["myvalue"])).values(1..).expect("Error");
If the number of values should be limited to at most 3 values the range
must be ..=3
.