Skip to content

Commit c0f64fd

Browse files
committed
Clean up, update deps, fix Linux functionality outside cargo project
1 parent 81163e0 commit c0f64fd

File tree

2 files changed

+9
-44
lines changed

2 files changed

+9
-44
lines changed

Cargo.toml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@ documentation = "https://docs.rs/confy"
88
repository = "https://github.com/rust-clique/confy"
99

1010
[dependencies]
11-
serde = "1.0"
12-
toml = "0.4"
13-
directories = "0.10"
14-
serde_derive = { version = "1.0", optional = true }
15-
cargo_metadata = "0.8"
16-
17-
[features]
18-
sd = ["serde_derive"]
11+
serde = "^1.0"
12+
toml = "^0.5"
13+
directories = "^2.0"
1914

2015
[[example]]
2116
name = "simple"
22-
required-features = ["sd"]
17+
18+
[dev-dependencies]
19+
serde_derive = "^1.0"

src/lib.rs

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
//! [`store`]: fn.store.html
5959
//!
6060
61-
extern crate cargo_metadata;
6261
extern crate directories;
6362
extern crate serde;
6463
extern crate toml;
@@ -84,9 +83,6 @@ pub enum ConfyError {
8483
WriteConfigurationFileError(std::io::Error),
8584
ReadConfigurationFileError(std::io::Error),
8685
OpenConfigurationFileError(std::io::Error),
87-
CargoMetadataExecError(cargo_metadata::Error),
88-
CargoMetadataResolveError,
89-
CargoMetadataRootError,
9086
}
9187

9288
impl fmt::Display for ConfyError {
@@ -100,9 +96,6 @@ impl fmt::Display for ConfyError {
10096
ConfyError::WriteConfigurationFileError(_) => write!(f, "Failed to write configuration file."),
10197
ConfyError::ReadConfigurationFileError(_) => write!(f, "Failed to read configuration file."),
10298
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."),
10699
}
107100
}
108101
}
@@ -131,9 +124,7 @@ impl Error for ConfyError {}
131124
/// let cfg: MyConfig = confy::load("my-app")?;
132125
/// ```
133126
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)?;
137128

138129
let config_dir_str = get_configuration_directory_str(&project)?;
139130

@@ -171,17 +162,15 @@ pub fn load<T: Serialize + DeserializeOwned + Default>(name: &str) -> Result<T,
171162
/// ```rust,no_run
172163
/// # struct MyConf {}
173164
/// let my_cfg = MyConf {};
174-
/// confy::store(my_cfg)?;
165+
/// confy::store("my-app", my_cfg)?;
175166
/// ```
176167
///
177168
/// Errors returned are I/O errors related to not being
178169
/// able to write the configuration file or if `confy`
179170
/// encounters an operating system or environment it does
180171
/// not support.
181172
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)?;
185174
fs::create_dir_all(project.config_dir()).map_err(ConfyError::DirectoryCreationFailed)?;
186175

187176
let config_dir_str = get_configuration_directory_str(&project)?;
@@ -208,24 +197,3 @@ fn get_configuration_directory_str(project: &ProjectDirs) -> Result<&str, ConfyE
208197
None => Err(ConfyError::BadConfigDirectoryStr),
209198
}
210199
}
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

Comments
 (0)