Skip to content

Commit

Permalink
Use deserialize_with instead of specializing doc_extern_map
Browse files Browse the repository at this point in the history
  • Loading branch information
jyn514 committed Nov 30, 2020
1 parent d85fd93 commit 8a48c87
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
25 changes: 24 additions & 1 deletion src/cargo/core/compiler/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,36 @@ impl<'de> serde::de::Deserialize<'de> for RustdocExternMode {
}
}

#[derive(serde::Deserialize, Debug, Default)]
#[derive(serde::Deserialize, Debug)]
#[serde(default)]
pub struct RustdocExternMap {
#[serde(deserialize_with = "default_crates_io_to_docs_rs")]
pub(crate) registries: HashMap<String, String>,
std: Option<RustdocExternMode>,
}

impl Default for RustdocExternMap {
fn default() -> Self {
let mut registries = HashMap::new();
registries.insert("crates-io".into(), "https://docs.rs/".into());
Self {
registries,
std: None,
}
}
}

fn default_crates_io_to_docs_rs<'de, D: serde::Deserializer<'de>>(
de: D,
) -> Result<HashMap<String, String>, D::Error> {
use serde::Deserialize;
let mut registries = HashMap::deserialize(de)?;
if !registries.contains_key("crates-io") {
registries.insert("crates-io".into(), "https://docs.rs/".into());
}
Ok(registries)
}

impl hash::Hash for RustdocExternMap {
fn hash<H: hash::Hasher>(&self, into: &mut H) {
self.std.hash(into);
Expand Down
11 changes: 2 additions & 9 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,15 +1217,8 @@ impl Config {
// Note: This does not support environment variables. The `Unit`
// fundamentally does not have access to the registry name, so there is
// nothing to query. Plumbing the name into SourceId is quite challenging.
self.doc_extern_map.try_borrow_with(|| {
let mut extern_map = self.get::<RustdocExternMap>("doc.extern-map");
if let Ok(map) = &mut extern_map {
map.registries
.entry("crates-io".into())
.or_insert("https://docs.rs/".into());
}
extern_map
})
self.doc_extern_map
.try_borrow_with(|| self.get::<RustdocExternMap>("doc.extern-map"))
}

/// Returns the `[target]` table definition for the given target triple.
Expand Down

0 comments on commit 8a48c87

Please sign in to comment.