https://github.com/encabulators/nats-types
Data types for converting NATS protocol messages to/from text
https://github.com/encabulators/nats-types
data-structures nats protocol
Last synced: 3 months ago
JSON representation
Data types for converting NATS protocol messages to/from text
- Host: GitHub
- URL: https://github.com/encabulators/nats-types
- Owner: encabulators
- License: apache-2.0
- Created: 2018-06-24T11:20:19.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-04-28T19:09:11.000Z (about 7 years ago)
- Last Synced: 2025-12-13T22:47:36.905Z (6 months ago)
- Topics: data-structures, nats, protocol
- Language: Rust
- Size: 27.3 KB
- Stars: 6
- Watchers: 1
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](https://travis-ci.org/encabulators/nats-types)
# nats-types
The `nats-types` crate contains an enum `ProtocolMessage`. This enum can be used to
parse the string output from a NATS server as well as produce strings to be sent to
a NATS server. This enum is up to date with NATS 2.0 protocol messages.
The primary use for this crate is to be used in support of building a NATS client, though
other potential uses might be possible.
To produce a protocol message, simply create the enum:
```rust
extern crate nats_types;
use nats_types::{PublishMessage, ProtocolMessage};
let publish = ProtocolMessage::Publish( PublishMessage::new(
Some("INBOX.42".to_string()),
"workdispatch".to_string(),
b"Hello World".to_vec(),
});
let out = format!("{}", publish);
assert_eq!(out, "PUB workdispatch INBOX.42 11\r\nHello World\r\n");
```
The same message can be constructed from the 2-line message received from a NATS server:
```rust
extern crate nats_types;
use std::str::FromStr;
use nats_types::{ProtocolMessage};
let msg = "PUB FOO 11\r\nHello NATS!\r\n";
let protomsg = ProtocolMessage::from_str(&msg).unwrap();
if let ProtocolMessage::Publish(pubm) = protomsg {
assert_eq!(pubm.payload_size, 11);
assert_eq!(pubm.subject, "FOO");
assert_eq!(pubm.reply_to, None);
assert_eq!(pubm.payload, b"Hello NATS!");
}
```