Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to implement platform features #1197 #6286

Closed
wants to merge 14 commits into from
Prev Previous commit
Next Next commit
Build fixed
  • Loading branch information
snuk182 committed Jan 5, 2019
commit cdc81558aefb1b02fc5feed7b6a7b4c08cc2f7a1
50 changes: 1 addition & 49 deletions src/cargo/core/dependency.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::fmt;
use std::rc::Rc;
use std::str::FromStr;

use log::trace;
use semver::ReqParseError;
Expand All @@ -12,7 +10,7 @@ use url::Url;
use crate::core::interning::InternedString;
use crate::core::{PackageId, SourceId, Summary};
use crate::util::errors::{CargoResult, CargoResultExt};
use crate::util::{Cfg, CfgExpr, Config};
use crate::util::{Platform, Config};

/// Information about a dependency requested by a Cargo manifest.
/// Cheap to copy.
Expand Down Expand Up @@ -432,49 +430,3 @@ impl Dependency {
}
}
}

impl Platform {
pub fn matches(&self, name: &str, cfg: Option<&[Cfg]>) -> bool {
match *self {
Platform::Name(ref p) => p == name,
Platform::Cfg(ref p) => match cfg {
Some(cfg) => p.matches(cfg),
None => false,
},
}
}
}

impl ser::Serialize for Platform {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
self.to_string().serialize(s)
}
}

impl FromStr for Platform {
type Err = failure::Error;

fn from_str(s: &str) -> CargoResult<Platform> {
if s.starts_with("cfg(") && s.ends_with(')') {
let s = &s[4..s.len() - 1];
let p = s.parse().map(Platform::Cfg).chain_err(|| {
failure::format_err!("failed to parse `{}` as a cfg expression", s)
})?;
Ok(p)
} else {
Ok(Platform::Name(s.to_string()))
}
}
}

impl fmt::Display for Platform {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Platform::Name(ref n) => n.fmt(f),
Platform::Cfg(ref e) => write!(f, "cfg({})", e),
}
}
}
2 changes: 1 addition & 1 deletion src/cargo/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use self::registry::Registry;
pub use self::resolver::Resolve;
pub use self::shell::{Shell, Verbosity};
pub use self::source::{GitReference, Source, SourceId, SourceMap};
pub use self::summary::{FeatureMap, RefFeatureMap, FeatureValue, Summary};
pub use self::summary::{FeatureMap, FeatureValue, Summary};
pub use self::workspace::{Members, Workspace, WorkspaceConfig, WorkspaceRootConfig};

pub mod compiler;
Expand Down
11 changes: 3 additions & 8 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use serde::Serialize;
use crate::core::interning::InternedString;
use crate::core::source::MaybePackage;
use crate::core::{Dependency, Manifest, PackageId, SourceId, Target};
use crate::core::{RefFeatureMap, SourceMap, Summary};
use crate::core::{FeatureMap, SourceMap, Summary};
use crate::ops;
use crate::util::errors::{CargoResult, CargoResultExt, HttpNot200};
use crate::util::network::Retry;
Expand Down Expand Up @@ -62,8 +62,8 @@ struct SerializedPackage<'a> {
source: SourceId,
dependencies: &'a [Dependency],
targets: Vec<&'a Target>,
features: RefFeatureMap<'a>,
manifest_path: &'a str,
features: &'a FeatureMap,
manifest_path: &'a Path,
metadata: Option<&'a toml::Value>,
authors: &'a [String],
categories: &'a [String],
Expand Down Expand Up @@ -92,11 +92,6 @@ impl ser::Serialize for Package {
let keywords = manmeta.keywords.as_ref();
let readme = manmeta.readme.as_ref().map(String::as_ref);
let repository = manmeta.repository.as_ref().map(String::as_ref);
let features = summary
.features()
.iter()
.map(|(k, (_, v))| (*k, v.as_slice()))
.collect::<RefFeatureMap<'_>>();
// Filter out metabuild targets. They are an internal implementation
// detail that is probably not relevant externally. There's also not a
// real path to show in `src_path`, and this avoids changing the format.
Expand Down
1 change: 0 additions & 1 deletion src/cargo/core/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,3 @@ impl Serialize for FeatureValue {
}

pub type FeatureMap = BTreeMap<InternedString, (Option<Platform>, Vec<FeatureValue>)>;
pub type RefFeatureMap<'a> = BTreeMap<InternedString, &'a [FeatureValue]>;
48 changes: 48 additions & 0 deletions src/cargo/util/cfg.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,63 @@
use std::fmt;
use std::iter;
use std::str::{self, FromStr};
use serde::ser;

use crate::util::CargoResult;
use crate::util::errors::CargoResultExt;

#[derive(Eq, PartialEq, Hash, Ord, PartialOrd, Clone, Debug)]
pub enum Platform {
Name(String),
Cfg(CfgExpr),
}

impl Platform {
pub fn matches(&self, name: &str, cfg: Option<&[Cfg]>) -> bool {
match *self {
Platform::Name(ref p) => p == name,
Platform::Cfg(ref p) => match cfg {
Some(cfg) => p.matches(cfg),
None => false,
},
}
}
}

impl ser::Serialize for Platform {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
self.to_string().serialize(s)
}
}

impl FromStr for Platform {
type Err = failure::Error;

fn from_str(s: &str) -> CargoResult<Platform> {
if s.starts_with("cfg(") && s.ends_with(')') {
let s = &s[4..s.len() - 1];
let p = s.parse().map(Platform::Cfg).chain_err(|| {
failure::format_err!("failed to parse `{}` as a cfg expression", s)
})?;
Ok(p)
} else {
Ok(Platform::Name(s.to_string()))
}
}
}

impl fmt::Display for Platform {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Platform::Name(ref n) => n.fmt(f),
Platform::Cfg(ref e) => write!(f, "cfg({})", e),
}
}
}

#[derive(Eq, PartialEq, Hash, Ord, PartialOrd, Clone, Debug)]
pub enum Cfg {
Name(String),
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::core::{GitReference, PackageIdSpec, SourceId, WorkspaceConfig, Worksp
use crate::sources::{CRATES_IO_INDEX, CRATES_IO_REGISTRY};
use crate::util::errors::{CargoResult, CargoResultExt, ManifestError};
use crate::util::paths;
use crate::util::{self, validate_package_name, Config, ToUrl};
use crate::util::{self, validate_package_name, Config, ToUrl, Platform};

mod targets;
use self::targets::targets;
Expand Down