Rust

protocol

Module

The NATS client protocol: what the client writes (Writer) and what it reads (Parser).

Contents

2 submodules, and what the module defines itself.

use utopia_nats::protocol::Command;
Rust
pub enum Command

The operations a client sends.

use utopia_nats::protocol::ServerOp;
Rust
pub enum ServerOp

The kinds of operation a server sends.

protocol::parser

Own page

Decodes server operations from the byte stream.

use utopia_nats::protocol::parser::Parser;
Rust
pub struct Parser

Reads server operations off a Transport, buffering what it read past the current operation.

A frame body that fails half way (its header line consumed, its payload not) poisons the parser: every later call fails with Error::Connection until the connection builds a new one.

Rust
use utopia_nats::protocol::{Op, Parser};
let mut transport = Script(b"PING\r\nMSG orders.new 1 5\r\nhello\r\n".to_vec());
let mut parser = Parser::new();
assert_eq!(parser.next(&mut transport, None).await?, Op::Ping);
let Op::Msg(msg) = parser.next(&mut transport, None).await? else { unreachable!() };
assert_eq!((msg.subject.as_str(), msg.payload.as_slice()), ("orders.new", &b"hello"[..]));
use utopia_nats::protocol::parser::RawMessage;
Rust
pub struct RawMessage

A MSG or HMSG frame as read, before it becomes a crate::Message.

use utopia_nats::protocol::parser::Op;
Rust
pub enum Op

One operation read from the server.

protocol::writer

Own page

Encodes client operations.

use utopia_nats::protocol::writer::Writer;
Rust
pub struct Writer

Encodes the operations a client sends, byte for byte as the server expects them.

Rust
use utopia_nats::protocol::Writer;

assert_eq!(Writer::publish("orders.new", b"hi", Some("_INBOX.1")), b"PUB orders.new _INBOX.1 2\r\nhi\r\n");
assert_eq!(Writer::sub("orders.>", "7", Some("workers")), b"SUB orders.> workers 7\r\n");