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 10 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
1 change: 1 addition & 0 deletions .external-types.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
# The following are external types that are allowed to be exposed in our public API.
allowed_external_types = [
"serde::*",
"url::Url",
]
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
4 changes: 4 additions & 0 deletions bench/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ 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_TERM_QUIET", "false"),
("CARGO_TERM_VERBOSE", "false"),
("CARGO_TERM_COLOR", "auto"),
Expand Down
97 changes: 95 additions & 2 deletions 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,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,88 @@ 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.
///
/// 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>>,
}

/// 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(s: &str) -> Result<Self, Self::Err> {
match s {
"git" => Ok(RegistriesProtocol::Git),
"sparse" => Ok(RegistriesProtocol::Sparse),
_ => bail!("CARGO_REGISTRIES_CRATES_IO_PROTOCOL environment variable must be `git` or `sparse`"),
}
}
}

/// The `[registry]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#registry)
#[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 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>>,
}

/// The `[term]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#term)
Expand Down
112 changes: 110 additions & 2 deletions 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,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,96 @@ 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.
///
/// 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 }
}
}

/// 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(Debug, Clone, Default, Serialize)]
yottalogical marked this conversation as resolved.
Show resolved Hide resolved
#[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 }
}
}

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