Trait from_file::FromFile

source ·
pub trait FromFile {
    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§

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

Examples
#[derive(Deserialize, Debug, PartialEq)]
struct Person {
  name: String
}

impl FromFile for Person {}

let p1 = Person::from_file("test/fixtures/person.json").expect("file->Person");
assert_eq!(p1, Person{name: "Shane".into()});

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

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

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

Attempt to Read the file’s contents into a string

Parse any YAML string directly into a Self

Parse json string directly into a Self

Implementors§