1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//! Loading values from paths.
//!
//! This crate provides the [`FromPath`] trait for types that can have values loaded from paths.
//!
//! There are also the [`load`] convenience function and the [`Load`] extension trait for
//! when the types can be inferred.
//!
//! # Example
//!
//! Firstly, we need to implement the [`FromPath`] trait, and then use it via [`load`] or [`Load`]:
//!
//! ```rust
//! use std::{fs::read_to_string, io::Error, path::Path};
//!
//! use from_path::{load, FromPath, Load};
//!
//! #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
//! pub 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(Self::new)
//! }
//! }
//!
//! let path = "hello-world";
//!
//! let content: Content = load(path).unwrap();
//!
//! assert_eq!(content.string.trim(), "Hello, world!");
//!
//! let extended: Content = path.load().unwrap();
//!
//! assert_eq!(content, extended);
//! ```
use Path;
/// Loading values from paths.
/// Loads the value of the given type from the given path.
///
/// # Errors
///
/// Returns [`Error`] when loading fails.
///
/// [`Error`]: FromPath::Error
/// Loading values from paths (sealed extension trait).