From 45bf3a87b96e435ff2a75a68be4e7956b0f85479 Mon Sep 17 00:00:00 2001 From: candifloss Date: Fri, 27 Sep 2024 16:28:22 +0530 Subject: [PATCH 01/14] starting to fix hints serialization --- src/formats/serde.rs | 30 ++++++++++++++++++++++++++---- src/notification.rs | 2 +- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/formats/serde.rs b/src/formats/serde.rs index da73200..489841c 100644 --- a/src/formats/serde.rs +++ b/src/formats/serde.rs @@ -1,8 +1,8 @@ use serde::{Serialize, Serializer}; -/* use std::collections::HashMap; -use std::hash::BuildHasher; -use zvariant::OwnedValue; */ +use zvariant::OwnedValue; +//use std::hash::BuildHasher; +use serde::ser::SerializeMap; pub fn serialize_actions(actions: &[String], serializer: S) -> Result where @@ -20,6 +20,27 @@ where map.serialize(serializer) } +/* +pub fn serialize_hints(hints: &HashMap, serializer: S) -> Result +where + S: serde::Serializer, +{ + let mut map = serializer.serialize_map(Some(hints.len()))?; + + for (key, value) in hints { + // Try to cast the value to a string, otherwise fallback to debug format + let value_str = match value.downcast_ref::() { + Ok(s) => s.clone(), // Successfully cast to String + Err(_) => format!("{:?}", value), // Fallback for non-String values + }; + + map.serialize_entry(key, &value_str)?; + } + + map.end() +} +*/ + /* pub fn serialize_hints( hints: &HashMap, @@ -38,4 +59,5 @@ where map.serialize(serializer) } -*/ + +*/ \ No newline at end of file diff --git a/src/notification.rs b/src/notification.rs index 538faa6..d278c31 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -1,4 +1,4 @@ -use crate::formats::serde::serialize_actions; +use crate::formats::serde::serialize_actions;//,serialize_hints}; use serde::Serialize; use std::collections::HashMap; use zbus::{message::Body, Result}; From 1e51c830679d8009d246a0e3add113d472af4ede Mon Sep 17 00:00:00 2001 From: candifloss Date: Fri, 27 Sep 2024 23:56:53 +0530 Subject: [PATCH 02/14] minor fixes --- Cargo.toml | 2 +- src/formats/rson.rs | 5 +++-- src/formats/serde.rs | 4 ++-- src/main.rs | 2 +- src/notification.rs | 2 +- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5e6d566..8abf818 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "snot" -version = "0.1.1" +version = "0.1.3" edition = "2021" authors = ["candifloss "] diff --git a/src/formats/rson.rs b/src/formats/rson.rs index fd4027d..8f03db6 100644 --- a/src/formats/rson.rs +++ b/src/formats/rson.rs @@ -1,10 +1,11 @@ // This module deals with converting the notification object into rson format, which can be used instead of json if preferred use crate::notification::Notification; -use rson_rs::ser::to_string as rson_string; use rson_rs::de::Error as RsonError; +use rson_rs::ser::to_string as rson_string; impl Notification { pub fn rson(&self) -> Result { - 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))) } } diff --git a/src/formats/serde.rs b/src/formats/serde.rs index 489841c..66bf771 100644 --- a/src/formats/serde.rs +++ b/src/formats/serde.rs @@ -1,6 +1,6 @@ use serde::{Serialize, Serializer}; use std::collections::HashMap; -use zvariant::OwnedValue; +use zvariant::OwnedValue; //use std::hash::BuildHasher; use serde::ser::SerializeMap; @@ -60,4 +60,4 @@ where map.serialize(serializer) } -*/ \ No newline at end of file +*/ diff --git a/src/main.rs b/src/main.rs index bdf9847..edcf5a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ use zbus::{message::Body, Connection, Result}; const SERVER_NAME: &str = "SNot"; // Server software name const VENDOR: &str = "candifloss.cc"; // Server software vendor -const VERSION: &str = "0.1.2"; // Server software version +const VERSION: &str = env!("CARGO_PKG_VERSION"); // Server software version, from Cargo.toml const SPEC_VERSION: &str = "0.42"; // DBus specification version const NOTIF_INTERFACE: &str = "org.freedesktop.Notifications"; // DBus interface name diff --git a/src/notification.rs b/src/notification.rs index d278c31..4c56bec 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -1,4 +1,4 @@ -use crate::formats::serde::serialize_actions;//,serialize_hints}; +use crate::formats::serde::serialize_actions; //,serialize_hints}; use serde::Serialize; use std::collections::HashMap; use zbus::{message::Body, Result}; From 21002f6521d2eeb8b349740efb2640551aa8f473 Mon Sep 17 00:00:00 2001 From: candifloss Date: Sat, 28 Sep 2024 16:49:48 +0530 Subject: [PATCH 03/14] commit --- src/formats/serde.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/formats/serde.rs b/src/formats/serde.rs index 66bf771..f18df96 100644 --- a/src/formats/serde.rs +++ b/src/formats/serde.rs @@ -10,9 +10,9 @@ where { let mut map = serde_json::Map::new(); - // Assuming actions are in pairs: [id, label, id, label, ...] - for chunk in actions.chunks(2) { - if let [id, label] = chunk { + // Actions are in pairs: [id, label, id, label, ...] + for pair in actions.chunks(2) { + if let [id, label] = pair { map.insert(id.clone(), serde_json::Value::String(label.clone())); } } From 58aed0897d0beae0c81b3a89b8d16c73c49cf1f7 Mon Sep 17 00:00:00 2001 From: candifloss Date: Mon, 14 Oct 2024 16:38:55 +0530 Subject: [PATCH 04/14] attempt - hint serialize --- src/formats/serde.rs | 18 ++++++++++++++++++ src/notification.rs | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/formats/serde.rs b/src/formats/serde.rs index f18df96..72f164f 100644 --- a/src/formats/serde.rs +++ b/src/formats/serde.rs @@ -20,6 +20,24 @@ where map.serialize(serializer) } +pub fn serialize_hints(hints: &HashMap, serializer: Serializer) -> Result +{ + let mut hint_map = serde_json::Map::new(); + + for hint in hints { + if let (hint_name, hint_val) = hint { + let mut h = serde_json::Map::new(); + let sig = hint_val.value_signature(); + let val = hint_val.try_clone(); + h.insert(sig.to_string(), serde_json::Value::String(val)); + + hint_map.insert(hint_name.clone(), serde_json::Value::Object(h.clone())); + } + } + + hint_map.serialize(serializer) +} + /* pub fn serialize_hints(hints: &HashMap, serializer: S) -> Result where diff --git a/src/notification.rs b/src/notification.rs index 4c56bec..2b73ef2 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -1,4 +1,4 @@ -use crate::formats::serde::serialize_actions; //,serialize_hints}; +use crate::formats::serde::{serialize_actions,serialize_hints}; use serde::Serialize; use std::collections::HashMap; use zbus::{message::Body, Result}; @@ -21,7 +21,7 @@ pub struct Notification { #[serde(serialize_with = "serialize_actions")] actions: Vec, // Useful extra data - notif type, urgency, notif sound, icon data, etc. - //#[serde(serialize_with = "serialize_hints")] + #[serde(serialize_with = "serialize_hints")] hints: HashMap, // Seconds till this notif expires. Optional expir_timeout: i32, From e71891343c4f2b0a2664583167eb30253e95fc15 Mon Sep 17 00:00:00 2001 From: candifloss Date: Thu, 17 Oct 2024 15:02:17 +0530 Subject: [PATCH 05/14] remove actions --- src/formats/serde.rs | 8 +++++--- src/main.rs | 2 +- src/notification.rs | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/formats/serde.rs b/src/formats/serde.rs index 72f164f..6023240 100644 --- a/src/formats/serde.rs +++ b/src/formats/serde.rs @@ -20,8 +20,10 @@ where map.serialize(serializer) } -pub fn serialize_hints(hints: &HashMap, serializer: Serializer) -> Result -{ +pub fn serialize_hints( + hints: &HashMap, + serializer: Serializer, +) -> Result { let mut hint_map = serde_json::Map::new(); for hint in hints { @@ -30,7 +32,7 @@ pub fn serialize_hints(hints: &HashMap, serializer: Seria let sig = hint_val.value_signature(); let val = hint_val.try_clone(); h.insert(sig.to_string(), serde_json::Value::String(val)); - + hint_map.insert(hint_name.clone(), serde_json::Value::Object(h.clone())); } } diff --git a/src/main.rs b/src/main.rs index edcf5a3..92942dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -109,7 +109,7 @@ async fn main() -> Result<()> { } "GetCapabilities" => { // Client requested server capabilities. Respond with the supported capabilities - let capabilities = vec!["actions", "body", "body-hyperlinks"]; // Add more LATER + let capabilities = vec![/*"actions",*/ "body", "body-hyperlinks"]; // Add more LATER connection.reply(&msg, &capabilities).await?; if verbose { println!("Request received: {member}\n\tCapabilities: {capabilities:?}"); diff --git a/src/notification.rs b/src/notification.rs index 2b73ef2..597201e 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -1,4 +1,4 @@ -use crate::formats::serde::{serialize_actions,serialize_hints}; +use crate::formats::serde::{serialize_actions, serialize_hints}; use serde::Serialize; use std::collections::HashMap; use zbus::{message::Body, Result}; From 28c808f07fe0a403b495d9db543a4a067f949a06 Mon Sep 17 00:00:00 2001 From: candifloss Date: Thu, 17 Oct 2024 16:46:01 +0530 Subject: [PATCH 06/14] commit --- src/formats/serde.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/formats/serde.rs b/src/formats/serde.rs index 6023240..23be10a 100644 --- a/src/formats/serde.rs +++ b/src/formats/serde.rs @@ -22,19 +22,21 @@ where pub fn serialize_hints( hints: &HashMap, - serializer: Serializer, -) -> Result { + serializer: S, +) -> Result +where + S: Serializer, +{ let mut hint_map = serde_json::Map::new(); for hint in hints { - if let (hint_name, hint_val) = hint { - let mut h = serde_json::Map::new(); - let sig = hint_val.value_signature(); - let val = hint_val.try_clone(); - h.insert(sig.to_string(), serde_json::Value::String(val)); + let (hint_name, hint_val) = hint; + let mut h = serde_json::Map::new(); + let sig = hint_val.value_signature(); + let val = hint_val.try_clone(); + h.insert(sig.to_string(), serde_json::Value::String(val)); - hint_map.insert(hint_name.clone(), serde_json::Value::Object(h.clone())); - } + hint_map.insert(hint_name.clone(), serde_json::Value::Object(h.clone())); } hint_map.serialize(serializer) From 2d42401b52fd65e2c1afa28ef731428325806330 Mon Sep 17 00:00:00 2001 From: candifloss Date: Sun, 20 Oct 2024 23:13:07 +0530 Subject: [PATCH 07/14] commit --- src/formats/serde.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/formats/serde.rs b/src/formats/serde.rs index 23be10a..2c5473d 100644 --- a/src/formats/serde.rs +++ b/src/formats/serde.rs @@ -3,6 +3,8 @@ use std::collections::HashMap; use zvariant::OwnedValue; //use std::hash::BuildHasher; use serde::ser::SerializeMap; +use serde_json::{Map,Value}; +use zvariant::SerializeValue; pub fn serialize_actions(actions: &[String], serializer: S) -> Result where @@ -27,19 +29,12 @@ pub fn serialize_hints( where S: Serializer, { - let mut hint_map = serde_json::Map::new(); - - for hint in hints { - let (hint_name, hint_val) = hint; - let mut h = serde_json::Map::new(); - let sig = hint_val.value_signature(); - let val = hint_val.try_clone(); - h.insert(sig.to_string(), serde_json::Value::String(val)); - - hint_map.insert(hint_name.clone(), serde_json::Value::Object(h.clone())); + // You can customize the serialization logic here + let mut state = serializer.serialize_map(Some(hints.len()))?; + for (key, value) in hints { + state.serialize_entry(key, value)?; } - - hint_map.serialize(serializer) + state.end() } /* From f25f2ce2ca7e6e340859bc1c79dce24b37d4e460 Mon Sep 17 00:00:00 2001 From: candifloss Date: Mon, 21 Oct 2024 10:10:53 +0530 Subject: [PATCH 08/14] another unsuccessful attempt to ser hints --- src/formats/serde.rs | 65 +++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 43 deletions(-) diff --git a/src/formats/serde.rs b/src/formats/serde.rs index 2c5473d..a86f8ec 100644 --- a/src/formats/serde.rs +++ b/src/formats/serde.rs @@ -1,8 +1,8 @@ -use serde::{Serialize, Serializer}; +use serde::ser::{Serialize, Serializer, SerializeMap}; use std::collections::HashMap; use zvariant::OwnedValue; //use std::hash::BuildHasher; -use serde::ser::SerializeMap; +//use serde::ser::SerializeMap; use serde_json::{Map,Value}; use zvariant::SerializeValue; @@ -29,52 +29,31 @@ pub fn serialize_hints( where S: Serializer, { - // You can customize the serialization logic here - let mut state = serializer.serialize_map(Some(hints.len()))?; - for (key, value) in hints { - state.serialize_entry(key, value)?; - } - state.end() -} - -/* -pub fn serialize_hints(hints: &HashMap, serializer: S) -> Result -where - S: serde::Serializer, -{ + // Start serializing the map let mut map = serializer.serialize_map(Some(hints.len()))?; - for (key, value) in hints { - // Try to cast the value to a string, otherwise fallback to debug format - let value_str = match value.downcast_ref::() { - Ok(s) => s.clone(), // Successfully cast to String - Err(_) => format!("{:?}", value), // Fallback for non-String values - }; - - map.serialize_entry(key, &value_str)?; + // Serialize each entry as desired + map.serialize_entry(key, &HintValueSerializer(value))?; } - map.end() } -*/ -/* -pub fn serialize_hints( - hints: &HashMap, - serializer: S, -) -> Result -where - S: Serializer, - H: BuildHasher, -{ - let mut map = serde_json::Map::new(); +// A custom struct to handle serialization of OwnedValue +struct HintValueSerializer<'a>(&'a OwnedValue); - for (key, value) in hints { - // Customize OwnedValue serialization as needed - map.insert(key.clone(), serde_json::Value::String(value.to_string())); +impl<'a> Serialize for HintValueSerializer<'a> { + fn serialize(&self, serializer: S) -> Result + 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() } - - map.serialize(serializer) -} - -*/ +} \ No newline at end of file From 08ac40ae852be1c16d8dffdd71689cc3e491ca04 Mon Sep 17 00:00:00 2001 From: candifloss Date: Mon, 18 Nov 2024 15:48:35 +0530 Subject: [PATCH 09/14] edited metadata --- Cargo.toml | 6 ++++++ src/main.rs | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 8abf818..ae14e97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,12 @@ name = "snot" version = "0.1.3" edition = "2021" authors = ["candifloss "] +description = "Simple NOTification" +license = "GPL-3.0-or-later" +repository = "https://git.candifloss.cc/candifloss/SNot" +homepage = "https://git.candifloss.cc/candifloss/SNot" +documentation = "https://git.candifloss.cc/candifloss/SNot" +readme = "README.md" [dependencies] zbus = "4.4.0" diff --git a/src/main.rs b/src/main.rs index 92942dd..54814c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ use futures_util::stream::TryStreamExt; use zbus::{message::Body, Connection, Result}; const SERVER_NAME: &str = "SNot"; // Server software name -const VENDOR: &str = "candifloss.cc"; // Server software vendor +const VENDOR: &str = env!("CARGO_PKG_AUTHORS"); // Server software vendor const VERSION: &str = env!("CARGO_PKG_VERSION"); // Server software version, from Cargo.toml const SPEC_VERSION: &str = "0.42"; // DBus specification version const NOTIF_INTERFACE: &str = "org.freedesktop.Notifications"; // DBus interface name From 27e0d3550d317c75236ec2e0833bb514cd4e83b9 Mon Sep 17 00:00:00 2001 From: candifloss Date: Mon, 2 Dec 2024 15:31:35 +0530 Subject: [PATCH 10/14] attempt to fix serialize hints --- src/formats/serde.rs | 56 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/src/formats/serde.rs b/src/formats/serde.rs index a86f8ec..40ad3db 100644 --- a/src/formats/serde.rs +++ b/src/formats/serde.rs @@ -1,27 +1,40 @@ -use serde::ser::{Serialize, Serializer, SerializeMap}; +/*use serde::ser::{Serialize, Serializer, SerializeMap}; use std::collections::HashMap; -use zvariant::OwnedValue; +//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, { - let mut map = serde_json::Map::new(); + let mut map = Map::new(); // Actions are in pairs: [id, label, id, label, ...] for pair in actions.chunks(2) { if let [id, label] = pair { - map.insert(id.clone(), serde_json::Value::String(label.clone())); + map.insert(id.clone(), JsonValue::String(label.clone())); } } map.serialize(serializer) } + pub fn serialize_hints( hints: &HashMap, serializer: S, @@ -40,7 +53,7 @@ where // 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 @@ -56,4 +69,35 @@ impl<'a> Serialize for HintValueSerializer<'a> { map.serialize_entry("value", value)?; map.end() } -} \ No newline at end of file +}*/ + +impl<'a> Serialize for HintValueSerializer<'a> { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + 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)) + } 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()) + } else { + serde_json::Value::Null // Unsupported types fallback to Null + }; + + // Serialize the final structure as a map with "signature" and "value" + let mut map = serializer.serialize_map(Some(2))?; + map.serialize_entry("signature", &signature)?; + map.serialize_entry("value", &raw_value)?; + map.end() + } +} + From afe7f6c908a65ddb770f4cbb8a445239c04c2bd3 Mon Sep 17 00:00:00 2001 From: candifloss Date: Mon, 2 Dec 2024 22:51:57 +0530 Subject: [PATCH 11/14] 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() } } - From 829a8741ee5a3f1c4822619aad68f0ebec9be1de Mon Sep 17 00:00:00 2001 From: candifloss Date: Tue, 3 Dec 2024 11:37:09 +0530 Subject: [PATCH 12/14] clippy and comments --- src/formats/rson.rs | 3 +-- src/formats/serde.rs | 45 +++++++++++++------------------------------- src/main.rs | 4 ++-- 3 files changed, 16 insertions(+), 36 deletions(-) diff --git a/src/formats/rson.rs b/src/formats/rson.rs index 8f03db6..9576477 100644 --- a/src/formats/rson.rs +++ b/src/formats/rson.rs @@ -5,7 +5,6 @@ use rson_rs::ser::to_string as rson_string; impl Notification { pub fn rson(&self) -> Result { - 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}"))) } } diff --git a/src/formats/serde.rs b/src/formats/serde.rs index 2666461..3e1fa97 100644 --- a/src/formats/serde.rs +++ b/src/formats/serde.rs @@ -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(actions: &[String], serializer: S) -> Result 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(&self, serializer: S) -> Result - 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(&self, serializer: S) -> Result 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::() { - JsonValue::from(v) // Removed *v + Value::from(v) } else if let Ok(v) = self.0.downcast_ref::() { - JsonValue::from(v) // Removed *v + Value::from(v) } else if let Ok(v) = self.0.downcast_ref::() { - JsonValue::from(v) // Removed *v + Value::from(v) } else if let Ok(v) = self.0.downcast_ref::() { - JsonValue::Bool(v) // Removed *v + Value::Bool(v) } else if let Ok(v) = self.0.downcast_ref::() { - 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 diff --git a/src/main.rs b/src/main.rs index 54814c9..b0f57d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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" => { From ac2dc744d5cccbbb66e2b73e01b95b0c05b1c129 Mon Sep 17 00:00:00 2001 From: candifloss Date: Tue, 3 Dec 2024 14:00:52 +0530 Subject: [PATCH 13/14] modified serialize function --- src/formats/serde.rs | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/formats/serde.rs b/src/formats/serde.rs index 3e1fa97..a7fd1e5 100644 --- a/src/formats/serde.rs +++ b/src/formats/serde.rs @@ -3,6 +3,7 @@ use serde_json::{Map, Value}; use std::collections::HashMap; use zvariant::OwnedValue; +/// Serialize actions pub fn serialize_actions(actions: &[String], serializer: S) -> Result where S: Serializer, @@ -19,6 +20,7 @@ where map.serialize(serializer) } +/// Serialize hints pub fn serialize_hints( hints: &HashMap, serializer: S, @@ -43,24 +45,34 @@ impl Serialize for HintValueSerializer<'_> { where S: Serializer, { - // Signature - let signature = self.0.value_signature().to_string(); + let signature = self.0.value_signature().to_string(); // Signature // Extract the raw value correctly - let raw_value = if let Ok(v) = self.0.downcast_ref::() { + let raw_value = if let Ok(v) = self.0.downcast_ref::() { // BYTE: y Value::from(v) - } else if let Ok(v) = self.0.downcast_ref::() { - Value::from(v) - } else if let Ok(v) = self.0.downcast_ref::() { - Value::from(v) - } else if let Ok(v) = self.0.downcast_ref::() { + } else if let Ok(v) = self.0.downcast_ref::() { // BOOLEAN: b Value::Bool(v) - } else if let Ok(v) = self.0.downcast_ref::() { + } else if let Ok(v) = self.0.downcast_ref::() { // INT16: n + Value::from(v) + } else if let Ok(v) = self.0.downcast_ref::() { // UINT16: q + Value::from(v) + } else if let Ok(v) = self.0.downcast_ref::() { // INT32: i + Value::from(v) + } else if let Ok(v) = self.0.downcast_ref::() { // UINT32: u + Value::from(v) + } else if let Ok(v) = self.0.downcast_ref::() { // INT64: x + Value::from(v) + } else if let Ok(v) = self.0.downcast_ref::() { // UINT64: t + Value::from(v) + } else if let Ok(v) = self.0.downcast_ref::() { // DOUBLE: d + Value::from(v) + } else if let Ok(v) = self.0.downcast_ref::() { // STRING: s Value::String(v.clone()) - } else if let Ok(v) = self.0.downcast_ref::<&str>() { + } else if let Ok(v) = self.0.downcast_ref::<&str>() { // str Value::String(v.to_string()) - } else { - Value::Null // Unsupported types fallback to Null + } else { // Unsupported types + Value::Null // Fallback to Null + // Not implemented: UNIX_FD: h, OBJECT_PATH: o, SIGNATURE: g }; // Serialize the final structure as a map @@ -69,4 +81,4 @@ impl Serialize for HintValueSerializer<'_> { map.serialize_entry("value", &raw_value)?; map.end() } -} +} \ No newline at end of file From 8e2d189a4d3955a920dd15cc586b420004c9f525 Mon Sep 17 00:00:00 2001 From: candifloss Date: Tue, 3 Dec 2024 15:31:42 +0530 Subject: [PATCH 14/14] clippy and comments --- src/formats/serde.rs | 59 +++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/src/formats/serde.rs b/src/formats/serde.rs index a7fd1e5..c2741c2 100644 --- a/src/formats/serde.rs +++ b/src/formats/serde.rs @@ -4,6 +4,8 @@ use std::collections::HashMap; use zvariant::OwnedValue; /// Serialize actions +/// # Errors +/// Will return an empty map if there are no actions pub fn serialize_actions(actions: &[String], serializer: S) -> Result where S: Serializer, @@ -21,6 +23,8 @@ where } /// Serialize hints +/// # Errors +/// Will return an empty map if there are no hints pub fn serialize_hints( hints: &HashMap, serializer: S, @@ -45,35 +49,34 @@ impl Serialize for HintValueSerializer<'_> { where S: Serializer, { - let signature = self.0.value_signature().to_string(); // Signature + let signature = self.0.value_signature().to_string(); // Get signature // Extract the raw value correctly - let raw_value = if let Ok(v) = self.0.downcast_ref::() { // BYTE: y - Value::from(v) - } else if let Ok(v) = self.0.downcast_ref::() { // BOOLEAN: b - Value::Bool(v) - } else if let Ok(v) = self.0.downcast_ref::() { // INT16: n - Value::from(v) - } else if let Ok(v) = self.0.downcast_ref::() { // UINT16: q - Value::from(v) - } else if let Ok(v) = self.0.downcast_ref::() { // INT32: i - Value::from(v) - } else if let Ok(v) = self.0.downcast_ref::() { // UINT32: u - Value::from(v) - } else if let Ok(v) = self.0.downcast_ref::() { // INT64: x - Value::from(v) - } else if let Ok(v) = self.0.downcast_ref::() { // UINT64: t - Value::from(v) - } else if let Ok(v) = self.0.downcast_ref::() { // DOUBLE: d - Value::from(v) - } else if let Ok(v) = self.0.downcast_ref::() { // STRING: s - Value::String(v.clone()) - } else if let Ok(v) = self.0.downcast_ref::<&str>() { // str - Value::String(v.to_string()) - } else { // Unsupported types - Value::Null // Fallback to Null - // Not implemented: UNIX_FD: h, OBJECT_PATH: o, SIGNATURE: g - }; + let raw_value = if let Ok(v) = self.0.downcast_ref::() { + Value::from(v) // BYTE: y + } else if let Ok(v) = self.0.downcast_ref::() { + Value::Bool(v) // BOOLEAN: b + } else if let Ok(v) = self.0.downcast_ref::() { + Value::from(v) // INT16: n + } else if let Ok(v) = self.0.downcast_ref::() { + Value::from(v) // UINT16: q + } else if let Ok(v) = self.0.downcast_ref::() { + Value::from(v) // INT32: i + } else if let Ok(v) = self.0.downcast_ref::() { + Value::from(v) // UINT32: u + } else if let Ok(v) = self.0.downcast_ref::() { + Value::from(v) // INT64: x + } else if let Ok(v) = self.0.downcast_ref::() { + Value::from(v) // UINT64: t + } else if let Ok(v) = self.0.downcast_ref::() { + Value::from(v) // DOUBLE: d + } else if let Ok(v) = self.0.downcast_ref::() { + Value::String(v.clone()) // STRING: s + } else if let Ok(v) = self.0.downcast_ref::<&str>() { + Value::String(v.to_string()) // str + } else { + Value::Null // Unsupported types: fallback to Null + }; // Not implemented: UNIX_FD: h, OBJECT_PATH: o, SIGNATURE: g // Serialize the final structure as a map let mut map = serializer.serialize_map(Some(2))?; @@ -81,4 +84,4 @@ impl Serialize for HintValueSerializer<'_> { map.serialize_entry("value", &raw_value)?; map.end() } -} \ No newline at end of file +}