Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 #![deny(missing_docs)]
2 #![warn(rust_2018_idioms)]
3 #![doc(html_root_url = "https://docs.rs/config-file/0.2.3/")]
4
5 //! # Read and parse configuration file automatically
6 //!
7 //! config-file reads your configuration files and parse them automatically using their extension.
8 //!
9 //! # Features
10 //!
11 //! - toml is enabled by default
12 //! - json is optional
13 //! - xml is optional
14 //! - yaml is optional
15 //!
16 //! # Examples
17 //!
18 //! ```rust,no_run
19 //! use config_file::FromConfigFile;
20 //! use serde::Deserialize;
21 //!
22 //! #[derive(Deserialize)]
23 //! struct Config {
24 //! host: String,
25 //! }
26 //!
27 //! let config = Config::from_config_file("/etc/myconfig.toml").unwrap();
28 //! ```
29
30 use serderust-analyzer cargo serde 1.0.229 crate/::derust-analyzer cargo serde_core 1.0.229 de/::DeserializeOwnedrust-analyzer cargo serde_core 1.0.229 de/DeserializeOwned#;
31 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#, fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#, pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#};
32 use thiserrorrust-analyzer cargo thiserror 1.0.69 crate/::Errorrust-analyzer cargo thiserror-impl 1.0.69 Error:;
33 #[cfg(feature = "toml")]
34 use toml_craterust-analyzer cargo toml 0.8.23 crate/ as tomlrust-analyzer cargo toml 0.8.23 crate/;
35
36 /// Trait for loading a struct from a configuration file.
37 /// This trait is automatically implemented when serde::Deserialize is.
38 pub trait FromConfigFilerust-analyzer cargo config-file 0.2.3 FromConfigFile# {
39 /// Load ourselves from the configuration file located at @path
40 fn from_config_filerust-analyzer cargo config-file 0.2.3 FromConfigFile#from_config_file().<P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>>(path: P) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Self, ConfigFileErrorrust-analyzer cargo config-file 0.2.3 ConfigFileError#>
41 where
42 Self: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#;
43 }
44
45 impl<C: DeserializeOwnedrust-analyzer cargo serde_core 1.0.229 de/DeserializeOwned#> FromConfigFilerust-analyzer cargo config-file 0.2.3 FromConfigFile# for C {
46 fn from_config_filerust-analyzer cargo config-file 0.2.3 impl#[C][FromConfigFile]from_config_file().<P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>>(path: P) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo config-file 0.2.3 impl#[C][FromConfigFile], ConfigFileErrorrust-analyzer cargo config-file 0.2.3 ConfigFileError#>
47 where
48 Selfrust-analyzer cargo config-file 0.2.3 impl#[C][FromConfigFile]: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#,
49 {
50 let path = path.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().();
51 let extension = path
52 .extensionrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]extension().()
53 .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#::to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_str().)
54 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|extension| extension.to_lowercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]to_lowercase().());
55 match extension.as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().() {
56 #[cfg(feature = "json")]
57 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("json") => {
58 serde_jsonrust-analyzer cargo serde_json 1.0.151 crate/::from_readerrust-analyzer cargo serde_json 1.0.151 de/from_reader().(open_filerust-analyzer cargo config-file 0.2.3 open_file().(path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(ConfigFileErrorrust-analyzer cargo config-file 0.2.3 ConfigFileError#::Jsonrust-analyzer cargo config-file 0.2.3 ConfigFileError#Json#)
59 }
60 #[cfg(feature = "toml")]
61 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("toml") => tomlrust-analyzer cargo toml 0.8.23 crate/::from_strrust-analyzer cargo toml 0.8.23 de/from_str().(
62 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_to_string().(path)
63 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(ConfigFileErrorrust-analyzer cargo config-file 0.2.3 ConfigFileError#::FileAccessrust-analyzer cargo config-file 0.2.3 ConfigFileError#FileAccess#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
64 .as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().(),
65 )
66 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(ConfigFileErrorrust-analyzer cargo config-file 0.2.3 ConfigFileError#::Tomlrust-analyzer cargo config-file 0.2.3 ConfigFileError#Toml#),
67 #[cfg(feature = "xml")]
68 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("xml") => {
69 serde_xml_rsrust-analyzer cargo serde-xml-rs 0.5.1 crate/::from_readerrust-analyzer cargo serde-xml-rs 0.5.1 de/from_reader().(open_filerust-analyzer cargo config-file 0.2.3 open_file().(path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(ConfigFileErrorrust-analyzer cargo config-file 0.2.3 ConfigFileError#::Xmlrust-analyzer cargo config-file 0.2.3 ConfigFileError#Xml#)
70 }
71 #[cfg(feature = "yaml")]
72 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("yaml") | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("yml") => {
73 serde_yamlrust-analyzer cargo serde_yaml 0.9.34+deprecated crate/::from_readerrust-analyzer cargo serde_yaml 0.9.34+deprecated de/from_reader().(open_filerust-analyzer cargo config-file 0.2.3 open_file().(path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(ConfigFileErrorrust-analyzer cargo config-file 0.2.3 ConfigFileError#::Yamlrust-analyzer cargo config-file 0.2.3 ConfigFileError#Yaml#)
74 }
75 _ => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ConfigFileErrorrust-analyzer cargo config-file 0.2.3 ConfigFileError#::UnsupportedFormatrust-analyzer cargo config-file 0.2.3 ConfigFileError#UnsupportedFormat#),
76 }
77 }
78 }
79
80 #[allow(unused)]
81 fn open_filerust-analyzer cargo config-file 0.2.3 open_file().(path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#, ConfigFileErrorrust-analyzer cargo config-file 0.2.3 ConfigFileError#> {
82 Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(path).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(ConfigFileErrorrust-analyzer cargo config-file 0.2.3 ConfigFileError#::FileAccessrust-analyzer cargo config-file 0.2.3 ConfigFileError#FileAccess#)
83 }
84
85 /// This type represents all possible errors that can occur when loading data from a configuration file.
86 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Errorrust-analyzer cargo thiserror-impl 1.0.69 Error:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
87 pub enum ConfigFileErrorrust-analyzer cargo config-file 0.2.3 ConfigFileError# {
88 #[errorrust-analyzer cargo thiserror-impl 1.0.69 error:("couldn't read config file")]
89 /// There was an error while reading the configuration file
90 FileAccessrust-analyzer cargo config-file 0.2.3 ConfigFileError#FileAccess#(#[from] stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#),
91 #[cfg(feature = "json")]
92 #[errorrust-analyzer cargo thiserror-impl 1.0.69 error:("couldn't parse JSON file")]
93 /// There was an error while parsing the JSON data
94 Jsonrust-analyzer cargo config-file 0.2.3 ConfigFileError#Json#(#[from] serde_jsonrust-analyzer cargo serde_json 1.0.151 crate/::Errorrust-analyzer cargo serde_json 1.0.151 error/Error#),
95 #[cfg(feature = "toml")]
96 #[errorrust-analyzer cargo thiserror-impl 1.0.69 error:("couldn't parse TOML file")]
97 /// There was an error while parsing the TOML data
98 Tomlrust-analyzer cargo config-file 0.2.3 ConfigFileError#Toml#(#[from] tomlrust-analyzer cargo toml 0.8.23 crate/::derust-analyzer cargo toml 0.8.23 de/::Errorrust-analyzer cargo toml 0.8.23 de/Error#),
99 #[cfg(feature = "xml")]
100 #[errorrust-analyzer cargo thiserror-impl 1.0.69 error:("couldn't parse XML file")]
101 /// There was an error while parsing the XML data
102 Xmlrust-analyzer cargo config-file 0.2.3 ConfigFileError#Xml#(#[from] serde_xml_rsrust-analyzer cargo serde-xml-rs 0.5.1 crate/::Errorrust-analyzer cargo serde-xml-rs 0.5.1 error/Error#),
103 #[cfg(feature = "yaml")]
104 #[errorrust-analyzer cargo thiserror-impl 1.0.69 error:("couldn't parse YAML file")]
105 /// There was an error while parsing the YAML data
106 Yamlrust-analyzer cargo config-file 0.2.3 ConfigFileError#Yaml#(#[from] serde_yamlrust-analyzer cargo serde_yaml 0.9.34+deprecated crate/::Errorrust-analyzer cargo serde_yaml 0.9.34+deprecated error/Error#),
107 #[errorrust-analyzer cargo thiserror-impl 1.0.69 error:("don't know how to parse file")]
108 /// We don't know how to parse this format according to the file extension
109 UnsupportedFormatrust-analyzer cargo config-file 0.2.3 ConfigFileError#UnsupportedFormat#,
110 }
111
112 #[cfg(test)]
113 mod testrust-analyzer cargo config-file 0.2.3 test/ {
114 use superrust-analyzer cargo config-file 0.2.3 crate/::*;
115
116 use serderust-analyzer cargo serde 1.0.229 crate/::Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#;
117
118 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Deserializerust-analyzer cargo serde_derive 1.0.229 Deserialize:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
119 struct TestConfigrust-analyzer cargo config-file 0.2.3 test/TestConfig# {
120 hostrust-analyzer cargo config-file 0.2.3 test/TestConfig#host.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
121 portrust-analyzer cargo config-file 0.2.3 test/TestConfig#port.: u64,
122 tagsrust-analyzer cargo config-file 0.2.3 test/TestConfig#tags.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
123 innerrust-analyzer cargo config-file 0.2.3 test/TestConfig#inner.: TestConfigInnerrust-analyzer cargo config-file 0.2.3 test/TestConfigInner#,
124 }
125
126 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Deserializerust-analyzer cargo serde_derive 1.0.229 Deserialize:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
127 struct TestConfigInnerrust-analyzer cargo config-file 0.2.3 test/TestConfigInner# {
128 answerrust-analyzer cargo config-file 0.2.3 test/TestConfigInner#answer.: u8,
129 }
130
131 impl TestConfigrust-analyzer cargo config-file 0.2.3 test/TestConfig# {
132 #[allow(unused)]
133 fn examplerust-analyzer cargo config-file 0.2.3 test/impl#[TestConfig]example().() -> Selfrust-analyzer cargo config-file 0.2.3 test/impl#[TestConfig] {
134 Selfrust-analyzer cargo config-file 0.2.3 test/impl#[TestConfig] {
135 hostrust-analyzer cargo config-file 0.2.3 test/TestConfig#host.: "example.com".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
136 portrust-analyzer cargo config-file 0.2.3 test/TestConfig#port.: 443,
137 tagsrust-analyzer cargo config-file 0.2.3 test/TestConfig#tags.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["example".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), "test".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()],
138 innerrust-analyzer cargo config-file 0.2.3 test/TestConfig#inner.: TestConfigInnerrust-analyzer cargo config-file 0.2.3 test/TestConfigInner# { answerrust-analyzer cargo config-file 0.2.3 test/TestConfigInner#answer.: 42 },
139 }
140 }
141 }
142
143 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
144 fn test_unknownrust-analyzer cargo config-file 0.2.3 test/test_unknown().() {
145 let config = TestConfigrust-analyzer cargo config-file 0.2.3 test/TestConfig#::from_config_filerust-analyzer cargo config-file 0.2.3 impl#[C][FromConfigFile]from_config_file().("/tmp/foobar");
146 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(config, Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ConfigFileErrorrust-analyzer cargo config-file 0.2.3 ConfigFileError#::UnsupportedFormatrust-analyzer cargo config-file 0.2.3 ConfigFileError#UnsupportedFormat#)));
147 }
148
149 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
150 #[cfg(feature = "toml")]
151 fn test_file_not_foundrust-analyzer cargo config-file 0.2.3 test/test_file_not_found().() {
152 let config = TestConfigrust-analyzer cargo config-file 0.2.3 test/TestConfig#::from_config_filerust-analyzer cargo config-file 0.2.3 impl#[C][FromConfigFile]from_config_file().("/tmp/foobar.toml");
153 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(config, Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ConfigFileErrorrust-analyzer cargo config-file 0.2.3 ConfigFileError#::FileAccessrust-analyzer cargo config-file 0.2.3 ConfigFileError#FileAccess#(_))));
154 }
155
156 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
157 #[cfg(feature = "json")]
158 fn test_jsonrust-analyzer cargo config-file 0.2.3 test/test_json().() {
159 let config = TestConfigrust-analyzer cargo config-file 0.2.3 test/TestConfig#::from_config_filerust-analyzer cargo config-file 0.2.3 impl#[C][FromConfigFile]from_config_file().("testdata/config.json");
160 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(config.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), TestConfigrust-analyzer cargo config-file 0.2.3 test/TestConfig#::examplerust-analyzer cargo config-file 0.2.3 test/impl#[TestConfig]example().());
161 }
162
163 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
164 #[cfg(feature = "toml")]
165 fn test_tomlrust-analyzer cargo config-file 0.2.3 test/test_toml().() {
166 let config = TestConfigrust-analyzer cargo config-file 0.2.3 test/TestConfig#::from_config_filerust-analyzer cargo config-file 0.2.3 impl#[C][FromConfigFile]from_config_file().("testdata/config.toml");
167 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(config.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), TestConfigrust-analyzer cargo config-file 0.2.3 test/TestConfig#::examplerust-analyzer cargo config-file 0.2.3 test/impl#[TestConfig]example().());
168 }
169
170 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
171 #[cfg(feature = "xml")]
172 fn test_xmlrust-analyzer cargo config-file 0.2.3 test/test_xml().() {
173 let config = TestConfigrust-analyzer cargo config-file 0.2.3 test/TestConfig#::from_config_filerust-analyzer cargo config-file 0.2.3 impl#[C][FromConfigFile]from_config_file().("testdata/config.xml");
174 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(config.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), TestConfigrust-analyzer cargo config-file 0.2.3 test/TestConfig#::examplerust-analyzer cargo config-file 0.2.3 test/impl#[TestConfig]example().());
175 }
176
177 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
178 #[cfg(feature = "yaml")]
179 fn test_yamlrust-analyzer cargo config-file 0.2.3 test/test_yaml().() {
180 let config = TestConfigrust-analyzer cargo config-file 0.2.3 test/TestConfig#::from_config_filerust-analyzer cargo config-file 0.2.3 impl#[C][FromConfigFile]from_config_file().("testdata/config.yml");
181 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(config.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), TestConfigrust-analyzer cargo config-file 0.2.3 test/TestConfig#::examplerust-analyzer cargo config-file 0.2.3 test/impl#[TestConfig]example().());
182 }
183 }