Skip to main content

proto_types/
any_impls.rs

1use core::cmp::Ordering;
2
3use crate::Any;
4
5impl Ord for Any {
6	fn cmp(&self, other: &Self) -> Ordering {
7		match self.type_url.cmp(&other.type_url) {
8			Ordering::Equal => self.value.cmp(&other.value),
9			other => other,
10		}
11	}
12}
13
14impl PartialOrd for Any {
15	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
16		Some(self.cmp(other))
17	}
18}
19
20#[cfg(feature = "serde")]
21mod serde {
22	use core::fmt;
23
24	use base64::{Engine, prelude::BASE64_STANDARD};
25	use serde::{Deserialize, Deserializer, Serialize, Serializer, de, ser::SerializeStruct};
26
27	use crate::{Any, String};
28
29	impl Serialize for Any {
30		fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
31		where
32			S: Serializer,
33		{
34			let mut state = serializer.serialize_struct("Any", 2)?;
35			state.serialize_field("@type", &self.type_url)?;
36			state.serialize_field("value", &BASE64_STANDARD.encode(&self.value))?;
37			state.end()
38		}
39	}
40
41	impl<'de> Deserialize<'de> for Any {
42		fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
43		where
44			D: Deserializer<'de>,
45		{
46			// Define a visitor to expect a map (JSON object)
47			struct AnyVisitor;
48
49			impl<'de> de::Visitor<'de> for AnyVisitor {
50				type Value = Any;
51
52				fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
53					formatter.write_str(
54						"struct Any with fields `@type` (string) and `value` (base64-encoded string)",
55					)
56				}
57
58				fn visit_map<V>(self, mut map: V) -> Result<Any, V::Error>
59				where
60					V: de::MapAccess<'de>,
61				{
62					let mut type_url: Option<String> = None;
63					let mut value_base64: Option<String> = None;
64
65					// Loop through fields, expecting "@type" and "value"
66					while let Some(key) = map.next_key::<String>()? {
67						match key.as_str() {
68							"@type" => {
69								if type_url.is_some() {
70									return Err(de::Error::duplicate_field("@type"));
71								}
72								type_url = Some(map.next_value()?);
73							}
74							"value" => {
75								if value_base64.is_some() {
76									return Err(de::Error::duplicate_field("value"));
77								}
78								value_base64 = Some(map.next_value()?);
79							}
80							_ => {
81								// Ignore any other fields
82								let _ = map.next_value::<de::IgnoredAny>()?;
83							}
84						}
85					}
86
87					// Check that required fields were present
88					let type_url = type_url.ok_or_else(|| de::Error::missing_field("@type"))?;
89					let value_base64 =
90						value_base64.ok_or_else(|| de::Error::missing_field("value"))?;
91
92					// Decode base64 value
93					let value = BASE64_STANDARD
94						.decode(&value_base64)
95						.map_err(de::Error::custom)?;
96
97					Ok(Any { type_url, value })
98				}
99
100				// If the JSON is not an object (e.g., if it was an unwrapped WKT like a string),
101				// this minimal implementation will just return an error.
102			}
103
104			deserializer.deserialize_map(AnyVisitor) // Instruct serde to expect a map/object
105		}
106	}
107}