refactor: Move parsing and warning code into two new functions

master
Jesper Håkansson 7 years ago
parent 59039e4118
commit 2ed3d01541
  1. 76
      src/manifest/mod.rs

@ -26,8 +26,9 @@ pub struct CrateData {
manifest: CargoManifest,
}
#[doc(hidden)]
#[derive(Deserialize)]
struct CargoManifest {
pub struct CargoManifest {
package: CargoPackage,
}
@ -197,6 +198,12 @@ struct NpmData {
main: String,
}
#[doc(hidden)]
pub struct ManifestAndUnsedKeys {
pub manifest: CargoManifest,
pub unused_keys: BTreeSet<String>,
}
impl CrateData {
/// Reads all metadata for the crate whose manifest is inside the directory
/// specified by `path`.
@ -209,31 +216,16 @@ impl CrateData {
crate_path.display()
)
}
let manifest = fs::read_to_string(&manifest_path)
.with_context(|_| format!("failed to read: {}", manifest_path.display()))?;
let manifest = &mut toml::Deserializer::new(&manifest);
let mut unused_keys = BTreeSet::new();
let manifest: CargoManifest = serde_ignored::deserialize(manifest, |path| {
let path_string = path.to_string();
if path_string.contains("metadata") {
unused_keys.insert(path_string);
}
})
.with_context(|_| format!("failed to parse manifest: {}", manifest_path.display()))?;
unused_keys.iter().for_each(|path| {
PBAR.warn(&format!(
"\"{}\" is misspelled and will be ignored. Please check your Cargo.toml.",
path
));
});
let data =
cargo_metadata::metadata(Some(&manifest_path)).map_err(error_chain_to_failure)?;
let ManifestAndUnsedKeys {
manifest,
unused_keys,
} = CrateData::parse_crate_data(&manifest_path)?;
CrateData::warn_for_unused_keys(&unused_keys);
let current_idx = data
.packages
.iter()
@ -259,6 +251,46 @@ impl CrateData {
}
}
/// Read the `manifest_path` file and deserializes it using the toml Deserializer.
/// Returns a Result containing `ManifestAndUnsedKeys` which contains `CargoManifest`
/// and a `BTreeSet<String>` containing the unused keys from the parsed file.
///
/// # Errors
/// Will return Err if the file (manifest_path) couldn't be read or
/// if deserializion of to `CargoManifest` fails.
pub fn parse_crate_data(manifest_path: &Path) -> Result<ManifestAndUnsedKeys, Error> {
let manifest = fs::read_to_string(&manifest_path)
.with_context(|_| format!("failed to read: {}", manifest_path.display()))?;
let manifest = &mut toml::Deserializer::new(&manifest);
let mut unused_keys = BTreeSet::new();
let manifest: CargoManifest = serde_ignored::deserialize(manifest, |path| {
let path_string = path.to_string();
if path_string.contains("metadata") {
unused_keys.insert(path_string);
}
})
.with_context(|_| format!("failed to parse manifest: {}", manifest_path.display()))?;
Ok(ManifestAndUnsedKeys {
manifest,
unused_keys,
})
}
/// Iterating through all the passed `unused_keys` and output
/// a warning for each unknown key.
pub fn warn_for_unused_keys(unused_keys: &BTreeSet<String>) {
unused_keys.iter().for_each(|path| {
PBAR.warn(&format!(
"\"{}\" is a unknown key and will be ignored. Please check your Cargo.toml.",
path
));
});
}
/// Get the configured profile.
pub fn configured_profile(&self, profile: BuildProfile) -> &CargoWasmPackProfile {
match profile {

Loading…
Cancel
Save