Skip to content

Commit

Permalink
feat: implement Serialize for Handle<T> and UntypedHandle. (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed Feb 18, 2023
1 parent 8751bdb commit 7e00c6e
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions crates/bones_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,40 @@ impl<'de, T: TypeUlid> serde::Deserialize<'de> for Handle<T> {
}
}

impl serde::Serialize for UntypedHandle {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&format!(
"/{}{}",
self.path.path.to_str().expect("Non-unicode path"),
self.path
.label
.as_ref()
.map(|x| format!("#{}", x))
.unwrap_or_default()
))
}
}

impl<T: TypeUlid> serde::Serialize for Handle<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&format!(
"/{}{}",
self.path.path.to_str().expect("Non-unicode path"),
self.path
.label
.as_ref()
.map(|x| format!("#{}", x))
.unwrap_or_default()
))
}
}

struct UntypedHandleVisitor;
impl<'de> serde::de::Visitor<'de> for UntypedHandleVisitor {
type Value = UntypedHandle;
Expand Down

0 comments on commit 7e00c6e

Please sign in to comment.