diff --git a/Cargo.lock b/Cargo.lock index 99e81e8d..0aa8c399 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -151,6 +151,15 @@ version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" +[[package]] +name = "beef" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a8241f3ebb85c056b509d4327ad0358fbbba6ffb340bf388f26350aeda225b1" +dependencies = [ + "serde", +] + [[package]] name = "binstalk" version = "0.6.0" @@ -216,6 +225,7 @@ dependencies = [ name = "binstalk-manifests" version = "0.1.1" dependencies = [ + "beef", "binstalk-types", "compact_str", "detect-targets", @@ -228,7 +238,7 @@ dependencies = [ "serde_json", "tempfile", "thiserror", - "toml_edit", + "toml", "url", ] @@ -237,6 +247,7 @@ name = "binstalk-types" version = "0.1.0" dependencies = [ "compact_str", + "maybe-owned", "once_cell", "semver", "serde", @@ -463,16 +474,6 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "combine" -version = "4.6.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35ed6e9d84f0b51a7f52daf1c7d71dd136fd7a3f41a8462b8cdb8c78d920fad4" -dependencies = [ - "bytes", - "memchr", -] - [[package]] name = "compact_str" version = "0.6.1" @@ -1341,6 +1342,15 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" +[[package]] +name = "maybe-owned" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4facc753ae494aeb6e3c22f839b158aebd4f9270f55cd3c79906c45476c47ab4" +dependencies = [ + "serde", +] + [[package]] name = "memchr" version = "2.5.0" @@ -2436,35 +2446,13 @@ dependencies = [ [[package]] name = "toml" -version = "0.5.8" +version = "0.5.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +checksum = "1333c76748e868a4d9d1017b5ab53171dfd095f70c712fdb4653a406547f598f" dependencies = [ "serde", ] -[[package]] -name = "toml_datetime" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "808b51e57d0ef8f71115d8f3a01e7d3750d01c79cac4b3eda910f4389fdf92fd" -dependencies = [ - "serde", -] - -[[package]] -name = "toml_edit" -version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1541ba70885967e662f69d31ab3aeca7b1aaecfcd58679590b893e9239c3646" -dependencies = [ - "combine", - "indexmap", - "itertools", - "serde", - "toml_datetime", -] - [[package]] name = "tower" version = "0.4.13" diff --git a/crates/binstalk-manifests/Cargo.toml b/crates/binstalk-manifests/Cargo.toml index d88e74f3..aee13394 100644 --- a/crates/binstalk-manifests/Cargo.toml +++ b/crates/binstalk-manifests/Cargo.toml @@ -10,6 +10,7 @@ edition = "2021" license = "Apache-2.0 OR MIT" [dependencies] +beef = { version = "0.5.2", features = ["impl_serde"] } binstalk-types = { version = "0.1.0", path = "../binstalk-types" } compact_str = { version = "0.6.1", features = ["serde"] } fs-lock = { version = "0.1.0", path = "../fs-lock" } @@ -20,7 +21,7 @@ serde = { version = "1.0.151", features = ["derive"] } serde-tuple-vec-map = "1.0.1" serde_json = "1.0.91" thiserror = "1.0.38" -toml_edit = { version = "0.15.0", features = ["easy"] } +toml = "0.5.10" url = { version = "2.3.1", features = ["serde"] } [dev-dependencies] diff --git a/crates/binstalk-manifests/src/cargo_crates_v1.rs b/crates/binstalk-manifests/src/cargo_crates_v1.rs index 536c484b..b9528220 100644 --- a/crates/binstalk-manifests/src/cargo_crates_v1.rs +++ b/crates/binstalk-manifests/src/cargo_crates_v1.rs @@ -15,6 +15,7 @@ use std::{ path::{Path, PathBuf}, }; +use beef::Cow; use compact_str::CompactString; use fs_lock::FileLock; use home::cargo_home; @@ -31,12 +32,12 @@ mod crate_version_source; use crate_version_source::*; #[derive(Clone, Debug, Default, Deserialize, Serialize)] -pub struct CratesToml { +pub struct CratesToml<'a> { #[serde(with = "tuple_vec_map")] - v1: Vec<(String, Vec)>, + v1: Vec<(String, Cow<'a, [CompactString]>)>, } -impl CratesToml { +impl CratesToml<'_> { pub fn default_path() -> Result { Ok(cargo_home()?.join(".crates.toml")) } @@ -48,7 +49,7 @@ impl CratesToml { pub fn load_from_reader(mut reader: R) -> Result { let mut vec = Vec::new(); reader.read_to_end(&mut vec)?; - Ok(toml_edit::easy::from_slice(&vec)?) + Ok(toml::from_slice(&vec)?) } pub fn load_from_path(path: impl AsRef) -> Result { @@ -59,7 +60,7 @@ impl CratesToml { /// Only use it when you know that the crate is not in the manifest. /// Otherwise, you need to call [`CratesToml::remove`] first. pub fn insert(&mut self, cvs: &CrateVersionSource, bins: Vec) { - self.v1.push((cvs.to_string(), bins)); + self.v1.push((cvs.to_string(), Cow::owned(bins))); } pub fn remove(&mut self, name: &str) { @@ -75,7 +76,7 @@ impl CratesToml { } pub fn write_to_writer(&self, mut writer: W) -> Result<(), CratesTomlParseError> { - let data = toml_edit::easy::to_vec(&self)?; + let data = toml::to_vec(&self)?; writer.write_all(&data)?; Ok(()) } @@ -108,8 +109,15 @@ impl CratesToml { }; for metadata in iter { - c1.remove(&metadata.name); - c1.insert(&CrateVersionSource::from(metadata), metadata.bins.clone()); + let name = &metadata.name; + let version = &metadata.current_version; + let source = Source::from(&metadata.source); + + c1.remove(name); + c1.v1.push(( + format!("{name} {version} ({source})"), + Cow::borrowed(&metadata.bins), + )); } file.rewind()?; @@ -151,10 +159,10 @@ pub enum CratesTomlParseError { Io(#[from] io::Error), #[error(transparent)] - TomlParse(#[from] toml_edit::easy::de::Error), + TomlParse(#[from] toml::de::Error), #[error(transparent)] - TomlWrite(Box), + TomlWrite(Box), #[error(transparent)] CvsParse(Box), @@ -166,8 +174,8 @@ impl From for CratesTomlParseError { } } -impl From for CratesTomlParseError { - fn from(e: toml_edit::easy::ser::Error) -> Self { +impl From for CratesTomlParseError { + fn from(e: toml::ser::Error) -> Self { CratesTomlParseError::TomlWrite(Box::new(e)) } } diff --git a/crates/binstalk-manifests/src/cargo_crates_v1/crate_version_source.rs b/crates/binstalk-manifests/src/cargo_crates_v1/crate_version_source.rs index 1ed77011..313532e0 100644 --- a/crates/binstalk-manifests/src/cargo_crates_v1/crate_version_source.rs +++ b/crates/binstalk-manifests/src/cargo_crates_v1/crate_version_source.rs @@ -1,6 +1,6 @@ use std::{borrow::Cow, fmt, str::FromStr}; -use binstalk_types::crate_info::cratesio_url; +use binstalk_types::{crate_info::cratesio_url, maybe_owned::MaybeOwned}; use compact_str::CompactString; use miette::Diagnostic; use semver::Version; @@ -14,37 +14,45 @@ use crate::crate_info::{CrateInfo, CrateSource, SourceType}; pub struct CrateVersionSource { pub name: CompactString, pub version: Version, - pub source: Source, + pub source: Source<'static>, } impl From<&CrateInfo> for CrateVersionSource { fn from(metadata: &CrateInfo) -> Self { + use SourceType::*; + + let url = metadata.source.url.clone(); + super::CrateVersionSource { name: metadata.name.clone(), version: metadata.current_version.clone(), - source: Source::from(&metadata.source), + source: match metadata.source.source_type { + Git => Source::Git(url), + Path => Source::Path(url), + Registry => Source::Registry(url), + }, } } } #[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)] -pub enum Source { - Git(Url), - Path(Url), - Registry(Url), +pub enum Source<'a> { + Git(MaybeOwned<'a, Url>), + Path(MaybeOwned<'a, Url>), + Registry(MaybeOwned<'a, Url>), } -impl Source { - pub fn cratesio_registry() -> Source { - Self::Registry(cratesio_url().clone()) +impl Source<'static> { + pub fn cratesio_registry() -> Self { + Self::Registry(MaybeOwned::Borrowed(cratesio_url())) } } -impl From<&CrateSource> for Source { - fn from(source: &CrateSource) -> Self { +impl<'a> From<&'a CrateSource> for Source<'a> { + fn from(source: &'a CrateSource) -> Self { use SourceType::*; - let url = source.url.clone(); + let url = MaybeOwned::Borrowed(source.url.as_ref()); match source.source_type { Git => Self::Git(url), @@ -65,9 +73,9 @@ impl FromStr for CrateVersionSource { .splitn(2, '+') .collect::>()[..] { - ["git", url] => Source::Git(Url::parse(url)?), - ["path", url] => Source::Path(Url::parse(url)?), - ["registry", url] => Source::Registry(Url::parse(url)?), + ["git", url] => Source::Git(Url::parse(url)?.into()), + ["path", url] => Source::Path(Url::parse(url)?.into()), + ["registry", url] => Source::Registry(Url::parse(url)?.into()), [kind, arg] => { return Err(CvsParseError::UnknownSourceType { kind: kind.to_string().into_boxed_str(), @@ -117,7 +125,7 @@ impl fmt::Display for CrateVersionSource { } } -impl fmt::Display for Source { +impl fmt::Display for Source<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Source::Git(url) => write!(f, "git+{url}"), diff --git a/crates/binstalk-types/Cargo.toml b/crates/binstalk-types/Cargo.toml index 804f1c16..3ca6d593 100644 --- a/crates/binstalk-types/Cargo.toml +++ b/crates/binstalk-types/Cargo.toml @@ -11,6 +11,7 @@ license = "Apache-2.0 OR MIT" [dependencies] compact_str = { version = "0.6.1", features = ["serde"] } +maybe-owned = { version = "0.3.4", features = ["serde"] } once_cell = "1.16.0" semver = { version = "1.0.16", features = ["serde"] } serde = { version = "1.0.151", features = ["derive"] } diff --git a/crates/binstalk-types/src/crate_info.rs b/crates/binstalk-types/src/crate_info.rs index 6d019593..eb28750e 100644 --- a/crates/binstalk-types/src/crate_info.rs +++ b/crates/binstalk-types/src/crate_info.rs @@ -3,6 +3,7 @@ use std::{borrow, cmp, hash}; use compact_str::CompactString; +use maybe_owned::MaybeOwned; use once_cell::sync::Lazy; use semver::Version; use serde::{Deserialize, Serialize}; @@ -69,14 +70,14 @@ pub enum SourceType { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct CrateSource { pub source_type: SourceType, - pub url: Url, + pub url: MaybeOwned<'static, Url>, } impl CrateSource { pub fn cratesio_registry() -> CrateSource { Self { source_type: SourceType::Registry, - url: cratesio_url().clone(), + url: MaybeOwned::Borrowed(cratesio_url()), } } } diff --git a/crates/binstalk-types/src/lib.rs b/crates/binstalk-types/src/lib.rs index f4c48c85..25096520 100644 --- a/crates/binstalk-types/src/lib.rs +++ b/crates/binstalk-types/src/lib.rs @@ -1,2 +1,4 @@ pub mod cargo_toml_binstall; pub mod crate_info; + +pub use maybe_owned;