diff --git a/src/binstall.rs b/src/binstall.rs index f28c0e5d..c6fcc569 100644 --- a/src/binstall.rs +++ b/src/binstall.rs @@ -1,8 +1,6 @@ use std::path::PathBuf; -use compact_str::CompactString; - -use crate::{metafiles, DesiredTargets, PkgOverride}; +use crate::{metafiles::binstall_v1::MetaData, DesiredTargets, PkgOverride}; mod resolve; pub use resolve::*; @@ -18,11 +16,3 @@ pub struct Options { pub cli_overrides: PkgOverride, pub desired_targets: DesiredTargets, } - -/// MetaData required to update MetaFiles. -pub struct MetaData { - pub bins: Vec, - pub cvs: metafiles::CrateVersionSource, - pub version_req: String, - pub target: String, -} diff --git a/src/binstall/install.rs b/src/binstall/install.rs index e7f12a36..21691323 100644 --- a/src/binstall/install.rs +++ b/src/binstall/install.rs @@ -1,12 +1,13 @@ use std::{path::PathBuf, process, sync::Arc}; use cargo_toml::Package; +use compact_str::CompactString; use log::{debug, error, info}; use miette::{miette, IntoDiagnostic, Result, WrapErr}; use tokio::{process::Command, task::block_in_place}; use super::{MetaData, Options, Resolution}; -use crate::{bins, fetchers::Fetcher, *}; +use crate::{bins, fetchers::Fetcher, metafiles::binstall_v1::Source, *}; pub async fn install( resolution: Resolution, @@ -22,13 +23,21 @@ pub async fn install( bin_path, bin_files, } => { - let cvs = metafiles::CrateVersionSource { - name, - version: package.version.parse().into_diagnostic()?, - source: metafiles::Source::cratesio_registry(), - }; + let current_version = package.version.parse().into_diagnostic()?; + let target = fetcher.target().into(); - install_from_package(fetcher, opts, cvs, version, bin_path, bin_files).await + install_from_package(fetcher, opts, bin_path, bin_files) + .await + .map(|option| { + option.map(|bins| MetaData { + name: name.into(), + version_req: version.into(), + current_version, + source: Source::cratesio_registry(), + target, + bins, + }) + }) } Resolution::InstallFromSource { package } => { let desired_targets = opts.desired_targets.get().await; @@ -54,11 +63,9 @@ pub async fn install( async fn install_from_package( fetcher: Arc, opts: Arc, - cvs: metafiles::CrateVersionSource, - version: String, bin_path: PathBuf, bin_files: Vec, -) -> Result> { +) -> Result>> { // Download package if opts.dry_run { info!("Dry run, not downloading package"); @@ -108,12 +115,9 @@ async fn install_from_package( } } - Ok(Some(MetaData { - bins: bin_files.into_iter().map(|bin| bin.base_name).collect(), - cvs, - version_req: version, - target: fetcher.target().to_string(), - })) + Ok(Some( + bin_files.into_iter().map(|bin| bin.base_name).collect(), + )) }) } diff --git a/src/main.rs b/src/main.rs index ab652f26..51d86863 100644 --- a/src/main.rs +++ b/src/main.rs @@ -344,11 +344,7 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> { block_in_place(|| { if !custom_install_path { debug!("Writing .crates.toml"); - metafiles::v1::CratesToml::append( - metadata_vec - .iter() - .map(|metadata| (&metadata.cvs, metadata.bins.clone())), - )?; + metafiles::v1::CratesToml::append(metadata_vec.iter())?; } if opts.no_cleanup { diff --git a/src/metafiles/binstall_v1.rs b/src/metafiles/binstall_v1.rs index a99e9d89..475e1274 100644 --- a/src/metafiles/binstall_v1.rs +++ b/src/metafiles/binstall_v1.rs @@ -23,30 +23,6 @@ pub struct MetaData { pub target: CompactString, pub bins: Vec, } -impl MetaData { - pub fn new(metadata: crate::binstall::MetaData) -> Self { - let crate::binstall::MetaData { - bins, - cvs: - super::CrateVersionSource { - name, - version, - source, - }, - version_req, - target, - } = metadata; - - Self { - name: name.into(), - version_req: version_req.into(), - current_version: version, - source: source.into(), - target: target.into(), - bins, - } - } -} #[derive(Debug, Copy, Clone, Serialize, Deserialize)] pub enum SourceType { @@ -70,27 +46,6 @@ impl Source { } } -impl From for Source { - fn from(src: super::Source) -> Self { - use super::Source::*; - - match src { - Git(url) => Source { - source_type: SourceType::Git, - url, - }, - Path(url) => Source { - source_type: SourceType::Path, - url, - }, - Registry(url) => Source { - source_type: SourceType::Registry, - url, - }, - } - } -} - #[derive(Debug, Diagnostic, Error)] pub enum Error { #[error(transparent)] diff --git a/src/metafiles/cvs.rs b/src/metafiles/cvs.rs index 15121c69..f0068328 100644 --- a/src/metafiles/cvs.rs +++ b/src/metafiles/cvs.rs @@ -15,6 +15,16 @@ pub struct CrateVersionSource { pub source: Source, } +impl From<&super::binstall_v1::MetaData> for CrateVersionSource { + fn from(metadata: &super::binstall_v1::MetaData) -> Self { + super::CrateVersionSource { + name: metadata.name.clone().to_string(), + version: metadata.current_version.clone(), + source: Source::from(&metadata.source), + } + } +} + #[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)] pub enum Source { Git(Url), @@ -28,6 +38,20 @@ impl Source { } } +impl From<&super::binstall_v1::Source> for Source { + fn from(source: &super::binstall_v1::Source) -> Self { + use super::binstall_v1::SourceType::*; + + let url = source.url.clone(); + + match source.source_type { + Git => Self::Git(url), + Path => Self::Path(url), + Registry => Self::Registry(url), + } + } +} + impl FromStr for CrateVersionSource { type Err = CvsParseError; fn from_str(s: &str) -> Result { diff --git a/src/metafiles/v1.rs b/src/metafiles/v1.rs index 374188ba..25ef6c3f 100644 --- a/src/metafiles/v1.rs +++ b/src/metafiles/v1.rs @@ -11,7 +11,7 @@ use miette::Diagnostic; use serde::{Deserialize, Serialize}; use thiserror::Error; -use super::CrateVersionSource; +use super::{binstall_v1::MetaData, CrateVersionSource}; use crate::{cargo_home, create_if_not_exist, FileLock}; #[derive(Clone, Debug, Default, Deserialize, Serialize)] @@ -71,13 +71,13 @@ impl CratesToml { iter: Iter, ) -> Result<(), CratesTomlParseError> where - Iter: IntoIterator)>, + Iter: IntoIterator, { let mut file = FileLock::new_exclusive(create_if_not_exist(path.as_ref())?)?; let mut c1 = Self::load_from_reader(&mut *file)?; - for (cvs, bins) in iter { - c1.insert(cvs, bins); + for metadata in iter { + c1.insert(&CrateVersionSource::from(metadata), metadata.bins.clone()); } file.rewind()?; @@ -88,7 +88,7 @@ impl CratesToml { pub fn append<'a, Iter>(iter: Iter) -> Result<(), CratesTomlParseError> where - Iter: IntoIterator)>, + Iter: IntoIterator, { Self::append_to_path(Self::default_path()?, iter) }