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§
sourcefn from_file(input: &str) -> Result<Self, FromFileError>where
for<'de> Self: Deserialize<'de> + Sized,
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, 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()});
sourcefn from_yml_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,
From a string like file:config.yaml
, try to read the file
and if it exists, parse into a strongly typed struct Person
sourcefn from_json_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,
From a string like file:config.yaml
, try to read the file
and if it exists, parse into a strongly typed struct Person
sourcefn get_file_path(input: &str) -> Result<String, FromFileError>
fn get_file_path(input: &str) -> Result<String, FromFileError>
Parse strings like file:config.yaml to extract the file path only
sourcefn file_read(input: String) -> Result<String, FromFileError>
fn file_read(input: String) -> Result<String, FromFileError>
Attempt to Read the file’s contents into a string
sourcefn from_yaml_string(contents: String) -> Result<Self, FromFileError>where
for<'de> Self: Deserialize<'de>,
fn from_yaml_string(contents: String) -> Result<Self, FromFileError>where
for<'de> Self: Deserialize<'de>,
Parse any YAML string directly into a Self
sourcefn from_json_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>,
Parse json string directly into a Self