fixed hint serialization

This commit is contained in:
Candifloss 2024-12-02 22:51:57 +05:30
parent 27e0d3550d
commit afe7f6c908
2 changed files with 19 additions and 30 deletions

1
.gitignore vendored
View File

@ -20,3 +20,4 @@ Cargo.lock
/target /target
/.idea /.idea
error.log

View File

@ -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 serde::ser::{Serialize, Serializer, SerializeMap};
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::TryInto; // Import TryInto use std::convert::TryInto; // Import TryInto
use serde_json::{Map, Value as JsonValue}; // Alias serde_json::Value to JsonValue use serde_json::{Map, Value as JsonValue}; // Alias serde_json::Value to JsonValue
use zvariant::{OwnedValue, Value as ZValue}; // Alias zvariant::Value to ZValue use zvariant::{OwnedValue, Value as ZValue}; // Alias zvariant::Value to ZValue
pub fn serialize_actions<S>(actions: &[String], serializer: S) -> Result<S::Ok, S::Error> pub fn serialize_actions<S>(actions: &[String], serializer: S) -> Result<S::Ok, S::Error>
where where
S: Serializer, S: Serializer,
@ -34,7 +20,6 @@ where
map.serialize(serializer) map.serialize(serializer)
} }
pub fn serialize_hints<S>( pub fn serialize_hints<S>(
hints: &HashMap<String, OwnedValue>, hints: &HashMap<String, OwnedValue>,
serializer: S, serializer: S,
@ -51,8 +36,6 @@ where
map.end() map.end()
} }
// A custom struct to handle serialization of OwnedValue
struct HintValueSerializer<'a>(&'a OwnedValue);
/* /*
impl<'a> Serialize for HintValueSerializer<'a> { impl<'a> Serialize for HintValueSerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@ -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> { impl<'a> Serialize for HintValueSerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
@ -78,26 +65,27 @@ impl<'a> Serialize for HintValueSerializer<'a> {
{ {
let signature = self.0.value_signature().to_string(); let signature = self.0.value_signature().to_string();
// Extract the raw value using downcast_ref for common types // Extract the raw value correctly
let raw_value = if let Ok(v) = self.0.downcast_ref::<&u8>() { let raw_value = if let Ok(v) = self.0.downcast_ref::<u8>() {
serde_json::Value::Number(serde_json::Number::from(*v)) JsonValue::from(v) // Removed *v
} else if let Ok(v) = self.0.downcast_ref::<&i32>() { } else if let Ok(v) = self.0.downcast_ref::<i32>() {
serde_json::Value::Number(serde_json::Number::from(*v)) JsonValue::from(v) // Removed *v
} else if let Ok(v) = self.0.downcast_ref::<&u64>() { } else if let Ok(v) = self.0.downcast_ref::<u64>() {
serde_json::Value::Number(serde_json::Number::from(*v)) JsonValue::from(v) // Removed *v
} else if let Ok(v) = self.0.downcast_ref::<bool>() { } else if let Ok(v) = self.0.downcast_ref::<bool>() {
serde_json::Value::Bool(v) JsonValue::Bool(v) // Removed *v
} else if let Ok(v) = self.0.downcast_ref::<&String>() { } else if let Ok(v) = self.0.downcast_ref::<String>() {
serde_json::Value::String(v.to_string()) JsonValue::String(v.clone())
} else if let Ok(v) = self.0.downcast_ref::<&str>() {
JsonValue::String(v.to_string())
} else { } 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))?; let mut map = serializer.serialize_map(Some(2))?;
map.serialize_entry("signature", &signature)?; map.serialize_entry("signature", &signature)?;
map.serialize_entry("value", &raw_value)?; map.serialize_entry("value", &raw_value)?;
map.end() map.end()
} }
} }