Skip to content
Merged
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
133 changes: 104 additions & 29 deletions src/backend/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,93 @@ use std::{fmt::Debug, sync::Arc};
use async_trait::async_trait;
use color_eyre::Section;
use eyre::{bail, eyre};
use indexmap::IndexMap;
use url::Url;

use crate::Result;
use crate::backend::Backend;
use crate::backend::VersionInfo;
use crate::backend::backend_type::BackendType;
use crate::backend::options::BackendOptions;
use crate::backend::platform_target::PlatformTarget;
use crate::backend::static_helpers::lookup_platform_key;
use crate::cli::args::BackendArg;
use crate::cmd::CmdLineRunner;
use crate::config::{Config, Settings};
use crate::env::GITHUB_TOKEN;
use crate::file;
use crate::http::HTTP_FETCH;
use crate::install_context::InstallContext;
use crate::toolset::{ToolRequest, ToolVersion};
use crate::toolset::{ToolRequest, ToolVersion, ToolVersionOptions};

#[derive(Debug)]
pub struct CargoBackend {
ba: Arc<BackendArg>,
}

#[derive(Debug, Clone, Copy)]
struct CargoOptions<'a> {
values: BackendOptions<'a>,
}

impl<'a> CargoOptions<'a> {
fn new(raw: &'a ToolVersionOptions) -> Self {
Self {
values: BackendOptions::new(raw),
}
}

fn bin(&self) -> Option<String> {
self.values.platform_string("bin")
}

fn bin_for_target(&self, target: &PlatformTarget) -> Option<String> {
self.values.platform_string_for_target("bin", target)
}

fn locked(&self) -> bool {
self.values
.raw()
.get("locked")
.is_none_or(|v| v.to_lowercase() != "false")
}

fn features(&self) -> Option<&'a str> {
self.values.str("features")
}

fn default_features_disabled(&self) -> bool {
self.values
.str("default-features")
.is_some_and(|v| v.to_lowercase() == "false")
}

fn crate_arg(&self) -> Option<&'a str> {
self.values.str("crate")
}

fn install_env(&self) -> &'a IndexMap<String, String> {
&self.values.raw().install_env
}

fn has_features_options(&self) -> bool {
self.values.raw().contains_key("features")
|| self.values.raw().contains_key("default-features")
}

fn lockfile_options(&self, target: &PlatformTarget) -> BTreeMap<String, String> {
let mut result = BTreeMap::new();
for key in ["features", "default-features"] {
if let Some(value) = self.values.str(key) {
result.insert(key.to_string(), value.to_string());
}
}
if let Some(bin) = self.bin_for_target(target) {
result.insert("bin".to_string(), bin);
}
result
}
}

#[async_trait]
impl Backend for CargoBackend {
fn get_type(&self) -> BackendType {
Expand Down Expand Up @@ -130,27 +195,21 @@ impl Backend for CargoBackend {
cmd.arg(install_arg)
};

let opts = tv.request.options();
if let Some(bin) =
lookup_platform_key(&opts, "bin").or_else(|| opts.get("bin").map(|s| s.to_string()))
{
let request_options = tv.request.options();
let opts = CargoOptions::new(&request_options);
if let Some(bin) = opts.bin() {
cmd = cmd.arg(format!("--bin={bin}"));
}
if opts
.get("locked")
.is_none_or(|v| v.to_lowercase() != "false")
{
if opts.locked() {
cmd = cmd.arg("--locked");
}
if let Some(features) = opts.get("features") {
if let Some(features) = opts.features() {
cmd = cmd.arg(format!("--features={features}"));
}
if let Some(default_features) = opts.get("default-features")
&& default_features.to_lowercase() == "false"
{
if opts.default_features_disabled() {
cmd = cmd.arg("--no-default-features");
}
if let Some(c) = opts.get("crate") {
if let Some(c) = opts.crate_arg() {
cmd = cmd.arg(c);
}
if let Some(registry_name) = registry_name {
Expand All @@ -161,7 +220,7 @@ impl Backend for CargoBackend {
.arg(tv.install_path())
.with_pr(ctx.pr.as_ref())
.envs(ctx.ts.env_with_path_without_tools(&ctx.config).await?)
.envs(opts.install_env.clone())
.envs(opts.install_env().clone())
.prepend_path(ctx.ts.list_paths(&ctx.config).await)?
.prepend_path(
self.dependency_toolset(&ctx.config)
Expand All @@ -177,19 +236,10 @@ impl Backend for CargoBackend {
fn resolve_lockfile_options(
&self,
request: &ToolRequest,
_target: &PlatformTarget,
target: &PlatformTarget,
) -> BTreeMap<String, String> {
let opts = request.options();
let mut result = BTreeMap::new();

// These options affect what gets compiled/installed
for key in ["features", "default-features", "bin"] {
if let Some(value) = opts.get(key) {
result.insert(key.to_string(), value.to_string());
}
}

result
CargoOptions::new(&opts).lockfile_options(target)
}
}

Expand Down Expand Up @@ -219,8 +269,9 @@ impl CargoBackend {
}
}
}
let opts = tv.request.options();
if opts.contains_key("features") || opts.contains_key("default-features") {
let request_options = tv.request.options();
let opts = CargoOptions::new(&request_options);
if opts.has_features_options() {
info!("not using cargo-binstall because features are specified");
return false;
}
Expand Down Expand Up @@ -250,3 +301,27 @@ struct CratesIoVersion {
yanked: bool,
created_at: String,
}

#[cfg(test)]
mod tests {
use super::*;
use crate::platform::Platform;

#[test]
fn test_lockfile_options_uses_target_platform_bin() {
let mut opts = ToolVersionOptions::default();
opts.opts
.insert("bin".into(), toml::Value::String("base-bin".into()));
let mut platforms = toml::Table::new();
let mut linux = toml::Table::new();
linux.insert("bin".into(), toml::Value::String("linux-bin".into()));
platforms.insert("linux-x64".into(), toml::Value::Table(linux));
opts.opts
.insert("platforms".into(), toml::Value::Table(platforms));

let target = PlatformTarget::new(Platform::parse("linux-x64").unwrap());
let lock_opts = CargoOptions::new(&opts).lockfile_options(&target);

assert_eq!(lock_opts.get("bin").map(String::as_str), Some("linux-bin"));
}
}
Loading