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