Skip to main content

oxigdal_pubsub/
lib.rs

1//! OxiGDAL Pub/Sub - Google Cloud Pub/Sub integration for OxiGDAL.
2//!
3//! This crate provides comprehensive support for Google Cloud Pub/Sub messaging,
4//! including publishing, subscribing, schema validation, and monitoring capabilities.
5//!
6//! # Features
7//!
8//! - **Publisher**: Async message publishing with batching and ordering keys
9//! - **Subscriber**: Pull and push subscriptions with flow control
10//! - **Schema Support**: Avro and Protobuf schema validation (feature-gated)
11//! - **Monitoring**: Cloud Monitoring integration for metrics and observability
12//! - **Dead Letter Queues**: Automatic handling of failed messages
13//! - **Flow Control**: Intelligent message throttling and backpressure
14//!
15//! # Example: Publishing Messages
16//!
17//! ```no_run
18//! use oxigdal_pubsub::{Publisher, PublisherConfig, Message};
19//!
20//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
21//! let config = PublisherConfig::new("my-project", "my-topic")
22//!     .with_batching(true)
23//!     .with_batch_size(100);
24//!
25//! let publisher = Publisher::new(config).await?;
26//!
27//! let message = Message::new(b"Hello, Pub/Sub!".to_vec())
28//!     .with_attribute("source", "oxigdal")
29//!     .with_ordering_key("order-1");
30//!
31//! let message_id = publisher.publish(message).await?;
32//! println!("Published message: {}", message_id);
33//! # Ok(())
34//! # }
35//! ```
36//!
37//! # Example: Subscribing to Messages
38//!
39//! ```no_run
40//! use oxigdal_pubsub::{Subscriber, SubscriberConfig, HandlerResult};
41//!
42//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
43//! let config = SubscriberConfig::new("my-project", "my-subscription")
44//!     .with_ack_deadline(30);
45//!
46//! let subscriber = Subscriber::new(config).await?;
47//!
48//! let handle = subscriber.start(|message| {
49//!     println!("Received: {:?}", message.data);
50//!     HandlerResult::Ack
51//! }).await?;
52//!
53//! // Wait for shutdown signal...
54//! subscriber.stop();
55//! # Ok(())
56//! # }
57//! ```
58//!
59//! # Feature Flags
60//!
61//! - `std` (default): Enable standard library support
62//! - `async` (default): Enable async runtime support
63//! - `publisher` (default): Enable publisher functionality
64//! - `subscriber` (default): Enable subscriber functionality
65//! - `schema`: Enable schema support
66//! - `avro`: Enable Apache Avro schema support
67//! - `protobuf`: Enable Protocol Buffers schema support
68//! - `monitoring`: Enable Cloud Monitoring integration
69//! - `batching`: Enable message batching
70//! - `ordering`: Enable message ordering
71//! - `flow-control`: Enable flow control
72//! - `dead-letter`: Enable dead letter queue support
73//!
74//! # Pure Rust Implementation
75//!
76//! This crate uses Pure Rust implementations for all functionality:
77//! - `google-cloud-pubsub` for Pub/Sub operations
78//! - `google-cloud-auth` for authentication
79//! - `google-cloud-monitoring` for monitoring (optional)
80//! - `apache-avro` for Avro schema support (optional)
81//! - `prost` for Protocol Buffers support (optional)
82//!
83//! # COOLJAPAN Policy Compliance
84//!
85//! - ✅ Pure Rust (no C/Fortran dependencies)
86//! - ✅ No `unwrap()` usage (proper error handling)
87//! - ✅ Files under 2000 lines (modular design)
88//! - ✅ Workspace dependencies
89
90#![deny(missing_docs)]
91#![deny(clippy::unwrap_used)]
92#![deny(clippy::panic)]
93#![warn(clippy::expect_used)]
94#![cfg_attr(not(feature = "std"), no_std)]
95
96#[cfg(feature = "alloc")]
97extern crate alloc;
98
99pub mod error;
100
101#[cfg(feature = "publisher")]
102pub mod publisher;
103
104#[cfg(feature = "subscriber")]
105pub mod subscriber;
106
107#[cfg(feature = "schema")]
108pub mod schema;
109
110#[cfg(feature = "monitoring")]
111pub mod monitoring;
112
113pub mod topic;
114
115pub mod subscription;
116
117// Re-exports for convenience
118pub use error::{PubSubError, Result};
119
120#[cfg(feature = "publisher")]
121pub use publisher::{
122    DEFAULT_BATCH_SIZE, DEFAULT_BATCH_TIMEOUT_MS, DEFAULT_MAX_OUTSTANDING_PUBLISHES,
123    MAX_MESSAGE_SIZE, Message, Publisher, PublisherConfig, PublisherStats, RetryConfig,
124};
125
126#[cfg(feature = "subscriber")]
127pub use subscriber::{
128    DEFAULT_ACK_DEADLINE_SECONDS, DEFAULT_HANDLER_CONCURRENCY, DEFAULT_MAX_OUTSTANDING_BYTES,
129    DEFAULT_MAX_OUTSTANDING_MESSAGES, DeadLetterConfig, FlowControlSettings, HandlerResult,
130    ReceivedMessage, Subscriber, SubscriberConfig, SubscriberStats, SubscriptionType,
131};
132
133#[cfg(feature = "schema")]
134pub use schema::{Schema, SchemaEncoding, SchemaRegistry, SchemaValidator};
135
136#[cfg(all(feature = "schema", feature = "avro"))]
137pub use schema::AvroSchema;
138
139#[cfg(all(feature = "schema", feature = "protobuf"))]
140pub use schema::ProtobufSchema;
141
142#[cfg(feature = "monitoring")]
143pub use monitoring::{
144    LatencyTracker, MetricPoint, MetricType, MetricValue, MetricsCollector, MetricsExporter,
145    OperationTimer, PublisherMetrics, SubscriberMetrics,
146};
147
148pub use topic::{TopicBuilder, TopicConfig, TopicManager, TopicMetadata, TopicStats};
149
150#[cfg(feature = "schema")]
151pub use topic::SchemaSettings;
152
153pub use subscription::{
154    DeadLetterPolicy, ExpirationPolicy, RetryPolicy, SubscriptionBuilder, SubscriptionCreateConfig,
155    SubscriptionManager, SubscriptionMetadata, SubscriptionStats,
156};
157
158/// Crate version.
159pub const VERSION: &str = env!("CARGO_PKG_VERSION");
160
161/// Crate name.
162pub const CRATE_NAME: &str = env!("CARGO_PKG_NAME");
163
164/// Gets the crate version.
165pub fn version() -> &'static str {
166    VERSION
167}
168
169/// Gets the crate name.
170pub fn crate_name() -> &'static str {
171    CRATE_NAME
172}
173
174#[cfg(test)]
175mod tests {
176    use super::*;
177
178    #[test]
179    fn test_version() {
180        assert!(!version().is_empty());
181    }
182
183    #[test]
184    fn test_crate_name() {
185        assert_eq!(crate_name(), "oxigdal-pubsub");
186    }
187}