Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the [registries] and [registry] tables #8

Merged
merged 16 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ members = ["bench", "tests/helper/build-info", "tools/codegen"]
doc-scrape-examples = false

# Note: serde is public dependencies.
# Note: url is public dependencies.
[dependencies]
cfg-expr = "0.14"
home = "0.5"
once_cell = { version = "1", default-features = false }
serde = { version = "1.0.103", features = ["derive"] }
shell-escape = "0.1.5"
toml = { version = "0.7", default-features = false, features = ["parse"] }
url = { version = "2", features = ["serde"] }
yottalogical marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
anyhow = "1"
Expand Down
2 changes: 2 additions & 0 deletions bench/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ fn reference(c: &mut Criterion) {
("CARGO_NET_RETRY", "1"),
("CARGO_NET_GIT_FETCH_WITH_CLI", "false"),
("CARGO_NET_OFFLINE", "false"),
("CARGO_REGISTRIES_crates.io_INDEX", "https://github.com/rust-lang/crates.io-index"),
("CARGO_REGISTRIES_crates.io_TOKEN", "00000000000000000000000000000000000"),
("CARGO_TERM_QUIET", "false"),
("CARGO_TERM_VERBOSE", "false"),
("CARGO_TERM_COLOR", "auto"),
Expand Down
27 changes: 26 additions & 1 deletion src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::{
};

use serde::{Deserialize, Serialize};
use url::Url;

pub use crate::value::{Definition, Value};
use crate::{
Expand Down Expand Up @@ -71,7 +72,12 @@ pub struct Config {
pub net: NetConfig,
// TODO: patch
// TODO: profile
// TODO: registries
/// The `[registries]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registries)
#[serde(default)]
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub registries: BTreeMap<String, RegistriesConfigValue>,
// TODO: registry
// TODO: source
/// The `[target]` table.
Expand Down Expand Up @@ -441,6 +447,25 @@ pub struct NetConfig {
pub offline: Option<Value<bool>>,
}

/// A value of the `[registries]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registries)
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
yottalogical marked this conversation as resolved.
Show resolved Hide resolved
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct RegistriesConfigValue {
/// Specifies the URL of the git index for the registry.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registriesnameindex)
#[serde(skip_serializing_if = "Option::is_none")]
pub index: Option<Value<Url>>,
/// Specifies the authentication token for the given registry.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registriesnametoken)
#[serde(skip_serializing_if = "Option::is_none")]
pub token: Option<Value<String>>,
}

/// The `[term]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#term)
Expand Down
40 changes: 39 additions & 1 deletion src/easy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
};

use serde::Serialize;
use url::Url;

use crate::{
de::{self, split_encoded, split_space_separated, Color, Frequency, When},
Expand Down Expand Up @@ -65,7 +66,12 @@ pub struct Config {
pub net: NetConfig,
// TODO: patch
// TODO: profile
// TODO: registries
/// The `[registries]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registries)
#[serde(default)]
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub registries: BTreeMap<String, RegistriesConfigValue>,
// TODO: registry
// TODO: source
/// The resolved `[target]` table.
Expand Down Expand Up @@ -137,6 +143,10 @@ impl Config {
let future_incompat_report =
FutureIncompatReportConfig::from_unresolved(de.future_incompat_report);
let net = NetConfig::from_unresolved(de.net);
let mut registries = BTreeMap::new();
for (k, v) in de.registries {
registries.insert(k, RegistriesConfigValue::from_unresolved(v));
}
let term = TermConfig::from_unresolved(de.term);

Ok(Self {
Expand All @@ -146,6 +156,7 @@ impl Config {
env,
future_incompat_report,
net,
registries,
target: RefCell::new(BTreeMap::new()),
de_target: de.target,
term,
Expand Down Expand Up @@ -701,6 +712,33 @@ impl NetConfig {
}
}

/// A value of the `[registries]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registries)
#[derive(Debug, Clone, Default, Serialize)]
yottalogical marked this conversation as resolved.
Show resolved Hide resolved
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct RegistriesConfigValue {
/// Specifies the URL of the git index for the registry.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registriesnameindex)
#[serde(skip_serializing_if = "Option::is_none")]
pub index: Option<Url>,
/// Specifies the authentication token for the given registry.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registriesnametoken)
#[serde(skip_serializing_if = "Option::is_none")]
pub token: Option<String>,
}

impl RegistriesConfigValue {
pub(crate) fn from_unresolved(de: de::RegistriesConfigValue) -> Self {
let index = de.index.map(|v| v.val);
let token = de.token.map(|v| v.val);
Self { index, token }
}
}

/// The `[term]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#term)
Expand Down
41 changes: 39 additions & 2 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::{
de::{
BuildConfig, Config, DocConfig, Flags, FutureIncompatReportConfig, NetConfig, PathAndArgs,
StringList, StringOrArray, TermConfig, TermProgress,
RegistriesConfigValue, StringList, StringOrArray, TermConfig, TermProgress,
},
error::{Context as _, Error, Result},
resolve::ResolveContext,
Expand All @@ -26,8 +26,8 @@ impl Config {
/// (e.g., In environment variables, `-` and `.` in the target triple are replaced by `_`)
#[doc(hidden)] // Not public API.
pub fn apply_env(&mut self, cx: &ResolveContext) -> Result<()> {
// https://doc.rust-lang.org/nightly/cargo/reference/config.html#alias
for (k, v) in &cx.env {
// https://doc.rust-lang.org/nightly/cargo/reference/config.html#alias
if let Some(k) = k.strip_prefix("CARGO_ALIAS_") {
self.alias.insert(
k.to_owned(),
Expand All @@ -38,6 +38,43 @@ impl Config {
);
continue;
}

// https://doc.rust-lang.org/nightly/cargo/reference/config.html#registries
if let Some(k) = k.strip_prefix("CARGO_REGISTRIES_") {
if let Some(k) = k.strip_suffix("_INDEX") {
let v = v.to_str().ok_or_else(|| Error::env_not_unicode(k, v.clone()))?;
let index = Some(
Value {
val: v.to_owned(),
definition: Some(Definition::Environment(k.to_owned().into())),
taiki-e marked this conversation as resolved.
Show resolved Hide resolved
}
.parse()
.with_context(|| format!("failed to parse URL `{v}`"))?,
);
if let Some(registries_config_value) = self.registries.get_mut(k) {
registries_config_value.index = index;
} else {
self.registries
.insert(k.to_owned(), RegistriesConfigValue { index, token: None });
}
continue;
}

if let Some(k) = k.strip_suffix("_TOKEN") {
let v = v.to_str().ok_or_else(|| Error::env_not_unicode(k, v.clone()))?;
let token = Some(Value {
val: v.to_owned(),
definition: Some(Definition::Environment(k.to_owned().into())),
taiki-e marked this conversation as resolved.
Show resolved Hide resolved
});
if let Some(registries_config_value) = self.registries.get_mut(k) {
registries_config_value.token = token;
} else {
self.registries
.insert(k.to_owned(), RegistriesConfigValue { index: None, token });
}
continue;
}
}
}

// For self.target, we handle it in Config::resolve.
Expand Down
2 changes: 2 additions & 0 deletions src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,8 @@ mod tests {
("CARGO_NET_RETRY", "1"),
("CARGO_NET_GIT_FETCH_WITH_CLI", "false"),
("CARGO_NET_OFFLINE", "false"),
("CARGO_REGISTRIES_crates.io_INDEX", "https://github.com/rust-lang/crates.io-index"),
("CARGO_REGISTRIES_crates.io_TOKEN", "00000000000000000000000000000000000"),
("CARGO_TERM_QUIET", "false"),
("CARGO_TERM_VERBOSE", "false"),
("CARGO_TERM_COLOR", "auto"),
Expand Down