Skip to content

Commit

Permalink
Fix untagged enum deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Apr 29, 2022
1 parent a06d229 commit 7d2f4e6
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,36 @@ macro_rules! create_bool_or_string_de {
create_bool_or_string_de!(true_or_always<true, "always">);
create_bool_or_string_de!(false_or_never<false, "never">);

macro_rules! named_unit_variant {
($variant:ident) => {
pub fn $variant<'de, D>(deserializer: D) -> Result<(), D::Error>
where
D: serde::Deserializer<'de>,
{
struct V;
impl<'de> serde::de::Visitor<'de> for V {
type Value = ();
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(concat!("\"", stringify!($variant), "\""))
}
fn visit_str<E: serde::de::Error>(self, value: &str) -> Result<Self::Value, E> {
if value == stringify!($variant) {
Ok(())
} else {
Err(E::invalid_value(serde::de::Unexpected::Str(value), &self))
}
}
}
deserializer.deserialize_str(V)
}
};
}

mod de_unit_v {
named_unit_variant!(all);
named_unit_variant!(skip_trivial);
}

#[derive(Deserialize, Debug, Clone, Copy)]
#[serde(rename_all = "snake_case")]
enum SnippetScopeDef {
Expand Down Expand Up @@ -1332,21 +1362,21 @@ enum CallableCompletionDef {
}

#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
#[serde(untagged)]
enum CargoFeatures {
#[serde(deserialize_with = "de_unit_v::all")]
All,
Listed(Vec<String>),
}

#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
#[serde(untagged)]
enum LifetimeElisionDef {
#[serde(deserialize_with = "true_or_always")]
Always,
#[serde(deserialize_with = "false_or_never")]
Never,
#[serde(deserialize_with = "de_unit_v::skip_trivial")]
SkipTrivial,
}

Expand Down

0 comments on commit 7d2f4e6

Please sign in to comment.