Trait FromFile

Source
pub trait FromFile {
    // Provided methods
    fn from_file(input: &str) -> Result<Self, FromFileError>
       where for<'de> Self: Deserialize<'de> + Sized { ... }
    fn from_yml_file(input: &str) -> Result<Self, FromFileError>
       where for<'de> Self: Deserialize<'de> + Sized { ... }
    fn from_json_file(input: &str) -> Result<Self, FromFileError>
       where for<'de> Self: Deserialize<'de> + Sized { ... }
    fn get_file_path(input: &str) -> Result<String, FromFileError> { ... }
    fn file_read(input: String) -> Result<String, FromFileError> { ... }
    fn from_yaml_string(contents: String) -> Result<Self, FromFileError>
       where for<'de> Self: Deserialize<'de> { ... }
    fn from_json_string(contents: String) -> Result<Self, FromFileError>
       where for<'de> Self: Deserialize<'de> { ... }
}
Expand description

Implement this trait to enable your Struct’s to be deserialized from a file-path like

  • conf/app.yaml
  • file:conf/app.yaml

Provided Methods§

Source

fn from_file(input: &str) -> Result<Self, FromFileError>
where for<'de> Self: Deserialize<'de> + Sized,

Support serialising to .yml, .yaml & .json files by looking at the file extension and then choosing the correct serde method

§Examples
#[derive(Deserialize)]
struct Person {
    name: String
}

impl FromFile for Person {}

fn main() {
    let path = "test/fixtures/person.json";
    let person = Person::from_file(path).expect("deserialize from file");
    assert_eq!(person.name, String::from("Shane"));
}
Source

fn from_yml_file(input: &str) -> Result<Self, FromFileError>
where for<'de> Self: Deserialize<'de> + Sized,

From a string like file:config.yaml, try to read the file and if it exists, parse into a strongly typed struct Self

Source

fn from_json_file(input: &str) -> Result<Self, FromFileError>
where for<'de> Self: Deserialize<'de> + Sized,

From a string like file:config.yaml, try to read the file and if it exists, parse into a strongly typed struct Self

Source

fn get_file_path(input: &str) -> Result<String, FromFileError>

Parse strings like file:config.yaml to extract the file path only

Source

fn file_read(input: String) -> Result<String, FromFileError>

Attempt to Read the file’s contents into a string

Source

fn from_yaml_string(contents: String) -> Result<Self, FromFileError>
where for<'de> Self: Deserialize<'de>,

Parse any YAML string directly into a Self

Source

fn from_json_string(contents: String) -> Result<Self, FromFileError>
where for<'de> Self: Deserialize<'de>,

Parse json string directly into a Self

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§