Skip to main content

krafka/
lib.rs

1//! # Krafka
2//!
3//! A pure Rust, async-native Apache Kafka client.
4//!
5//! Krafka provides high-performance, safe, and idiomatic Rust APIs for
6//! producing and consuming messages from Apache Kafka clusters.
7//!
8//! ## Features
9//!
10//! - **Pure Rust**: No librdkafka or C bindings
11//! - **Async-native**: Built on Tokio for non-blocking I/O
12//! - **High-performance**: Zero-copy buffers, minimal allocations
13//! - **Safe**: No unsafe code by default
14//! - **Cloud-native**: First-class AWS MSK support including IAM auth
15//!
16//! ## Thread Safety
17//!
18//! All main types in Krafka implement `Send + Sync`:
19//!
20//! - [`Producer`](producer::Producer) - can be shared across tasks with `Arc`
21//! - [`Consumer`](consumer::Consumer) - can be shared across tasks with `Arc`
22//! - [`AdminClient`](admin::AdminClient) - can be shared across tasks with `Arc`
23//!
24//! This allows safe concurrent access from multiple Tokio tasks:
25//!
26//! ```rust,no_run
27//! use std::sync::Arc;
28//! use krafka::producer::Producer;
29//!
30//! # async fn example() -> Result<(), krafka::error::KrafkaError> {
31//! let producer = Arc::new(Producer::builder()
32//!     .bootstrap_servers("localhost:9092")
33//!     .build()
34//!     .await?);
35//!
36//! // Spawn multiple tasks sharing the producer
37//! for i in 0..10 {
38//!     let producer = producer.clone();
39//!     tokio::spawn(async move {
40//!         producer.send("topic", None, b"message").await.ok();
41//!     });
42//! }
43//! # Ok(())
44//! # }
45//! ```
46//!
47//! ## Quick Start
48//!
49//! ### Producer
50//!
51//! ```rust,no_run
52//! use krafka::producer::Producer;
53//!
54//! # async fn example() -> Result<(), krafka::error::KrafkaError> {
55//! let producer = Producer::builder()
56//!     .bootstrap_servers("localhost:9092")
57//!     .build()
58//!     .await?;
59//!
60//! producer.send("my-topic", Some(b"key"), b"value").await?;
61//! # Ok(())
62//! # }
63//! ```
64//!
65//! ### Consumer
66//!
67//! ```rust,no_run
68//! use krafka::consumer::Consumer;
69//!
70//! # async fn example() -> Result<(), krafka::error::KrafkaError> {
71//! let consumer = Consumer::builder()
72//!     .bootstrap_servers("localhost:9092")
73//!     .group_id("my-group")
74//!     .build()
75//!     .await?;
76//!
77//! consumer.subscribe(&["my-topic"]).await?;
78//!
79//! while let Some(msg) = consumer.recv().await? {
80//!     println!("{:?}", msg);
81//! }
82//! # Ok(())
83//! # }
84//! ```
85
86#![warn(missing_docs)]
87#![warn(rust_2018_idioms)]
88#![deny(unsafe_code)]
89
90pub mod admin;
91pub mod auth;
92pub mod consumer;
93pub mod error;
94pub mod interceptor;
95pub mod metadata;
96pub mod metrics;
97pub mod network;
98pub mod producer;
99pub mod protocol;
100pub mod tracing_ext;
101pub mod util;
102
103pub use error::{KrafkaError, Result};
104
105/// Kafka protocol API version.
106pub type ApiVersion = i16;
107
108/// Kafka correlation ID for request/response matching.
109pub type CorrelationId = i32;
110
111/// Kafka partition ID.
112pub type PartitionId = i32;
113
114/// Kafka broker ID.
115pub type BrokerId = i32;
116
117/// Kafka offset.
118pub type Offset = i64;
119
120/// Kafka timestamp (milliseconds since epoch).
121pub type Timestamp = i64;