From afe7f6c908a65ddb770f4cbb8a445239c04c2bd3 Mon Sep 17 00:00:00 2001 From: candifloss Date: Mon, 2 Dec 2024 22:51:57 +0530 Subject: [PATCH] fixed hint serialization --- .gitignore | 1 + src/formats/serde.rs | 48 +++++++++++++++++--------------------------- 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 922c946..695a23c 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ Cargo.lock /target /.idea +error.log diff --git a/src/formats/serde.rs b/src/formats/serde.rs index 40ad3db..2666461 100644 --- a/src/formats/serde.rs +++ b/src/formats/serde.rs @@ -1,23 +1,9 @@ -/*use serde::ser::{Serialize, Serializer, SerializeMap}; -use std::collections::HashMap; -//use zvariant::OwnedValue; -//use std::hash::BuildHasher; -//use serde::ser::SerializeMap; -use serde_json::{Map,Value}; -use zvariant::SerializeValue; - -use std::convert::TryInto; -use zvariant::{OwnedValue, Value};*/ - - use serde::ser::{Serialize, Serializer, SerializeMap}; use std::collections::HashMap; use std::convert::TryInto; // Import TryInto use serde_json::{Map, Value as JsonValue}; // Alias serde_json::Value to JsonValue use zvariant::{OwnedValue, Value as ZValue}; // Alias zvariant::Value to ZValue - - pub fn serialize_actions(actions: &[String], serializer: S) -> Result where S: Serializer, @@ -34,7 +20,6 @@ where map.serialize(serializer) } - pub fn serialize_hints( hints: &HashMap, serializer: S, @@ -51,8 +36,6 @@ where map.end() } -// A custom struct to handle serialization of OwnedValue -struct HintValueSerializer<'a>(&'a OwnedValue); /* impl<'a> Serialize for HintValueSerializer<'a> { fn serialize(&self, serializer: S) -> Result @@ -71,6 +54,10 @@ impl<'a> Serialize for HintValueSerializer<'a> { } }*/ +// A custom struct to handle serialization of OwnedValue +struct HintValueSerializer<'a>(&'a OwnedValue); + + impl<'a> Serialize for HintValueSerializer<'a> { fn serialize(&self, serializer: S) -> Result where @@ -78,26 +65,27 @@ impl<'a> Serialize for HintValueSerializer<'a> { { let signature = self.0.value_signature().to_string(); - // Extract the raw value using downcast_ref for common types - let raw_value = if let Ok(v) = self.0.downcast_ref::<&u8>() { - serde_json::Value::Number(serde_json::Number::from(*v)) - } else if let Ok(v) = self.0.downcast_ref::<&i32>() { - serde_json::Value::Number(serde_json::Number::from(*v)) - } else if let Ok(v) = self.0.downcast_ref::<&u64>() { - serde_json::Value::Number(serde_json::Number::from(*v)) + // Extract the raw value correctly + let raw_value = if let Ok(v) = self.0.downcast_ref::() { + JsonValue::from(v) // Removed *v + } else if let Ok(v) = self.0.downcast_ref::() { + JsonValue::from(v) // Removed *v + } else if let Ok(v) = self.0.downcast_ref::() { + JsonValue::from(v) // Removed *v } else if let Ok(v) = self.0.downcast_ref::() { - serde_json::Value::Bool(v) - } else if let Ok(v) = self.0.downcast_ref::<&String>() { - serde_json::Value::String(v.to_string()) + JsonValue::Bool(v) // Removed *v + } else if let Ok(v) = self.0.downcast_ref::() { + JsonValue::String(v.clone()) + } else if let Ok(v) = self.0.downcast_ref::<&str>() { + JsonValue::String(v.to_string()) } else { - serde_json::Value::Null // Unsupported types fallback to Null + JsonValue::Null // Unsupported types fallback to Null }; - // Serialize the final structure as a map with "signature" and "value" + // Serialize the final structure as a map let mut map = serializer.serialize_map(Some(2))?; map.serialize_entry("signature", &signature)?; map.serialize_entry("value", &raw_value)?; map.end() } } -