intercom_rs/lib.rs
1//! # Intercom
2//!
3//! A fully typed async wrapper for NATS with JetStream support.
4//!
5//! ## Features
6//!
7//! - Fully typed publish/subscribe with turbofish syntax support
8//! - Pluggable codec support (MessagePack default, JSON optional)
9//! - JetStream support with builder pattern
10//! - Push and pull consumers
11//! - Interest-based consumers
12//! - Work queues
13//! - Stream trait for subscribers
14//! - Sink trait for publishers
15//!
16//! ## Codec Selection
17//!
18//! The library supports multiple serialization codecs via cargo features:
19//! - `msgpack` (default) - MessagePack binary serialization
20//! - `json` - JSON text serialization
21//!
22//! When creating a client, specify the codec type:
23//!
24//! ```no_run
25//! use intercom::{Client, MsgPackCodec, Result};
26//!
27//! # async fn example() -> Result<()> {
28//! // Using MessagePack (default)
29//! let client = Client::<MsgPackCodec>::connect("nats://localhost:4222").await?;
30//! # Ok(())
31//! # }
32//! ```
33//!
34//! Or with JSON:
35//!
36//! ```ignore
37//! use intercom::{Client, JsonCodec, Result};
38//!
39//! # async fn example() -> Result<()> {
40//! // Using JSON (requires `json` feature)
41//! let client = Client::<JsonCodec>::connect("nats://localhost:4222").await?;
42//! # Ok(())
43//! # }
44//! ```
45//!
46//! ## Example
47//!
48//! ```no_run
49//! use intercom::{Client, MsgPackCodec, Result};
50//! use serde::{Deserialize, Serialize};
51//!
52//! #[derive(Debug, Serialize, Deserialize)]
53//! struct MyMessage {
54//! content: String,
55//! }
56//!
57//! #[tokio::main]
58//! async fn main() -> Result<()> {
59//! let client = Client::<MsgPackCodec>::connect("nats://localhost:4222").await?;
60//!
61//! // Type-safe publish
62//! client.publish::<MyMessage>("subject", &MyMessage { content: "hello".into() }).await?;
63//!
64//! // Create a typed subscriber with Stream trait
65//! let mut subscriber = client.subscribe::<MyMessage>("subject").await?;
66//!
67//! // Create a typed publisher with Sink trait
68//! let publisher = client.publisher::<MyMessage>("subject");
69//!
70//! Ok(())
71//! }
72//! ```
73//!
74//! ## JetStream Example
75//!
76//! ```no_run
77//! use intercom::{Client, MsgPackCodec, Result};
78//! use intercom::jetstream::stream::RetentionPolicy;
79//! use serde::{Deserialize, Serialize};
80//! use futures::StreamExt;
81//!
82//! #[derive(Debug, Serialize, Deserialize)]
83//! struct Event {
84//! id: u64,
85//! data: String,
86//! }
87//!
88//! #[tokio::main]
89//! async fn main() -> Result<()> {
90//! let client = Client::<MsgPackCodec>::connect("nats://localhost:4222").await?;
91//! let jetstream = client.jetstream();
92//!
93//! // Create a stream with builder pattern
94//! let stream = jetstream
95//! .stream_builder("events")
96//! .subjects(vec!["events.>".to_string()])
97//! .max_messages(1_000_000)
98//! .create()
99//! .await?;
100//!
101//! // Publish typed messages
102//! jetstream.publish::<Event>("events.user", &Event {
103//! id: 1,
104//! data: "user created".to_string(),
105//! }).await?;
106//!
107//! // Create a pull consumer with turbofish syntax
108//! let consumer = stream
109//! .pull_consumer_builder::<Event>("my-consumer")
110//! .durable()
111//! .create()
112//! .await?;
113//!
114//! // Fetch messages with Stream trait
115//! let mut messages = consumer.fetch(10).await?;
116//! while let Some(result) = messages.next().await {
117//! let msg = result?;
118//! println!("Got: {:?}", msg.payload);
119//! msg.ack().await?;
120//! }
121//!
122//! Ok(())
123//! }
124//! ```
125
126pub mod client;
127pub mod codec;
128pub mod error;
129pub mod jetstream;
130pub mod publisher;
131pub mod subscriber;
132
133pub use client::Client;
134pub use codec::{Codec, CodecType, Decode, Encode};
135#[cfg(feature = "msgpack")]
136pub use codec::MsgPackCodec;
137#[cfg(feature = "json")]
138pub use codec::JsonCodec;
139#[cfg(any(feature = "msgpack", feature = "json"))]
140pub use codec::DefaultCodec;
141pub use error::{Error, Result};
142pub use jetstream::consumer::{
143 AckPolicy, DeliverPolicy, JetStreamMessage, PullBatch, PullConsumer, PullConsumerBuilder,
144 PullMessages, PushConsumer, PushConsumerBuilder, PushMessages, ReplayPolicy,
145};
146pub use jetstream::context::{JetStreamContext, PublishAck, PublishAckFuture};
147pub use jetstream::queue::{InterestQueue, InterestQueueBuilder, QueueMessage, StreamingWorkQueue, WorkQueue, WorkQueueBuilder, WorkQueueSink};
148pub use jetstream::stream::{
149 Compression, DiscardPolicy, RetentionPolicy, StorageType, Stream, StreamBuilder, StreamConfig,
150 StreamInfo, StreamSource, StreamState,
151};
152pub use publisher::Publisher;
153pub use subscriber::{Message, Subscriber};