Trait brane_cfg::spec::Config

source ·
pub trait Config: Clone + Debug {
    type Error: Error;

    // Required methods
    fn to_string(
        &self,
        pretty: bool
    ) -> Result<String, ConfigError<Self::Error>>;
    fn to_writer(
        &self,
        writer: impl Write,
        pretty: bool
    ) -> Result<(), ConfigError<Self::Error>>;
    fn from_string(
        raw: impl AsRef<str>
    ) -> Result<Self, ConfigError<Self::Error>>;
    fn from_reader(reader: impl Read) -> Result<Self, ConfigError<Self::Error>>;

    // Provided methods
    fn to_path(
        &self,
        path: impl AsRef<Path>
    ) -> Result<(), ConfigError<Self::Error>> { ... }
    fn from_path(
        path: impl AsRef<Path>
    ) -> Result<Self, ConfigError<Self::Error>> { ... }
    fn from_path_async<'async_trait>(
        path: impl 'async_trait + Send + AsRef<Path>
    ) -> Pin<Box<dyn Future<Output = Result<Self, ConfigError<Self::Error>>> + Send + 'async_trait>>
       where Self: Send + 'async_trait { ... }
}
Expand description

Defines a serializable struct that we typically use as configuration for a service.

Required Associated Types§

source

type Error: Error

The types of errors that may be thrown by the serialization function(s).

Required Methods§

source

fn to_string(&self, pretty: bool) -> Result<String, ConfigError<Self::Error>>

Serializes this Config to a string.

Arguments
  • pretty: If true, then it will be serialized using a pretty version of the backend (if available).
Returns

A new String that represents this config but serialized.

Errors

This function may error if the serialization failed.

source

fn to_writer( &self, writer: impl Write, pretty: bool ) -> Result<(), ConfigError<Self::Error>>

Serializes this Config to a reader.

Arguments
  • writer: The Writer to write the serialized representation to.
  • pretty: If true, then it will be serialized using a pretty version of the backend (if available).
Errors

This function may error if the serialization failed or if we failed to write to the given writer.

source

fn from_string(raw: impl AsRef<str>) -> Result<Self, ConfigError<Self::Error>>

Deserializes the given string to an instance of ourselves.

Arguments
  • raw: The raw string to deserialize.
Returns

A new instance of Self with its contents read from the given raw string.

Errors

This function may fail if the input string was invalid for this object.

source

fn from_reader(reader: impl Read) -> Result<Self, ConfigError<Self::Error>>

Deserializes the contents of the given reader to an instance of ourselves.

Arguments
  • reader: The Reader who’s contents to deserialize.
Returns

A new instance of Self with its contents read from the given reader.

Errors

This function may fail if we failed to read from the reader or if its contents were invalid for this object.

Provided Methods§

source

fn to_path( &self, path: impl AsRef<Path> ) -> Result<(), ConfigError<Self::Error>>

Serializes this Config to a file at the given path.

This will always choose a pretty representation of the serialization (if applicable).

Arguments
  • path: The path where to write the file to.
Errors

This function may error if the serialization failed or if we failed to create and/or write to the file.

source

fn from_path(path: impl AsRef<Path>) -> Result<Self, ConfigError<Self::Error>>

Deserializes this Config from the file at the given path.

Arguments
  • path: The path where to read the file from.
Errors

This function may fail if we failed to open/read from the file or if its contents were invalid for this object.

source

fn from_path_async<'async_trait>( path: impl 'async_trait + Send + AsRef<Path> ) -> Pin<Box<dyn Future<Output = Result<Self, ConfigError<Self::Error>>> + Send + 'async_trait>>where Self: Send + 'async_trait,

Deserializes this Config from the file at the given path, with the reading part done asynchronously.

Note that the parsing path cannot be done asynchronously. Also, note that, because serde does not support asynchronous deserialization, we have to read the entire file in one go.

Arguments
  • path: The path where to read the file from.
Errors

This function may fail if we failed to open/read from the file or if its contents were invalid for this object.

Implementors§

source§

impl<T: DeserializeOwned + Serialize + for<'de> YamlConfig<'de>> Config for T

§

type Error = Error