diff --git a/src/serde.rs b/src/serde.rs index 06eceb68..1fcc7d87 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -1,4 +1,4 @@ -use crate::{Version, VersionReq}; +use crate::{Comparator, Version, VersionReq}; use core::fmt; use serde::de::{Deserialize, Deserializer, Error, Visitor}; use serde::ser::{Serialize, Serializer}; @@ -21,6 +21,15 @@ impl Serialize for VersionReq { } } +impl Serialize for Comparator { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.collect_str(self) + } +} + impl<'de> Deserialize<'de> for Version { fn deserialize(deserializer: D) -> Result where @@ -72,3 +81,29 @@ impl<'de> Deserialize<'de> for VersionReq { deserializer.deserialize_str(VersionReqVisitor) } } + +impl<'de> Deserialize<'de> for Comparator { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + struct ComparatorVisitor; + + impl<'de> Visitor<'de> for ComparatorVisitor { + type Value = Comparator; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("semver comparator") + } + + fn visit_str(self, string: &str) -> Result + where + E: Error, + { + string.parse().map_err(Error::custom) + } + } + + deserializer.deserialize_str(ComparatorVisitor) + } +}