Skip to content

Commit

Permalink
Support deserialization from string via from_str()
Browse files Browse the repository at this point in the history
  • Loading branch information
mqudsi committed Apr 27, 2024
1 parent 1e27b71 commit 20292b2
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ impl<'de> de::Visitor<'de> for SizeVisitor {
})
}
}

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
Size::from_str(value)
.map_err(|_| E::custom(format!("Invalid size: \"{value}\"")))
}
}

impl Serialize for Size {
Expand All @@ -73,8 +81,7 @@ impl<'de> Deserialize<'de> for Size {
where
D: Deserializer<'de>,
{
// Name is misleading; does not mean only SizeVisitor::visit_i64 is called!
deserializer.deserialize_i64(SizeVisitor)
deserializer.deserialize_any(SizeVisitor)
}
}

Expand Down

0 comments on commit 20292b2

Please sign in to comment.