From 570684cd75e7f91e08b58aa841166148a96b9394 Mon Sep 17 00:00:00 2001 From: Alexander Koz Date: Sat, 1 Jun 2024 16:17:04 +0400 Subject: [PATCH] allow use other strings --- cargo/src/assets/mod.rs | 1 + cargo/src/utils/cargo/metadata.rs | 3 +- support/build/src/metadata/format.rs | 68 ++++++++++---------- support/build/src/metadata/source.rs | 92 ++++++++++++++-------------- 4 files changed, 87 insertions(+), 77 deletions(-) diff --git a/cargo/src/assets/mod.rs b/cargo/src/assets/mod.rs index a8910de5..fc931e5f 100644 --- a/cargo/src/assets/mod.rs +++ b/cargo/src/assets/mod.rs @@ -55,6 +55,7 @@ pub fn build<'cfg>(config: &'cfg Config) -> CargoResult> { layout.clean()?; } + // primary top-level package let target_pid = package.package_id(); let has_dev = targets.iter() .any(|t| t.is_example() || t.is_test() || t.is_bench()); diff --git a/cargo/src/utils/cargo/metadata.rs b/cargo/src/utils/cargo/metadata.rs index 69aeeae0..61b5d83b 100644 --- a/cargo/src/utils/cargo/metadata.rs +++ b/cargo/src/utils/cargo/metadata.rs @@ -1,5 +1,6 @@ use std::ffi::OsStr; +use cargo::util::interning::InternedString; use cargo::CargoResult; use crate::config::Config; @@ -7,7 +8,7 @@ use crate::proc::cargo_proxy_with; use crate::proc::read_cargo_json; -pub type CargoMetadataPd = format::Report; +pub type CargoMetadataPd = format::Report>; pub fn metadata(cfg: &Config) -> CargoResult { diff --git a/support/build/src/metadata/format.rs b/support/build/src/metadata/format.rs index 6a8e8ee3..aba77561 100644 --- a/support/build/src/metadata/format.rs +++ b/support/build/src/metadata/format.rs @@ -13,8 +13,8 @@ use super::source::*; #[derive(Debug)] #[cfg_attr(feature = "serde", derive(serde::Deserialize))] #[cfg_attr(feature = "serde", - serde(bound(deserialize = "S: Deserialize<'de> + Default + Eq + Hash")))] -pub struct CrateMetadata { + serde(bound(deserialize = "S: Deserialize<'de> + Eq + Hash")))] +pub struct CrateMetadata { #[cfg_attr(feature = "serde", serde(rename = "playdate"))] pub inner: Option>, } @@ -33,13 +33,13 @@ fn eq_metadata_field() { /// - Configuration table - `options` #[derive(Debug, Clone, PartialEq)] -pub struct Metadata { +pub struct Metadata { pub(super) inner: MetadataInner, } #[cfg(feature = "serde")] -impl<'de, S: Deserialize<'de> + Default + Eq + Hash> Deserialize<'de> for Metadata { +impl<'de, S: Deserialize<'de> + Eq + Hash> Deserialize<'de> for Metadata { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de> { let meta = MetadataInner::::deserialize(deserializer)?; @@ -49,23 +49,29 @@ impl<'de, S: Deserialize<'de> + Default + Eq + Hash> Deserialize<'de> for Metada } -impl MetadataSource for Metadata { - type Manifest = Ext>; - type TargetManifest = Override; +impl MetadataSource for Metadata + where S: Eq + Hash + AsRef, + Override: ManifestSourceOptExt, + Ext>: ManifestSourceOptExt, + for<'t> &'t Ext>: ManifestSourceOptExt +{ + type S = S; + type Manifest = Ext>; + type TargetManifest = Override; - fn manifest(&self) -> impl ManifestSourceOptExt { &self.inner.manifest } + fn manifest(&self) -> &Self::Manifest { &self.inner.manifest } fn bins(&self) -> &[Self::TargetManifest] { self.inner.bins.as_slice() } fn examples(&self) -> &[Self::TargetManifest] { self.inner.examples.as_slice() } - fn bin_targets(&self) -> impl IntoIterator { self.inner.bins.iter().map(|o| o.target.as_str()) } + fn bin_targets(&self) -> impl IntoIterator { self.inner.bins.iter().map(|o| o.target.as_ref()) } fn example_targets(&self) -> impl IntoIterator { - self.inner.examples.iter().map(|o| o.target.as_str()) + self.inner.examples.iter().map(|o| o.target.as_ref()) } - fn assets(&self) -> &AssetsRules { &self.inner.assets } - fn dev_assets(&self) -> &AssetsRules { &self.inner.dev_assets } + fn assets(&self) -> &AssetsRules { &self.inner.assets } + fn dev_assets(&self) -> &AssetsRules { &self.inner.dev_assets } fn options(&self) -> &Options { &self.inner.options } @@ -88,8 +94,8 @@ impl MetadataSource for Metadata { #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "serde", derive(Deserialize))] #[cfg_attr(feature = "serde", - serde(bound(deserialize = "S: Deserialize<'de> + Default + Eq + Hash")))] -pub(super) struct MetadataInner { + serde(bound(deserialize = "S: Deserialize<'de> + Eq + Hash")))] +pub(super) struct MetadataInner { #[cfg_attr(feature = "serde", serde(flatten))] pub(super) manifest: Ext>, @@ -134,7 +140,7 @@ impl Ext { } impl Ext> where S: ToOwned { - pub fn clone_owned(self) -> Ext::Owned>> { + pub fn clone_owned(&self) -> Ext::Owned>> { Ext { main: self.main.clone_owned(), extra: self.extra.to_owned() } } @@ -223,16 +229,16 @@ impl IntoOwned::Owned>> for Manifest> { } impl Manifest where S: ToOwned { - pub fn clone_owned(self) -> Manifest<::Owned> { - Manifest { name: self.name.map(|s| s.to_owned()), - version: self.version.map(|s| s.to_owned()), - author: self.author.map(|s| s.to_owned()), - bundle_id: self.bundle_id.map(|s| s.to_owned()), - description: self.description.map(|s| s.to_owned()), - image_path: self.image_path.map(|s| s.to_owned()), - launch_sound_path: self.launch_sound_path.map(|s| s.to_owned()), - content_warning: self.content_warning.map(|s| s.to_owned()), - content_warning2: self.content_warning2.map(|s| s.to_owned()), + pub fn clone_owned(&self) -> Manifest<::Owned> { + Manifest { name: self.name.as_ref().map(|s| s.to_owned()), + version: self.version.as_ref().map(|s| s.to_owned()), + author: self.author.as_ref().map(|s| s.to_owned()), + bundle_id: self.bundle_id.as_ref().map(|s| s.to_owned()), + description: self.description.as_ref().map(|s| s.to_owned()), + image_path: self.image_path.as_ref().map(|s| s.to_owned()), + launch_sound_path: self.launch_sound_path.as_ref().map(|s| s.to_owned()), + content_warning: self.content_warning.as_ref().map(|s| s.to_owned()), + content_warning2: self.content_warning2.as_ref().map(|s| s.to_owned()), build_number: self.build_number } } } @@ -240,7 +246,7 @@ impl Manifest where S: ToOwned { #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "serde", derive(Deserialize))] -#[cfg_attr(feature = "serde", serde(bound(deserialize = "S: Deserialize<'de> +Default")))] +#[cfg_attr(feature = "serde", serde(bound(deserialize = "S: Deserialize<'de>")))] pub struct Override { /// Associated cargo-target name #[cfg_attr(feature = "serde", serde(rename = "id", alias = "target"))] @@ -274,7 +280,7 @@ impl<'t> IntoOwned> for Override> { } impl Override where S: ToOwned { - pub fn clone_owned(self) -> Override<::Owned> { + pub fn clone_owned(&self) -> Override<::Owned> { Override { target: self.target.to_owned(), manifest: self.manifest.clone_owned() } } @@ -659,10 +665,10 @@ mod one_of { pub fn assets_rules<'de, S, D>(deserializer: D) -> Result, D::Error> where D: Deserializer<'de>, - S: Deserialize<'de> + Eq + Hash + Default { + S: Deserialize<'de> + Eq + Hash { struct OneOf(PhantomData); - impl<'de, S> Visitor<'de> for OneOf where S: Deserialize<'de> + Eq + Hash + Default { + impl<'de, S> Visitor<'de> for OneOf where S: Deserialize<'de> + Eq + Hash { type Value = super::AssetsRules; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -688,10 +694,10 @@ mod one_of { pub fn targets_overrides<'de, S, D>(deserializer: D) -> Result>, D::Error> where D: Deserializer<'de>, - S: Deserialize<'de> + Eq + Hash + Default { + S: Deserialize<'de> + Eq + Hash { struct OneOf(PhantomData); - impl<'de, S> Visitor<'de> for OneOf where S: Deserialize<'de> + Eq + Hash + Default { + impl<'de, S> Visitor<'de> for OneOf where S: Deserialize<'de> + Eq + Hash { type Value = Vec>; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { diff --git a/support/build/src/metadata/source.rs b/support/build/src/metadata/source.rs index f3fc290e..f983510d 100644 --- a/support/build/src/metadata/source.rs +++ b/support/build/src/metadata/source.rs @@ -1,3 +1,4 @@ +use std::hash::Hash; use std::borrow::Cow; use std::path::Path; @@ -27,46 +28,45 @@ pub trait CrateInfoSource { fn manifest_for_crate(&self) -> impl ManifestSourceOptExt { use super::format::Manifest; - { - let author = { - let author = self.authors().join(", "); - if author.trim().is_empty() { - None - } else { - Some(author.into()) - } - }; - let version = Some(self.version()); - let package = Manifest { name: Some(self.name()), - description: self.description(), - author, - version, - bundle_id: None, - image_path: None, - launch_sound_path: None, - content_warning: None, - content_warning2: None, - build_number: None }; - - if let Some(meta) = self.metadata() { - let manifest = meta.manifest(); - let base = Ext { main: package, - extra: Default::default() }; - // TODO: Reduce coping, return associated type instead with all strings in the Cow<'self>. - // Also get merged manifest with refs, using `override_with_extra_ref` - let result = base.override_with_extra(&manifest); - Ext { main: Manifest::from(&result), - extra: result.iter_extra() - .map(|m| { - m.into_iter() - .map(|(k, v)| (k.as_ref().to_owned(), v.as_ref().clone())) - .collect() - }) - .unwrap_or_default() } + + let author = { + let author = self.authors().join(", "); + if author.trim().is_empty() { + None } else { - Ext { main: package.into_owned(), - extra: Default::default() } + Some(author.into()) } + }; + let version = Some(self.version()); + let package = Manifest { name: Some(self.name()), + description: self.description(), + author, + version, + bundle_id: None, + image_path: None, + launch_sound_path: None, + content_warning: None, + content_warning2: None, + build_number: None }; + + if let Some(meta) = self.metadata() { + let manifest = meta.manifest(); + let base = Ext { main: package, + extra: Default::default() }; + // TODO: Reduce coping, return associated type instead with all strings in the Cow<'self>. + // Also get merged manifest with refs, using `override_with_extra_ref` + let result = base.override_with_extra(manifest); + Ext { main: Manifest::from(&result), + extra: result.iter_extra() + .map(|m| { + m.into_iter() + .map(|(k, v)| (k.as_ref().to_owned(), v.as_ref().clone())) + .collect() + }) + .unwrap_or_default() } + } else { + Ext { main: package.into_owned(), + extra: Default::default() } } } @@ -103,11 +103,12 @@ pub trait CrateInfoSource { pub trait MetadataSource { + type S: Eq + Hash; type Manifest: ManifestSourceOptExt; type TargetManifest: ManifestSourceOptExt + TargetId; /// Main manifest, default and base for all cargo-targets. - fn manifest(&self) -> impl ManifestSourceOptExt; + fn manifest(&self) -> &Self::Manifest; /// All manifests for "bin" cargo-targets. /// Overrides main manifest field-by-field. @@ -147,8 +148,8 @@ pub trait MetadataSource { .chain(self.examples_iter().into_iter().flatten()) } - fn assets(&self) -> &AssetsRules; - fn dev_assets(&self) -> &AssetsRules; + fn assets(&self) -> &AssetsRules; + fn dev_assets(&self) -> &AssetsRules; fn options(&self) -> &Options; fn assets_options(&self) -> Cow<'_, AssetsOptions>; @@ -181,17 +182,18 @@ pub trait MetadataSource { self.manifest_for_target(target, false) .or_else(|| self.manifest_for_target(target, true)) .map(|m| m.into_manifest()) - .or_else(|| Some(self.manifest().into_manifest())) + .or_else(|| Some(Ext::>::from(self.manifest()))) } } impl MetadataSource for &T { + type S = ::S; type Manifest = ::Manifest; type TargetManifest = ::TargetManifest; - fn manifest(&self) -> impl ManifestSourceOptExt { (*self).manifest() } + fn manifest(&self) -> &Self::Manifest { (*self).manifest() } fn bins(&self) -> &[Self::TargetManifest] { ::bins(*self) } fn examples(&self) -> &[Self::TargetManifest] { ::examples(*self) } @@ -199,8 +201,8 @@ impl MetadataSource for &T { fn bin_targets(&self) -> impl IntoIterator { (*self).bin_targets() } fn example_targets(&self) -> impl IntoIterator { (*self).example_targets() } - fn assets(&self) -> &AssetsRules { (*self).assets() } - fn dev_assets(&self) -> &AssetsRules { (*self).dev_assets() } + fn assets(&self) -> &AssetsRules { (*self).assets() } + fn dev_assets(&self) -> &AssetsRules { (*self).dev_assets() } fn options(&self) -> &Options { (*self).options() } fn assets_options(&self) -> Cow<'_, AssetsOptions> { (*self).assets_options() } fn support(&self) -> &Support { (*self).support() }