pub struct Datastore { /* private fields */ }
Implementations§
Source§impl Datastore
impl Datastore
Sourcepub fn open<P: Into<PathBuf>>(path: P) -> Datastore
pub fn open<P: Into<PathBuf>>(path: P) -> Datastore
Open a handle to a datastore at the given path.
At present, this doesn’t actually perform any operations.
Sourcepub fn get<T: DeserializeOwned>(&self, keypath: &str) -> Result<T, Error>
pub fn get<T: DeserializeOwned>(&self, keypath: &str) -> Result<T, Error>
Get a value from the datastore given a keypath.
This method parses the given string into a KeyPath
and then iterates over the possible path
and key combinations until it finds a match or has exhausted them. It starts with the longest
possible path and shortest key and works backwards.
More explicitly, it will search each possible path for the given keypath, starting with the longest. If it finds a file that matches, it attempts to use the remainder of the keypath as keys into the YAML file. If the full keypath matches a file path, then the entire YAML file data is returned.
See the documentation for keypath for more information on how the keypath is used to generate combinations.
§Examples
For a keypath of a.b.c.d
, the first match of the following would be returned if found:
- The entire contents of file
a/b/c/d.yaml
. - The contents of the key
d
ina/b/c.yaml
. - The contents of the key
c.d
ina/b.yaml
. - The contents of the key
b.c.d
ina.yaml
.
For the above, the dot-notation for YAML keys implies nesting. So for b.c.d
:
b:
c:
d: 42
§Errors
Returns Error::KeyPathError
if keypath
is invalid.
Returns Error::KeyNotFound
if the given key was not found.
Sourcepub fn get_with_path<P, T>(&self, path: P) -> Result<T, Error>
pub fn get_with_path<P, T>(&self, path: P) -> Result<T, Error>
Get all the data from a given YAML file in the datastore.
This function makes no assumptions about the underlying YAML data other than it being valid.
On success, returns an object of the specified return type.
§Errors
Will return Error::IOError
if a file at path
cannot be read.
Will return Error::DataParseError
if:
- A file at
path
is not able to be parsed as valid YAML - The return type specified does not match the type found in the input file.
Sourcepub fn get_with_key<P, T>(&self, path: P, key: &str) -> Result<T, Error>
pub fn get_with_key<P, T>(&self, path: P, key: &str) -> Result<T, Error>
Get a value from the given YAML file in the datastore based on a key.
This function assumes the input YAML is a mapping.
On success, returns an object of the specified return type.
§Errors
Will return Error::IOError
if a file at path
cannot be read.
Will return Error::DataParseError
if:
- A file at
path
is not able to be parsed as valid YAML - The return type specified does not match the type found in the input file.
Will return Error::KeyNotFound
if the given key was not found in a top-level map of the YAML file.
- A file at
path
is not able to be parsed as valid YAML - The return type specified does not match the type found in the input file.
Sourcepub fn get_with_key_vec<P, T, S>(
&self,
path: P,
key_vec: &[S],
) -> Result<T, Error>
pub fn get_with_key_vec<P, T, S>( &self, path: P, key_vec: &[S], ) -> Result<T, Error>
Get a value from the given YAML file in the datastore based on a set of keys.
This function assumes the input YAML is a mapping.
It traverses each element of key_vec
and treats it as a level of nesting.
For example, for the given input:
outer:
middle:
inner: 42
In order to get the value of inner
(42), A key_vec
would be passed as:
vec!["outer", "middle", "inner"]
On success, returns an object of the specified return type.
§Errors
Will return Error::IOError
if a file at path
cannot be read.
Will return Error::DataParseError
if:
- A file at
path
is not able to be parsed as valid YAML - The return type specified does not match the type found in the input file.
Will return Error::KeyNotFound
if the given key was not found in a top-level map of the YAML file.
- A file at
path
is not able to be parsed as valid YAML - The return type specified does not match the type found in the input file.