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)]
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"));
}

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

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

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§