-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from vincentdephily/refactor_0.2
API changes, unittest, and docs
- Loading branch information
Showing
15 changed files
with
1,064 additions
and
658 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# 0.2 (unreleased) | ||
|
||
This is a fairly large release with API fixes and improvements, bug fixes, and much better test | ||
coverage and documentation. | ||
|
||
## API changes | ||
|
||
* We now return a dedicated error type instead of abusing `std::io::Error`. | ||
* `PacketIdentifier` was renamed to `Pid`. It now avoids the illegal value 0, wraps around automatically, and can be hashed. | ||
* `Publish.qos` and `Publish.pid` have been merged together, avoiding accidental illegal combinations. | ||
* `Connect.password` and `Connect.will.payload` can now contain binary data. | ||
* The `Protocol` enum doesn't carry extra data anymore. | ||
* All public structs/enum/functions are now (re)exported from the crate root, and the rest has been made private. | ||
* The letter-casing of packet types is more consistent. | ||
* Packet subtypes can be converted to `Packet` using `.into()`. | ||
|
||
## Other changes | ||
|
||
* Much improved documentation. See it with `cargo doc --open`. | ||
* More thorough unittesting, including exhaustive and random value ranges testing. | ||
* Lots of corner-case bugfixes, particularly when decoding partial or corrupted data. | ||
* The minimum rust version is now 1.32. | ||
* Raised `mqttrs`'s bus factor to 2 ;) | ||
|
||
# 0.1.4 (2019-09-16) | ||
|
||
* Fix issue #8: Decoding an incomplete packet still consumes bytes from the buffer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
cargo-features = ["edition"] | ||
|
||
[package] | ||
name = "mqttrs" | ||
version = "0.1.4" | ||
authors = ["00imvj00 <[email protected]>"] | ||
authors = ["00imvj00 <[email protected]>", | ||
"Vincent de Phily <[email protected]>"] | ||
edition = "2018" | ||
description = "mqttrs is encoding & decoding library for mqtt protocol, it can work with both sync as well as async apps" | ||
homepage = "https://github.com/00imvj00/mqttrs" | ||
repository = "https://github.com/00imvj00/mqttrs" | ||
keywords = ["mqtt", "encoding", "decoding", "async", "async-mqtt"] | ||
license = "Apache-2.0" | ||
|
||
|
||
[dependencies] | ||
bytes = "0.4" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,45 @@ | ||
# Rust Mqtt Encoding & Decoding | ||
|
||
### What is Mqtt? | ||
MQTT is an ISO standard publish-subscribe-based messaging protocol. It works on top of the TCP/IP protocol. | ||
|
||
### What is Rust? | ||
Rust is a multi-paradigm systems programming language focused on safety, especially safe concurrency. Rust is syntactically similar to C++, but is designed to provide better memory safety while maintaining high performance. | ||
|
||
### What is mqttrs? | ||
|
||
It is library which can be used in any rust projects where you need to transform valid mqtt bytes buffer to Mqtt types and vice versa. | ||
|
||
In short it is encoding/decoding library which you can use it in sync as well as async environment. | ||
|
||
The way it works is, It will take byte buffer as input and then will try to read the header of the mqtt packet, if the packet is not completely received as it happens in async networking, the library function will return `None` and will not remove any bytes from buffer. | ||
|
||
Once, the whole mqtt packet is received, mqttrs will convert the bytes into appropriate mqtt packet type and return as well as remove all bytes from the beginning which belongs to already received packet. | ||
|
||
So, in this way, this library can be used for sync tcp streams as well as async streams like tokio tcp streams. | ||
|
||
# Rust Mqtt Encoding & Decoding [![Crates.io](https://img.shields.io/crates/l/mqttrs)](LICENSE) [![Docs.rs](https://docs.rs/mqttrs/badge.svg)](https://docs.rs/mqttrs/*/mqttrs/) | ||
|
||
`Mqttrs` is a [Rust](https://www.rust-lang.org/) crate (library) to write [MQTT | ||
protocol](https://mqtt.org/) clients and servers. | ||
|
||
It is a codec-only library with [very few dependencies](Cargo.toml) and a [straightworward and | ||
composable API](https://docs.rs/mqttrs/*/mqttrs/), usable with rust's standard library or with async | ||
frameworks like [tokio](https://tokio.rs/). | ||
|
||
`Mqttrs` currently requires [Rust >= 1.32](https://www.rust-lang.org/learn/get-started) and supports | ||
[MQTT 3.1.1](http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html). Support for [MQTT | ||
5](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html) is planned for a future version. | ||
|
||
## Usage | ||
|
||
Add `mqttrs = "0.2"` to your `Cargo.toml`. | ||
|
||
```rust | ||
use mqttrs::*; | ||
use bytes::BytesMut; | ||
|
||
// Allocate write buffer. | ||
let mut buf = BytesMut::with_capacity(1024); | ||
|
||
// Encode an MQTT Connect packet. | ||
let pkt = Packet::Connect(Connect { protocol: Protocol::MQTT311, | ||
keep_alive: 30, | ||
client_id: "doc_client".into(), | ||
clean_session: true, | ||
last_will: None, | ||
username: None, | ||
password: None }); | ||
assert!(encode(&pkt, &mut buf).is_ok()); | ||
assert_eq!(&buf[14..], "doc_client".as_bytes()); | ||
let mut encoded = buf.clone(); | ||
|
||
// Decode one packet. The buffer will advance to the next packet. | ||
assert_eq!(Ok(Some(pkt)), decode(&mut buf)); | ||
|
||
// Example decode failures. | ||
let mut incomplete = encoded.split_to(10); | ||
assert_eq!(Ok(None), decode(&mut incomplete)); | ||
let mut garbage = BytesMut::from(vec![0u8,0,0,0]); | ||
assert_eq!(Err(Error::InvalidHeader), decode(&mut garbage)); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.