clippy and comments

This commit is contained in:
Candifloss 2024-12-03 11:37:09 +05:30
parent afe7f6c908
commit 829a8741ee
3 changed files with 16 additions and 36 deletions

View File

@ -5,7 +5,6 @@ use rson_rs::ser::to_string as rson_string;
impl Notification {
pub fn rson(&self) -> Result<String, RsonError> {
rson_string(self)
.map_err(|e| RsonError::Message(format!("RSON serialization error: {}", e)))
rson_string(self).map_err(|e| RsonError::Message(format!("RSON serialization error: {e}")))
}
}

View File

@ -1,8 +1,7 @@
use serde::ser::{Serialize, Serializer, SerializeMap};
use serde::ser::{Serialize, SerializeMap, Serializer};
use serde_json::{Map, Value};
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
use zvariant::OwnedValue;
pub fn serialize_actions<S>(actions: &[String], serializer: S) -> Result<S::Ok, S::Error>
where
@ -13,7 +12,7 @@ where
// Actions are in pairs: [id, label, id, label, ...]
for pair in actions.chunks(2) {
if let [id, label] = pair {
map.insert(id.clone(), JsonValue::String(label.clone()));
map.insert(id.clone(), Value::String(label.clone()));
}
}
@ -36,50 +35,32 @@ where
map.end()
}
/*
impl<'a> Serialize for HintValueSerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
// Access the signature and value parts of the OwnedValue
let signature = self.0.value_signature().to_string();
let value = &self.0;
// Serialize them as a map with "signature" and "value" fields
let mut map = serializer.serialize_map(Some(2))?;
map.serialize_entry("signature", &signature)?;
map.serialize_entry("value", value)?;
map.end()
}
}*/
// A custom struct to handle serialization of OwnedValue
struct HintValueSerializer<'a>(&'a OwnedValue);
impl<'a> Serialize for HintValueSerializer<'a> {
impl Serialize for HintValueSerializer<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
// Signature
let signature = self.0.value_signature().to_string();
// Extract the raw value correctly
let raw_value = if let Ok(v) = self.0.downcast_ref::<u8>() {
JsonValue::from(v) // Removed *v
Value::from(v)
} else if let Ok(v) = self.0.downcast_ref::<i32>() {
JsonValue::from(v) // Removed *v
Value::from(v)
} else if let Ok(v) = self.0.downcast_ref::<u64>() {
JsonValue::from(v) // Removed *v
Value::from(v)
} else if let Ok(v) = self.0.downcast_ref::<bool>() {
JsonValue::Bool(v) // Removed *v
Value::Bool(v)
} else if let Ok(v) = self.0.downcast_ref::<String>() {
JsonValue::String(v.clone())
Value::String(v.clone())
} else if let Ok(v) = self.0.downcast_ref::<&str>() {
JsonValue::String(v.to_string())
Value::String(v.to_string())
} else {
JsonValue::Null // Unsupported types fallback to Null
Value::Null // Unsupported types fallback to Null
};
// Serialize the final structure as a map

View File

@ -134,8 +134,8 @@ async fn main() -> Result<()> {
"r" => {
// Print the rson version
match notif.rson() {
Ok(rson_string) => println!("{}", rson_string),
Err(e) => eprintln!("Failed to convert to RSON: {}", e),
Ok(rson_string) => println!("{rson_string}"),
Err(e) => eprintln!("Failed to convert to RSON: {e}"),
}
}
"p" => {