diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/semver/src/serde.rs | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/semver/src/serde.rs')
-rw-r--r-- | third_party/rust/semver/src/serde.rs | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/third_party/rust/semver/src/serde.rs b/third_party/rust/semver/src/serde.rs new file mode 100644 index 0000000000..1fcc7d87f6 --- /dev/null +++ b/third_party/rust/semver/src/serde.rs @@ -0,0 +1,109 @@ +use crate::{Comparator, Version, VersionReq}; +use core::fmt; +use serde::de::{Deserialize, Deserializer, Error, Visitor}; +use serde::ser::{Serialize, Serializer}; + +impl Serialize for Version { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.collect_str(self) + } +} + +impl Serialize for VersionReq { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.collect_str(self) + } +} + +impl Serialize for Comparator { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.collect_str(self) + } +} + +impl<'de> Deserialize<'de> for Version { + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> + where + D: Deserializer<'de>, + { + struct VersionVisitor; + + impl<'de> Visitor<'de> for VersionVisitor { + type Value = Version; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("semver version") + } + + fn visit_str<E>(self, string: &str) -> Result<Self::Value, E> + where + E: Error, + { + string.parse().map_err(Error::custom) + } + } + + deserializer.deserialize_str(VersionVisitor) + } +} + +impl<'de> Deserialize<'de> for VersionReq { + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> + where + D: Deserializer<'de>, + { + struct VersionReqVisitor; + + impl<'de> Visitor<'de> for VersionReqVisitor { + type Value = VersionReq; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("semver version") + } + + fn visit_str<E>(self, string: &str) -> Result<Self::Value, E> + where + E: Error, + { + string.parse().map_err(Error::custom) + } + } + + deserializer.deserialize_str(VersionReqVisitor) + } +} + +impl<'de> Deserialize<'de> for Comparator { + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> + 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<E>(self, string: &str) -> Result<Self::Value, E> + where + E: Error, + { + string.parse().map_err(Error::custom) + } + } + + deserializer.deserialize_str(ComparatorVisitor) + } +} |