From 8c87dfd308038483f10f7113059afa83809fc253 Mon Sep 17 00:00:00 2001 From: Maciek Borzecki Date: Sat, 22 Feb 2020 21:19:44 +0100 Subject: [PATCH] kdeconnect: basic packet types Signed-off-by: Maciek Borzecki --- kdeconnect/Cargo.toml | 3 ++ kdeconnect/src/lib.rs | 29 ++++++++--- kdeconnect/src/packet.rs | 103 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 kdeconnect/src/packet.rs diff --git a/kdeconnect/Cargo.toml b/kdeconnect/Cargo.toml index 8ec6125..08266fb 100644 --- a/kdeconnect/Cargo.toml +++ b/kdeconnect/Cargo.toml @@ -3,7 +3,10 @@ name = "kdeconnect" version = "0.1.0" authors = ["Maciek Borzecki "] edition = "2018" +license = "MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +serde = { version = "1.0", features = ["derive"] } +serde_json = { version = "1.0", features = ["raw_value"]} diff --git a/kdeconnect/src/lib.rs b/kdeconnect/src/lib.rs index a6c14ff..757a7ed 100644 --- a/kdeconnect/src/lib.rs +++ b/kdeconnect/src/lib.rs @@ -1,10 +1,25 @@ -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } -} +// The MIT License (MIT) +// +// Copyright (c) 2020 Maciek Borzecki +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +mod packet; pub fn bang() { println!("bang!"); diff --git a/kdeconnect/src/packet.rs b/kdeconnect/src/packet.rs new file mode 100644 index 0000000..29960aa --- /dev/null +++ b/kdeconnect/src/packet.rs @@ -0,0 +1,103 @@ +// The MIT License (MIT) +// +// Copyright (c) 2020 Maciek Borzecki +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +use serde::{Deserialize, Serialize}; +use serde_json::value::{RawValue, Value}; + +// Identity type +const IDENTITY: &str = "kdeconnect.identity"; +// Pair type +const PAIR : &str = "kdeconnect.pair"; + +/// Represents an incoming packet. Only the type and ID deserialized. The +/// actual payload is handled higher up the stack. +#[derive(Deserialize)] +pub struct IncomingPacket<'a> { + #[serde(rename = "type")] + typ: String, + id: String, + #[serde(borrow)] + body: &'a RawValue, +} + +// Represents and outgoing packet. +#[derive(Serialize)] +pub struct Packet { + #[serde(rename = "type")] + typ: String, + id: String, + body: Value, +} + +#[cfg(test)] +mod tests { + use serde_json::{json, value::RawValue, value::Value, Result}; + + #[test] + fn packet_serialize_deserialize_simple() { + let s = serde_json::to_string(&super::Packet { + typ: "kdeconnect.foo".to_string(), + id: "123".to_string(), + body: json!("foobar foobar"), + }) + .unwrap(); + + let back: Result = serde_json::from_str(&s); + assert_eq!(back.is_ok(), true); + + let v = back.unwrap(); + assert_eq!(v.typ, "kdeconnect.foo"); + assert_eq!(v.id, "123"); + assert_eq!(v.body.get(), r#""foobar foobar""#) + } + + #[test] + fn packet_serialize_deserialize_complex() { + let s = serde_json::to_string(&super::Packet { + typ: "kdeconnect.foo".to_string(), + id: "123".to_string(), + body: json!({ + "foo": "bar", + "baz": 2, + "list": ["a", "b"], + }), + }) + .unwrap(); + + let back: Result = serde_json::from_str(&s); + assert_eq!(back.is_ok(), true); + + let v = back.unwrap(); + assert_eq!(v.typ, "kdeconnect.foo"); + assert_eq!(v.id, "123"); + + let bv: Value = serde_json::from_str(v.body.get()).unwrap(); + assert_eq!( + bv, + json!({ + "foo": "bar", + "baz": 2, + "list": ["a", "b"], + }) + ); + } +}