Skip to content

Commit

Permalink
Support visiting enums
Browse files Browse the repository at this point in the history
  • Loading branch information
ModProg committed Dec 30, 2022
1 parent 47190f5 commit b8d7777
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/de.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde::{forward_to_deserialize_any, de};
use serde::{forward_to_deserialize_any, de::{self, VariantAccess}};
use std::collections::BTreeMap;
use std::error::Error;
use std::fmt;
Expand Down Expand Up @@ -289,6 +289,14 @@ impl<'de> de::Visitor<'de> for ValueVisitor {
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Value, E> {
Ok(Value::Bytes(v))
}

fn visit_enum<A: de::EnumAccess<'de>>(self, data: A) -> Result<Self::Value, A::Error> {
let mut map = BTreeMap::new();
let (key, value) = data.variant()?;
let value = value.newtype_variant().map_or_else(|_| Value::Unit, Value::Newtype);
map.insert(key, value);
Ok(Value::Map(map))
}
}

impl<'de> de::Deserialize<'de> for Value {
Expand Down

0 comments on commit b8d7777

Please sign in to comment.