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 all commits
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
5 changes: 5 additions & 0 deletions bench/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ 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_REGISTRY_DEFAULT", "crates-io"),
("CARGO_REGISTRY_TOKEN", "00000000000000000000000000000000000"),
("CARGO_REGISTRIES_CRATES_IO_PROTOCOL", "git"),
("CARGO_TERM_QUIET", "false"),
("CARGO_TERM_VERBOSE", "false"),
("CARGO_TERM_COLOR", "auto"),
Expand Down
124 changes: 122 additions & 2 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
borrow::Cow,
collections::BTreeMap,
ffi::OsStr,
fmt::{self, Formatter},
fs,
path::{Path, PathBuf},
slice,
Expand Down Expand Up @@ -71,8 +72,18 @@ pub struct Config {
pub net: NetConfig,
// TODO: patch
// TODO: profile
// TODO: registries
// TODO: registry
/// 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>,
/// The `[registry]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registry)
#[serde(default)]
#[serde(skip_serializing_if = "RegistryConfig::is_none")]
pub registry: RegistryConfig,
// TODO: source
/// The `[target]` table.
///
Expand Down Expand Up @@ -441,6 +452,115 @@ 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(Clone, Default, Serialize, Deserialize)]
#[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<String>>,
/// Specifies the authentication token for the given registry.
///
/// Note: This library does not read any values in the
/// [credentials](https://doc.rust-lang.org/nightly/cargo/reference/config.html#credentials)
/// file.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registriesnametoken)
#[serde(skip_serializing_if = "Option::is_none")]
pub token: Option<Value<String>>,
/// Specifies the protocol used to access crates.io.
/// Not allowed for any registries besides crates.io.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registriescrates-ioprotocol)
#[serde(skip_serializing_if = "Option::is_none")]
pub protocol: Option<Value<RegistriesProtocol>>,
}

impl fmt::Debug for RegistriesConfigValue {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let Self { index, token, protocol } = self;
let redacted_token = token
.as_ref()
.map(|token| Value { val: "[REDACTED]", definition: token.definition.clone() });
f.debug_struct("RegistriesConfigValue")
.field("index", &index)
.field("token", &redacted_token)
.field("protocol", &protocol)
.finish()
}
}

/// Specifies the protocol used to access crates.io.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registriescrates-ioprotocol)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub enum RegistriesProtocol {
/// Causes Cargo to clone the entire index of all packages ever published to
/// [crates.io](https://crates.io/) from <https://github.com/rust-lang/crates.io-index/>.
Git,
/// A newer protocol which uses HTTPS to download only what is necessary from
/// <https://index.crates.io/>.
Sparse,
}

impl FromStr for RegistriesProtocol {
type Err = Error;

fn from_str(protocol: &str) -> Result<Self, Self::Err> {
match protocol {
"git" => Ok(Self::Git),
"sparse" => Ok(Self::Sparse),
other => bail!("must be git or sparse, but found `{other}`"),
}
}
}

/// The `[registry]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registry)
#[derive(Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct RegistryConfig {
/// The name of the registry (from the
/// [`registries` table](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registries))
/// to use by default for registry commands like
/// [`cargo publish`](https://doc.rust-lang.org/nightly/cargo/commands/cargo-publish.html).
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registrydefault)
#[serde(skip_serializing_if = "Option::is_none")]
pub default: Option<Value<String>>,
/// Specifies the authentication token for [crates.io](https://crates.io/).
///
/// Note: This library does not read any values in the
/// [credentials](https://doc.rust-lang.org/nightly/cargo/reference/config.html#credentials)
/// file.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registrytoken)
#[serde(skip_serializing_if = "Option::is_none")]
pub token: Option<Value<String>>,
}

impl fmt::Debug for RegistryConfig {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let Self { default, token } = self;
let redacted_token = token
.as_ref()
.map(|token| Value { val: "[REDACTED]", definition: token.definition.clone() });
f.debug_struct("RegistryConfig")
.field("default", &default)
.field("token", &redacted_token)
.finish()
}
}

