Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,55 @@ impl TomlProfile {
}
}

#[derive(Clone, Debug, Serialize, Eq, PartialEq)]
#[serde(untagged)]
pub enum StringOrU32 {
String(String),
U32(u32),
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the same code for TomlOptLevel as well? It already does something similar, but in a slightly different way. Maybe it's better even to go in the opposite direction and reuse that logic here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, or perhaps the best way to rewrite both with #[serde(deserialize_with="string_or_int")]?


impl<'de> de::Deserialize<'de> for StringOrU32 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
struct Visitor;

impl<'de> de::Visitor<'de> for Visitor {
type Value = StringOrU32;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("an integer or a string")
}

fn visit_i64<E>(self, u: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(StringOrU32::U32(u as u32))
}

fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(StringOrU32::String(s.to_string()))
}
}

deserializer.deserialize_any(Visitor)
}
}

impl StringOrU32 {
fn to_string(&self) -> String {
match self {
StringOrU32::String(v) => v.clone(),
StringOrU32::U32(v) => format!("{}", v),
}
}
}

#[derive(Clone, Debug, Serialize, Eq, PartialEq)]
#[serde(untagged)]
pub enum StringOrBool {
Expand Down Expand Up @@ -595,7 +644,7 @@ pub struct TomlProject {
license_file: Option<String>,
repository: Option<String>,
metadata: Option<toml::Value>,
edition: Option<String>,
edition: Option<StringOrU32>,
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -761,6 +810,7 @@ impl TomlManifest {
.require(Feature::edition())
.chain_err(|| "editions are unstable")?;
edition
.to_string()
.parse()
.chain_err(|| "failed to parse the `edition` key")?
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/doc/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ cargo-features = ["edition"]

[package]
...
edition = "2018"
edition = 2018
```


Expand Down
30 changes: 30 additions & 0 deletions tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,36 @@ fn test_edition() {
);
}

#[test]
fn test_edition_integral() {
let p = project("foo")
.file(
"Cargo.toml",
r#"
cargo-features = ["edition"]
[package]
name = "foo"
version = "0.0.1"
authors = []
edition = 2018
"#,
)
.file("src/lib.rs", r#" "#)
.build();

assert_that(
p.cargo("build").arg("-v").masquerade_as_nightly_cargo(),
execs()
// --edition is still in flux and we're not passing -Zunstable-options
// from Cargo so it will probably error. Only partially match the output
// until stuff stabilizes
.with_stderr_contains("\
[COMPILING] foo v0.0.1 ([..])
[RUNNING] `rustc [..]--edition=2018 [..]
"),
);
}

#[test]
fn test_edition_missing() {
// no edition = 2015
Expand Down