Expand description
Loading values from paths.
This crate provides the FromPath trait for types that can have values loaded from paths.
There is also the from_path convenience function for when the types can be inferred.
§Example
Assuming there is some file named hello-world with the content Hello, world!,
we first implement the trait and then load the content from the file:
use std::{fs::read_to_string, io::Error, path::Path};
use from_path::{FromPath, from_path};
struct Content {
pub string: String,
}
impl Content {
pub fn new(string: String) -> Self {
Self { string }
}
}
impl FromPath for Content {
type Error = Error;
fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, Self::Error> {
read_to_string(path).map(Content::new)
}
}
let content: Content = from_path("hello-world").unwrap();
assert_eq!(content.string.trim(), "Hello, world!");Traits§
- From
Path - Loading values from paths.
Functions§
- from_
path - Loads the value of the given type from the given path.