Skip to main content

arminius_lib_nats/
lib.rs

1// SPDX-FileCopyrightText: 2025 BWI GmbH
2//
3// SPDX-License-Identifier: Apache-2.0
4
5//! NATS-related stuff and utilities
6
7#[cfg(any(test, feature = "test_utils"))]
8pub mod test_utils;
9
10pub mod auth_management;
11mod client;
12pub mod config;
13pub mod consumergroup;
14mod credentials;
15pub mod headers;
16pub mod opentelemetry;
17mod publisher;
18mod requester;
19mod responder;
20mod resubscribe;
21mod subscriber;
22
23#[cfg(feature = "test_utils")]
24pub use arminius_lib_message_bus::mock;
25/// Re-exported so services only need to depend on `arminius-lib-nats`.
26///
27/// The message-bus [`Subscriber`](arminius_lib_message_bus::Subscriber) trait
28/// is intentionally *not* re-exported here to avoid clashing with the
29/// [`async_nats::Subscriber`] re-export above; reach it via
30/// `arminius_lib_message_bus::Subscriber`.
31pub use arminius_lib_message_bus::{
32	Command, CommandError, Error as MessageBusError, Event, Publisher, ReceivedCommand,
33	ReceivedEvent, Requester, Responder, encode_subject_segment,
34};
35/// This is exported so that the NatsClient APIs can access it.
36pub use async_nats::Subscriber;
37use async_nats::jetstream::{
38	self,
39	context::{CreateKeyValueError, CreateStreamError, GetStreamError},
40	kv,
41};
42pub use client::{NatsClient, NatsRequestBody, SharedNatsClient};
43pub use config::{NatsConfiguration, NatsConfigurationError};
44pub use credentials::{NatsCredentials, NatsJwtCredentials};
45pub use ed25519_dalek::SigningKey;
46pub use requester::{__private, NatsReceivedCommand};
47pub use subscriber::NatsReceivedEvent;
48
49/// Errors that can occur while interacting with NATS, its JetStream context, or
50/// the key-value store.
51#[derive(thiserror::Error, Debug)]
52pub enum NatsError {
53	/// A generic error surfaced by the underlying `async-nats` client.
54	#[error(transparent)]
55	NatsError(#[from] async_nats::Error),
56	/// A payload could not be serialized to or deserialized from JSON.
57	#[error(transparent)]
58	SerializationError(#[from] serde_json::Error),
59	/// Subscribing to a subject failed.
60	#[error(transparent)]
61	SubscribeError(#[from] async_nats::SubscribeError),
62	/// Publishing a message to the core NATS client failed.
63	#[error(transparent)]
64	PublishError(#[from] async_nats::client::PublishError),
65	/// Publishing a message to a JetStream stream failed.
66	#[error(transparent)]
67	JetstreamPublishError(#[from] async_nats::error::Error<jetstream::context::PublishErrorKind>),
68	/// A request timed out before a response was received.
69	#[error("Timed out waiting for NATS response")]
70	Timeout,
71	/// The reply stream for a request was closed before a response arrived.
72	#[error("Reply stream prematurely closed")]
73	ReplyStreamPrematurelyClosed,
74	/// Writing an entry to the key-value store failed.
75	#[error(transparent)]
76	Put(#[from] kv::PutError),
77	/// Retrieving a JetStream stream failed.
78	#[error(transparent)]
79	Get(#[from] GetStreamError),
80	/// Reading an entry from the key-value store failed.
81	#[error(transparent)]
82	Entry(#[from] kv::EntryError),
83	/// Watching a key-value store for changes failed.
84	#[error(transparent)]
85	Watch(#[from] kv::WatchError),
86	/// Creating a JetStream stream failed.
87	#[error(transparent)]
88	CreateStream(#[from] CreateStreamError),
89	/// Creating a key-value store failed.
90	#[error(transparent)]
91	KeyValue(#[from] CreateKeyValueError),
92}