58
58
//! [`store`]: fn.store.html
59
59
//!
60
60
61
- extern crate cargo_metadata;
62
61
extern crate directories;
63
62
extern crate serde;
64
63
extern crate toml;
@@ -84,9 +83,6 @@ pub enum ConfyError {
84
83
WriteConfigurationFileError ( std:: io:: Error ) ,
85
84
ReadConfigurationFileError ( std:: io:: Error ) ,
86
85
OpenConfigurationFileError ( std:: io:: Error ) ,
87
- CargoMetadataExecError ( cargo_metadata:: Error ) ,
88
- CargoMetadataResolveError ,
89
- CargoMetadataRootError ,
90
86
}
91
87
92
88
impl fmt:: Display for ConfyError {
@@ -100,9 +96,6 @@ impl fmt::Display for ConfyError {
100
96
ConfyError :: WriteConfigurationFileError ( _) => write ! ( f, "Failed to write configuration file." ) ,
101
97
ConfyError :: ReadConfigurationFileError ( _) => write ! ( f, "Failed to read configuration file." ) ,
102
98
ConfyError :: OpenConfigurationFileError ( _) => write ! ( f, "Failed to open configuration file." ) ,
103
- ConfyError :: CargoMetadataExecError ( _) => write ! ( f, "Failed to get cargo metadata." ) ,
104
- ConfyError :: CargoMetadataResolveError => write ! ( f, "Failed to get crate's dependency graph." ) ,
105
- ConfyError :: CargoMetadataRootError => write ! ( f, "Failed to get crate's root dependency." ) ,
106
99
}
107
100
}
108
101
}
@@ -131,9 +124,7 @@ impl Error for ConfyError {}
131
124
/// let cfg: MyConfig = confy::load("my-app")?;
132
125
/// ```
133
126
pub fn load < T : Serialize + DeserializeOwned + Default > ( name : & str ) -> Result < T , ConfyError > {
134
- let root_name = get_root_name ( ) ?;
135
-
136
- let project = ProjectDirs :: from ( "rs" , & root_name, name) ;
127
+ let project = ProjectDirs :: from ( "rs" , "" , name) . ok_or ( ConfyError :: BadConfigDirectoryStr ) ?;
137
128
138
129
let config_dir_str = get_configuration_directory_str ( & project) ?;
139
130
@@ -171,17 +162,15 @@ pub fn load<T: Serialize + DeserializeOwned + Default>(name: &str) -> Result<T,
171
162
/// ```rust,no_run
172
163
/// # struct MyConf {}
173
164
/// let my_cfg = MyConf {};
174
- /// confy::store(my_cfg)?;
165
+ /// confy::store("my-app", my_cfg)?;
175
166
/// ```
176
167
///
177
168
/// Errors returned are I/O errors related to not being
178
169
/// able to write the configuration file or if `confy`
179
170
/// encounters an operating system or environment it does
180
171
/// not support.
181
172
pub fn store < T : Serialize > ( name : & str , cfg : T ) -> Result < ( ) , ConfyError > {
182
- let root_name = get_root_name ( ) ?;
183
-
184
- let project = ProjectDirs :: from ( "rs" , & root_name, name) ;
173
+ let project = ProjectDirs :: from ( "rs" , "" , name) . ok_or ( ConfyError :: BadConfigDirectoryStr ) ?;
185
174
fs:: create_dir_all ( project. config_dir ( ) ) . map_err ( ConfyError :: DirectoryCreationFailed ) ?;
186
175
187
176
let config_dir_str = get_configuration_directory_str ( & project) ?;
@@ -208,24 +197,3 @@ fn get_configuration_directory_str(project: &ProjectDirs) -> Result<&str, ConfyE
208
197
None => Err ( ConfyError :: BadConfigDirectoryStr ) ,
209
198
}
210
199
}
211
-
212
- fn get_root_name ( ) -> Result < String , ConfyError > {
213
- let mut cmd = cargo_metadata:: MetadataCommand :: new ( ) ;
214
- let dep_graph = cmd. exec ( ) . map_err ( ConfyError :: CargoMetadataExecError ) ?;
215
-
216
- let package = match dep_graph. resolve {
217
- Some ( p) => Ok ( p) ,
218
- None => Err ( ConfyError :: CargoMetadataResolveError ) ,
219
- } ?;
220
-
221
- let package_root = match package. root {
222
- Some ( r) => Ok ( r) ,
223
- None => Err ( ConfyError :: CargoMetadataRootError ) ,
224
- } ?;
225
- //Package root will look like:
226
- //PackageId { repr: "conf_test 0.1.0 (path+file:///Users/code/conf_test)" }
227
-
228
- let root_name_string = package_root. repr . split ( ' ' ) . collect :: < Vec < & str > > ( ) ;
229
-
230
- Ok ( root_name_string[ 0 ] . to_string ( ) )
231
- }
0 commit comments