cob: Fix `serde` instances for `ObjectId`
The type now serializes the same as `Oid`.
This commit is contained in:
parent
f58af8fef4
commit
70f0cc354d
|
|
@ -23,7 +23,8 @@ pub enum ParseObjectId {
|
|||
}
|
||||
|
||||
/// The id of an object
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[serde(transparent)]
|
||||
pub struct ObjectId(Oid);
|
||||
|
||||
impl FromStr for ObjectId {
|
||||
|
|
@ -73,26 +74,6 @@ impl fmt::Display for ObjectId {
|
|||
}
|
||||
}
|
||||
|
||||
impl Serialize for ObjectId {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
serializer.serialize_bytes(self.0.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for ObjectId {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let raw = <&[u8]>::deserialize(deserializer)?;
|
||||
let oid = Oid::try_from(raw).map_err(serde::de::Error::custom)?;
|
||||
Ok(ObjectId(oid))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&ObjectId> for Component<'_> {
|
||||
fn from(id: &ObjectId) -> Self {
|
||||
let refstr = RefString::from(*id);
|
||||
|
|
@ -108,3 +89,19 @@ impl From<ObjectId> for RefString {
|
|||
.expect("collaborative object id's are valid ref strings")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_serde() {
|
||||
let id = ObjectId::from_str("3ad84420bd882f983c2f9b605e7a68f5bdf95f5c").unwrap();
|
||||
|
||||
assert_eq!(
|
||||
serde_json::to_string(&id).unwrap(),
|
||||
serde_json::to_string(&id.0).unwrap()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue