Skip to content

Commit

Permalink
Support deserialization into newtype structs (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwalker authored Nov 12, 2023
1 parent fce4b46 commit 78df42d
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/src/types/serde/typ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ impl<'de> Deserializer<'de> for BoltTypeDeserializer<'de> {
BoltType::DateTimeZoneId(dtz) if name == "Timezone" => {
visitor.visit_newtype_struct(BorrowedStrDeserializer::new(dtz.tz_id()))
}
_ => self.unexpected(visitor),
_ => visitor.visit_newtype_struct(self),
}
}

Expand Down Expand Up @@ -2169,4 +2169,25 @@ mod tests {

assert_eq!(actual, Frobnicate::Foo);
}

#[test]
fn deserialize_tuple_struct() {
#[derive(Deserialize, Debug)]
pub struct MyString(String);

#[derive(Deserialize, Debug)]
pub struct MyThing {
my_string: MyString,
}

let value = BoltType::from("Frobnicate");
let value = [(BoltString::from("my_string"), value)]
.into_iter()
.collect::<BoltMap>();
let value = BoltType::Map(value);

let actual = value.to::<MyThing>().unwrap();

assert_eq!(actual.my_string.0, "Frobnicate");
}
}

0 comments on commit 78df42d

Please sign in to comment.