drasi_lib/lib.rs
1// Copyright 2025 The Drasi Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! # drasi-lib
16//!
17//! Embedded library for running Drasi continuous queries with sources, queries, and reactions.
18//!
19//! ## Error Handling
20//!
21//! All public API methods return [`error::Result<T>`], which uses [`DrasiError`] —
22//! a structured enum that supports pattern matching on failure modes.
23//!
24//! Internal modules and plugin trait implementations use `anyhow::Result` for flexibility.
25//! The [`DrasiError::Internal`] variant (with `#[from] anyhow::Error`) auto-converts
26//! internal errors at the public API boundary via `?`.
27//!
28//! See the [`error`] module documentation for the full error handling architecture.
29
30// ============================================================================
31// Core Public Modules
32// ============================================================================
33
34/// Component dependency graph — the single source of truth for configuration
35pub mod component_graph;
36
37/// Fluent builders for DrasiLib and components
38pub mod builder;
39
40/// Runtime context types for plugin service injection
41pub mod context;
42
43/// State store provider for persistent plugin state
44pub mod state_store;
45
46/// Error types for drasi-lib
47pub mod error;
48
49/// Identity providers for authentication credentials
50pub mod identity;
51
52/// Recovery policy and error types for checkpoint-based recovery
53pub mod recovery;
54
55// ============================================================================
56// Internal Modules (crate-private, but visible to integration tests)
57// ============================================================================
58
59// These modules are internal but need to be accessible to integration tests
60// that test platform-specific components
61#[cfg_attr(not(test), doc(hidden))]
62pub mod bootstrap;
63#[cfg_attr(not(test), doc(hidden))]
64pub mod channels;
65#[cfg_attr(not(test), doc(hidden))]
66pub mod component_ops;
67#[cfg_attr(not(test), doc(hidden))]
68pub mod inspection;
69#[cfg_attr(not(test), doc(hidden))]
70pub mod lib_core;
71#[cfg_attr(not(test), doc(hidden))]
72pub mod lifecycle;
73#[cfg_attr(not(test), doc(hidden))]
74pub mod queries;
75#[cfg_attr(not(test), doc(hidden))]
76pub mod reactions;
77#[cfg_attr(not(test), doc(hidden))]
78pub mod sources;
79
80// Sub-modules for lib_core operations (split for maintainability)
81mod lib_core_ops;
82#[cfg_attr(not(test), doc(hidden))]
83pub mod managers;
84#[cfg_attr(not(test), doc(hidden))]
85pub mod state_guard;
86
87// Config module needs to be public for configuration types
88pub mod config;
89
90// Indexes module for storage backend configuration
91pub mod indexes;
92
93// Profiling module for performance monitoring
94#[cfg_attr(not(test), doc(hidden))]
95pub mod profiling;
96
97#[cfg(test)]
98pub(crate) mod test_helpers;
99
100#[cfg(test)]
101mod lifecycle_events_tests;
102
103// ============================================================================
104// Clean Public API - Everything Users Need
105// ============================================================================
106
107/// Main server type - use `DrasiLib::builder()` to create instances
108///
109/// # Examples
110///
111/// ```no_run
112/// use drasi_lib::DrasiLib;
113///
114/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
115/// let core = DrasiLib::builder()
116/// .with_id("my-server")
117/// .build()
118/// .await?;
119/// core.start().await?;
120/// # Ok(())
121/// # }
122/// ```
123pub use lib_core::DrasiLib;
124
125/// Error types for drasi-lib
126pub use error::{DrasiError, Result};
127
128/// Recovery policy and error types for checkpoint-based recovery
129pub use recovery::{RecoveryError, RecoveryPolicy};
130
131/// Component status type for monitoring component states
132pub use channels::ComponentStatus;
133
134/// Component event for tracking lifecycle changes
135pub use channels::{ComponentEvent, ComponentType};
136
137/// Subscription response for source subscriptions
138pub use channels::SubscriptionResponse;
139
140/// Dispatch mode for configuring event routing (Broadcast or Channel)
141pub use channels::DispatchMode;
142
143/// Log level and log message types for component log streaming
144pub use managers::{LogLevel, LogMessage};
145
146/// Tracing initialization function - call to set up component log routing
147pub use managers::get_or_init_global_registry;
148
149/// Deprecated tracing initialization functions — use `get_or_init_global_registry()` instead.
150#[allow(deprecated)]
151pub use managers::{init_tracing, try_init_tracing};
152
153// ============================================================================
154// Configuration Types
155// ============================================================================
156
157pub use config::snapshot::QuerySnapshot;
158/// Configuration types
159pub use config::{
160 BootstrapSnapshot, ConfigurationSnapshot, DrasiLibConfig, QueryConfig, QueryLanguage,
161 QueryRuntime, ReactionRuntime, ReactionSnapshot, RuntimeConfig, SourceRuntime, SourceSnapshot,
162 SourceSubscriptionSettings,
163};
164
165/// Storage backend configuration types
166pub use indexes::{StorageBackendConfig, StorageBackendRef, StorageBackendSpec};
167
168// ============================================================================
169// Plugin Traits (for plugin development)
170// ============================================================================
171
172/// Source trait for implementing source plugins
173pub use sources::Source;
174
175/// Structured error type for source operations (e.g., replay position unavailable)
176pub use sources::SourceError;
177
178/// Reaction traits for implementing reaction plugins
179pub use reactions::Reaction;
180
181/// Bootstrap provider trait for implementing bootstrap plugins
182pub use bootstrap::BootstrapProvider;
183/// Bootstrap provider that generates data from the component graph
184pub use bootstrap::ComponentGraphBootstrapProvider;
185
186/// Index backend plugin trait for implementing storage backends
187pub use indexes::IndexBackendPlugin;
188
189/// State store provider traits and default implementation
190pub use state_store::{
191 MemoryStateStoreProvider, StateStoreError, StateStoreProvider, StateStoreResult,
192};
193
194/// Runtime context types for plugin initialization
195pub use context::{QueryRuntimeContext, ReactionRuntimeContext, SourceRuntimeContext};
196
197/// Base implementations for reaction plugins
198pub use reactions::{ReactionBase, ReactionBaseParams};
199/// Base implementations for source plugins
200pub use sources::{SourceBase, SourceBaseParams};
201
202// ============================================================================
203// Builder Types (for fluent configuration)
204// ============================================================================
205
206/// Fluent builder for DrasiLib instances
207pub use builder::DrasiLibBuilder;
208
209/// Fluent builder for query configurations
210pub use builder::Query;
211
212/// Async helper to wait for a component to reach a target status without polling.
213pub use component_graph::wait_for_status;
214/// Component graph types for dependency tracking and configuration queries
215pub use component_graph::{
216 ComponentGraph, ComponentKind, ComponentNode, GraphEdge, GraphSnapshot, RelationshipKind,
217};
218
219// ============================================================================
220// API Module (backward compatibility alias)
221// ============================================================================
222
223/// Re-export builders as `api` module for backward compatibility with tests.
224/// This allows `use crate::api::{Query};` to work.
225pub mod api {
226 pub use crate::builder::Query;
227}