Skip to content

Commit

Permalink
Use serde's error message option to avoid implementing Deserialize.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mukund Lakshman committed Mar 19, 2021
1 parent c9ce0ec commit 54742ce
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 80 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ percent-encoding = "2.0"
rustfix = "0.5.0"
same-file = "1"
semver = { version = "0.10", features = ["serde"] }
serde = { version = "1.0.82", features = ["derive"] }
serde = { version = "1.0.123", features = ["derive"] }
serde_ignored = "0.1.0"
serde_json = { version = "1.0.30", features = ["raw_value"] }
shell-escape = "0.1.4"
Expand Down
81 changes: 4 additions & 77 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,53 +419,13 @@ impl ser::Serialize for TomlOptLevel {
}
}

#[derive(Clone, Debug, Serialize, Eq, PartialEq)]
#[serde(untagged)]
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(untagged, expecting = "expected a boolean or an integer")]
pub enum U32OrBool {
U32(u32),
Bool(bool),
}

impl<'de> de::Deserialize<'de> for U32OrBool {
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 = U32OrBool;

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

fn visit_bool<E>(self, b: bool) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(U32OrBool::Bool(b))
}

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

fn visit_u64<E>(self, u: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(U32OrBool::U32(u as u32))
}
}

deserializer.deserialize_any(Visitor)
}
}

#[derive(Deserialize, Serialize, Clone, Debug, Default, Eq, PartialEq)]
#[serde(default, rename_all = "kebab-case")]
pub struct TomlProfile {
Expand Down Expand Up @@ -770,46 +730,13 @@ impl<'de> de::Deserialize<'de> for StringOrVec {
}
}

#[derive(Clone, Debug, Serialize, Eq, PartialEq)]
#[serde(untagged)]
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(untagged, expecting = "expected a boolean or a string")]
pub enum StringOrBool {
String(String),
Bool(bool),
}

impl<'de> de::Deserialize<'de> for StringOrBool {
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 = StringOrBool;

fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a boolean or a string")
}

fn visit_bool<E>(self, b: bool) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(StringOrBool::Bool(b))
}

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

deserializer.deserialize_any(Visitor)
}
}

#[derive(PartialEq, Clone, Debug, Serialize)]
#[serde(untagged)]
pub enum VecStringOrBool {
Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/bad_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ fn bad_debuginfo() {
error: failed to parse manifest at `[..]`
Caused by:
invalid type: string \"a\", expected a boolean or an integer for [..]
expected a boolean or an integer for [..]
",
)
.run();
Expand Down Expand Up @@ -1338,7 +1338,7 @@ fn bad_opt_level() {
error: failed to parse manifest at `[..]`
Caused by:
invalid type: integer `3`, expected a boolean or a string for key [..]
expected a boolean or a string for key [..]
",
)
.run();
Expand Down

0 comments on commit 54742ce

Please sign in to comment.