Skip to content

Commit

Permalink
allow use other strings
Browse files Browse the repository at this point in the history
  • Loading branch information
boozook committed Jun 1, 2024
1 parent 7d16e3c commit 570684c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 77 deletions.
1 change: 1 addition & 0 deletions cargo/src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub fn build<'cfg>(config: &'cfg Config) -> CargoResult<AssetsArtifacts<'cfg>> {
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());
Expand Down
3 changes: 2 additions & 1 deletion cargo/src/utils/cargo/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::ffi::OsStr;

use cargo::util::interning::InternedString;
use cargo::CargoResult;

use crate::config::Config;
use crate::proc::cargo_proxy_with;
use crate::proc::read_cargo_json;


pub type CargoMetadataPd = format::Report<format::Metadata>;
pub type CargoMetadataPd = format::Report<format::Metadata<InternedString>>;


pub fn metadata(cfg: &Config) -> CargoResult<CargoMetadataPd> {
Expand Down
68 changes: 37 additions & 31 deletions support/build/src/metadata/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S: Default + Eq + Hash = String> {
serde(bound(deserialize = "S: Deserialize<'de> + Eq + Hash")))]
pub struct CrateMetadata<S: Eq + Hash = String> {
#[cfg_attr(feature = "serde", serde(rename = "playdate"))]
pub inner: Option<Metadata<S>>,
}
Expand All @@ -33,13 +33,13 @@ fn eq_metadata_field() {
/// - Configuration table - `options`
#[derive(Debug, Clone, PartialEq)]

pub struct Metadata<S: Default + Eq + Hash = String> {
pub struct Metadata<S: Eq + Hash = String> {
pub(super) inner: MetadataInner<S>,
}


#[cfg(feature = "serde")]
impl<'de, S: Deserialize<'de> + Default + Eq + Hash> Deserialize<'de> for Metadata<S> {
impl<'de, S: Deserialize<'de> + Eq + Hash> Deserialize<'de> for Metadata<S> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de> {
let meta = MetadataInner::<S>::deserialize(deserializer)?;
Expand All @@ -49,23 +49,29 @@ impl<'de, S: Deserialize<'de> + Default + Eq + Hash> Deserialize<'de> for Metada
}


impl MetadataSource for Metadata {
type Manifest = Ext<Manifest<String>>;
type TargetManifest = Override<String>;
impl<S> MetadataSource for Metadata<S>
where S: Eq + Hash + AsRef<str>,
Override<S>: ManifestSourceOptExt,
Ext<Manifest<S>>: ManifestSourceOptExt,
for<'t> &'t Ext<Manifest<S>>: ManifestSourceOptExt
{
type S = S;
type Manifest = Ext<Manifest<S>>;
type TargetManifest = Override<S>;


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<Item = &str> { self.inner.bins.iter().map(|o| o.target.as_str()) }
fn bin_targets(&self) -> impl IntoIterator<Item = &str> { self.inner.bins.iter().map(|o| o.target.as_ref()) }
fn example_targets(&self) -> impl IntoIterator<Item = &str> {
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<S> { &self.inner.assets }
fn dev_assets(&self) -> &AssetsRules<S> { &self.inner.dev_assets }


fn options(&self) -> &Options { &self.inner.options }
Expand All @@ -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<S: Default + Eq + Hash = String> {
serde(bound(deserialize = "S: Deserialize<'de> + Eq + Hash")))]
pub(super) struct MetadataInner<S: Eq + Hash = String> {
#[cfg_attr(feature = "serde", serde(flatten))]
pub(super) manifest: Ext<Manifest<S>>,

Expand Down Expand Up @@ -134,7 +140,7 @@ impl<T> Ext<T> {
}

impl<S> Ext<Manifest<S>> where S: ToOwned {
pub fn clone_owned(self) -> Ext<Manifest<<S as ToOwned>::Owned>> {
pub fn clone_owned(&self) -> Ext<Manifest<<S as ToOwned>::Owned>> {
Ext { main: self.main.clone_owned(),
extra: self.extra.to_owned() }
}
Expand Down Expand Up @@ -223,24 +229,24 @@ impl IntoOwned<Manifest<<str as ToOwned>::Owned>> for Manifest<Cow<'_, str>> {
}

impl<S> Manifest<S> where S: ToOwned {
pub fn clone_owned(self) -> Manifest<<S as ToOwned>::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<<S as ToOwned>::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 }
}
}


#[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<S> {
/// Associated cargo-target name
#[cfg_attr(feature = "serde", serde(rename = "id", alias = "target"))]
Expand Down Expand Up @@ -274,7 +280,7 @@ impl<'t> IntoOwned<Override<String>> for Override<Cow<'t, str>> {
}

impl<S> Override<S> where S: ToOwned {
pub fn clone_owned(self) -> Override<<S as ToOwned>::Owned> {
pub fn clone_owned(&self) -> Override<<S as ToOwned>::Owned> {
Override { target: self.target.to_owned(),
manifest: self.manifest.clone_owned() }
}
Expand Down Expand Up @@ -659,10 +665,10 @@ mod one_of {

pub fn assets_rules<'de, S, D>(deserializer: D) -> Result<super::AssetsRules<S>, D::Error>
where D: Deserializer<'de>,
S: Deserialize<'de> + Eq + Hash + Default {
S: Deserialize<'de> + Eq + Hash {
struct OneOf<S>(PhantomData<S>);

impl<'de, S> Visitor<'de> for OneOf<S> where S: Deserialize<'de> + Eq + Hash + Default {
impl<'de, S> Visitor<'de> for OneOf<S> where S: Deserialize<'de> + Eq + Hash {
type Value = super::AssetsRules<S>;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
Expand All @@ -688,10 +694,10 @@ mod one_of {

pub fn targets_overrides<'de, S, D>(deserializer: D) -> Result<Vec<super::Override<S>>, D::Error>
where D: Deserializer<'de>,
S: Deserialize<'de> + Eq + Hash + Default {
S: Deserialize<'de> + Eq + Hash {
struct OneOf<S>(PhantomData<S>);

impl<'de, S> Visitor<'de> for OneOf<S> where S: Deserialize<'de> + Eq + Hash + Default {
impl<'de, S> Visitor<'de> for OneOf<S> where S: Deserialize<'de> + Eq + Hash {
type Value = Vec<super::Override<S>>;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
Expand Down
92 changes: 47 additions & 45 deletions support/build/src/metadata/source.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::hash::Hash;
use std::borrow::Cow;
use std::path::Path;

Expand Down Expand Up @@ -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() }
}
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<Self::S>;
fn dev_assets(&self) -> &AssetsRules<Self::S>;

fn options(&self) -> &Options;
fn assets_options(&self) -> Cow<'_, AssetsOptions>;
Expand Down Expand Up @@ -181,26 +182,27 @@ 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::<Manifest<String>>::from(self.manifest())))
}
}


impl<T: MetadataSource> MetadataSource for &T {
type S = <T as MetadataSource>::S;
type Manifest = <T as MetadataSource>::Manifest;
type TargetManifest = <T as MetadataSource>::TargetManifest;


fn manifest(&self) -> impl ManifestSourceOptExt { (*self).manifest() }
fn manifest(&self) -> &Self::Manifest { (*self).manifest() }

fn bins(&self) -> &[Self::TargetManifest] { <T as MetadataSource>::bins(*self) }
fn examples(&self) -> &[Self::TargetManifest] { <T as MetadataSource>::examples(*self) }

fn bin_targets(&self) -> impl IntoIterator<Item = &str> { (*self).bin_targets() }
fn example_targets(&self) -> impl IntoIterator<Item = &str> { (*self).example_targets() }

fn assets(&self) -> &AssetsRules { (*self).assets() }
fn dev_assets(&self) -> &AssetsRules { (*self).dev_assets() }
fn assets(&self) -> &AssetsRules<Self::S> { (*self).assets() }
fn dev_assets(&self) -> &AssetsRules<Self::S> { (*self).dev_assets() }
fn options(&self) -> &Options { (*self).options() }
fn assets_options(&self) -> Cow<'_, AssetsOptions> { (*self).assets_options() }
fn support(&self) -> &Support { (*self).support() }
Expand Down

0 comments on commit 570684c

Please sign in to comment.