Skip to content

Commit ee76f1c

Browse files
committed
Initial commit
1 parent 4ce779a commit ee76f1c

16 files changed

+350
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target/
2+
.idea/

Cargo.lock

+114
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[workspace]
2+
members = ["server", "client", "proto"]

client/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

client/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "client"
3+
version = "0.1.0"
4+
authors = ["Redrield <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]

client/src/main.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}

proto/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
Cargo.lock

proto/Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "proto"
3+
version = "0.1.0"
4+
authors = ["Redrield <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
serde = { version = "^1.0.115", features = ["derive"] }
11+
serde_json = "^1.0.57"
12+
serde_cbor = "^0.11.1"

proto/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
mod text;

proto/src/text.rs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use serde::{Serialize, Deserialize};
2+
use serde_json::Value;
3+
4+
macro_rules! impl_message {
5+
($($name:ident),+) => {
6+
$(
7+
impl MessageBody for $name {
8+
fn into_message(self) -> $crate::text::NTMessage {
9+
$crate::text::NTMessage {
10+
_type: $crate::text::MessageType::$name,
11+
data: serde_json::to_value(self).unwrap()
12+
}
13+
}
14+
}
15+
)+
16+
}
17+
}
18+
19+
mod publish;
20+
mod directory;
21+
mod subscription;
22+
23+
pub trait MessageBody {
24+
fn into_message(self) -> NTMessage;
25+
}
26+
27+
#[derive(Serialize, Deserialize)]
28+
#[serde(rename_all = "lowercase")]
29+
pub enum MessageType {
30+
#[serde(rename = "publish")]
31+
PublishReq,
32+
#[serde(rename = "puback")]
33+
PublishAck,
34+
#[serde(rename = "pubrel")]
35+
PublishRel,
36+
List,
37+
Directory,
38+
Listen,
39+
Unlisten,
40+
Announce,
41+
Unannounce,
42+
GetValues,
43+
Subscribe,
44+
Unsubscribe,
45+
}
46+
47+
#[derive(Serialize, Deserialize)]
48+
#[serde(rename_all = "lowercase")]
49+
pub enum DataType {
50+
Boolean,
51+
Raw,
52+
String,
53+
Integer,
54+
Float,
55+
Double,
56+
#[serde(rename = "boolean[]")]
57+
BooleanArray,
58+
#[serde(rename = "string[]")]
59+
StringArray,
60+
#[serde(rename = "integer[]")]
61+
IntegerArray,
62+
#[serde(rename = "float[]")]
63+
FloatArray,
64+
#[serde(rename = "double[]")]
65+
DoubleArray,
66+
}
67+
68+
#[derive(Serialize, Deserialize)]
69+
pub struct NTMessage {
70+
#[serde(rename = "type")]
71+
_type: MessageType,
72+
data: Value,
73+
}

proto/src/text/directory.rs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use serde::{Serialize, Deserialize};
2+
use super::MessageBody;
3+
use crate::text::DataType;
4+
5+
#[derive(Serialize, Deserialize)]
6+
pub struct List {
7+
prefix: String,
8+
}
9+
10+
#[derive(Serialize, Deserialize)]
11+
pub struct Directory {
12+
items: Vec<DirectoryItem>,
13+
}
14+
15+
#[derive(Serialize, Deserialize)]
16+
pub struct DirectoryItem {
17+
name: String,
18+
id: u32,
19+
#[serde(rename = "type")]
20+
_type: DataType,
21+
persistent: bool,
22+
}
23+
24+
#[derive(Serialize, Deserialize)]
25+
pub struct Listen {
26+
prefix: String,
27+
listenuid: u32,
28+
}
29+
30+
#[derive(Serialize, Deserialize)]
31+
pub struct Unlisten {
32+
listenuid: u32,
33+
}
34+
35+
#[derive(Serialize, Deserialize)]
36+
pub struct Announce {
37+
name: String,
38+
id: u32,
39+
#[serde(rename = "type")]
40+
_type: DataType,
41+
persistent: bool,
42+
}
43+
44+
#[derive(Serialize, Deserialize)]
45+
pub struct Unannounce {
46+
name: String,
47+
id: u32,
48+
}
49+
50+
impl_message!(List, Directory, Listen, Unlisten, Announce, Unannounce);

proto/src/text/publish.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use serde::{Serialize, Deserialize};
2+
use super::{MessageBody, DataType};
3+
4+
5+
#[derive(Serialize, Deserialize)]
6+
pub struct PublishReq {
7+
name: String,
8+
#[serde(rename = "type")]
9+
_type: DataType,
10+
options: Option<PublishRequestOptions>
11+
}
12+
13+
#[derive(Serialize, Deserialize)]
14+
pub struct PublishRequestOptions {
15+
persistent: bool,
16+
}
17+
18+
#[derive(Serialize, Deserialize)]
19+
pub struct PublishAck {
20+
name: String,
21+
#[serde(rename = "type")]
22+
_type: String,
23+
id: u32,
24+
}
25+
26+
#[derive(Serialize, Deserialize)]
27+
pub struct PublishRel {
28+
id: u32,
29+
delete: bool,
30+
}
31+
32+
impl_message!(PublishReq, PublishAck, PublishRel);

proto/src/text/subscription.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use serde::{Serialize, Deserialize};
2+
use super::MessageBody;
3+
4+
#[derive(Serialize, Deserialize)]
5+
pub struct GetValues {
6+
ids: Vec<u32>,
7+
options: Option<GetValuesOptions>
8+
}
9+
10+
#[derive(Serialize, Deserialize)]
11+
pub struct GetValuesOptions {
12+
timestamped: bool
13+
}
14+
15+
#[derive(Serialize, Deserialize)]
16+
pub struct Subscribe {
17+
ids: Vec<u32>,
18+
subuid: u32,
19+
options: Option<SubscribeOptions>
20+
}
21+
22+
#[derive(Serialize, Deserialize)]
23+
pub struct SubscribeOptions {
24+
immediate: bool,
25+
periodic: f64,
26+
logging: bool,
27+
timestamped: bool,
28+
}
29+
30+
#[derive(Serialize, Deserialize)]
31+
pub struct Unsubscribe {
32+
subuid: u32,
33+
}
34+
35+
impl_message!(GetValues, Subscribe, Unsubscribe);

server/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

server/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "server"
3+
version = "0.1.0"
4+
authors = ["Redrield <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]

server/src/main.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}

0 commit comments

Comments
 (0)