/// The `[term]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#term)
Expand Down
135 changes: 133 additions & 2 deletions src/easy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
cell::RefCell,
collections::BTreeMap,
ffi::{OsStr, OsString},
fmt::{self, Formatter},
ops,
path::{Path, PathBuf},
process::Command,
Expand Down Expand Up @@ -65,8 +66,18 @@ pub struct Config {
pub net: NetConfig,
// TODO: patch
// TODO: profile
// TODO: registries
// TODO: registry
/// 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>,
/// The `[registry]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registry)
#[serde(default)]
#[serde(skip_serializing_if = "RegistryConfig::is_none")]
pub registry: RegistryConfig,
// TODO: source
/// The resolved `[target]` table.
#[serde(skip_deserializing)]
Expand Down Expand Up @@ -137,6 +148,11 @@ 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 registry = RegistryConfig::from_unresolved(de.registry);
let term = TermConfig::from_unresolved(de.term);

Ok(Self {
Expand All @@ -146,6 +162,8 @@ impl Config {
env,
future_incompat_report,
net,
registries,
registry,
target: RefCell::new(BTreeMap::new()),
de_target: de.target,
term,
Expand Down Expand Up @@ -701,6 +719,119 @@ impl NetConfig {
}
}

/// A value of the `[registries]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registries)
#[derive(Clone, Default, Serialize)]
#[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<String>,
/// Specifies the authentication token for the given registry.
///
/// Note: This library does not read any values in the
/// [credentials](https://doc.rust-lang.org/nightly/cargo/reference/config.html#credentials)
/// file.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registriesnametoken)
#[serde(skip_serializing_if = "Option::is_none")]
pub token: Option<String>,
/// Specifies the protocol used to access crates.io.
/// Not allowed for any registries besides crates.io.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registriescrates-ioprotocol)
#[serde(skip_serializing_if = "Option::is_none")]
pub protocol: Option<RegistriesProtocol>,
}

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);
let protocol = de.protocol.map(|v| match v.val {
de::RegistriesProtocol::Git => RegistriesProtocol::Git,
de::RegistriesProtocol::Sparse => RegistriesProtocol::Sparse,
});
Self { index, token, protocol }
}
}

impl fmt::Debug for RegistriesConfigValue {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let Self { index, token, protocol } = self;
let redacted_token = token.as_ref().map(|_| "[REDACTED]");
f.debug_struct("RegistriesConfigValue")
.field("index", &index)
.field("token", &redacted_token)
.field("protocol", &protocol)
.finish_non_exhaustive()
}
}

/// Specifies the protocol used to access crates.io.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registriescrates-ioprotocol)
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub enum RegistriesProtocol {
/// Causes Cargo to clone the entire index of all packages ever published to
/// [crates.io](https://crates.io/) from <https://github.com/rust-lang/crates.io-index/>.
Git,
/// A newer protocol which uses HTTPS to download only what is necessary from
/// <https://index.crates.io/>.
Sparse,
}

/// The `[registry]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registry)
#[derive(Clone, Default, Serialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct RegistryConfig {
/// The name of the registry (from the
/// [`registries` table](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registries))
/// to use by default for registry commands like
/// [`cargo publish`](https://doc.rust-lang.org/nightly/cargo/commands/cargo-publish.html).
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registrydefault)
#[serde(skip_serializing_if = "Option::is_none")]
pub default: Option<String>,
/// Specifies the authentication token for [crates.io](https://crates.io/).
///
/// Note: This library does not read any values in the
/// [credentials](https://doc.rust-lang.org/nightly/cargo/reference/config.html#credentials)
/// file.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registrytoken)
#[serde(skip_serializing_if = "Option::is_none")]
pub token: Option<String>,
}

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

impl fmt::Debug for RegistryConfig {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let Self { default, token } = self;
let redacted_token = token.as_ref().map(|_| "[REDACTED]");
f.debug_struct("RegistryConfig")
.field("default", &default)
.field("token", &redacted_token)
.finish()
}
}

/// The `[term]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#term)
Expand Down
Loading