Browse Source

kdeconnect: basic packet types

Signed-off-by: Maciek Borzecki <maciek.borzecki@gmail.com>
bboozzoo/maybe-rust
Maciek Borzecki 5 years ago
parent
commit
8c87dfd308
3 changed files with 128 additions and 7 deletions
  1. +3
    -0
      kdeconnect/Cargo.toml
  2. +22
    -7
      kdeconnect/src/lib.rs
  3. +103
    -0
      kdeconnect/src/packet.rs

+ 3
- 0
kdeconnect/Cargo.toml View File

@ -3,7 +3,10 @@ name = "kdeconnect"
version = "0.1.0"
authors = ["Maciek Borzecki <maciek.borzecki@gmail.com>"]
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"]}

+ 22
- 7
kdeconnect/src/lib.rs View File

@ -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 <maciek.borzecki@gmail.com>
//
// 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!");


+ 103
- 0
kdeconnect/src/packet.rs View File

@ -0,0 +1,103 @@
// The MIT License (MIT)
//
// Copyright (c) 2020 Maciek Borzecki <maciek.borzecki@gmail.com>
//
// 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<super::IncomingPacket> = 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<super::IncomingPacket> = 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"],
})
);
}
}

Loading…
Cancel
Save