Struct Datastore

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

Handle for a YAML datastore.

Open with open(). Access with get().

Implementations§

Source§

impl Datastore

Source

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.

Source

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:

  1. The entire contents of file a/b/c/d.yaml.
  2. The contents of the key d in a/b/c.yaml.
  3. The contents of the key c.d in a/b.yaml.
  4. The contents of the key b.c.d in a.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.

Source

pub fn get_with_path<P, T>(&self, path: P) -> Result<T, Error>
where P: AsRef<Path>, T: DeserializeOwned,

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

pub fn get_with_key<P, T>(&self, path: P, key: &str) -> Result<T, Error>
where P: AsRef<Path>, T: DeserializeOwned,

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

pub fn get_with_key_vec<P, T, S>( &self, path: P, key_vec: &[S], ) -> Result<T, Error>
where P: AsRef<Path>, T: DeserializeOwned, S: AsRef<str> + Index,

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.

Trait Implementations§

Source§

impl Debug for Datastore

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Datastore

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Datastore

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

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

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,