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/// Secret store provider for resolving named secrets at runtime
47pub mod secret_store;
48
49/// Write-Ahead Log plugin contract for transient source crash recovery
50pub mod wal;
51
52/// Error types for drasi-lib
53pub mod error;
54
55/// Identity providers for authentication credentials
56pub mod identity;
57
58/// Recovery policy and error types for checkpoint-based recovery
59pub mod recovery;
60
61// ============================================================================
62// Internal Modules (crate-private, but visible to integration tests)
63// ============================================================================
64
65// These modules are internal but need to be accessible to integration tests
66// that test platform-specific components
67#[cfg_attr(not(test), doc(hidden))]
68pub mod bootstrap;
69#[cfg_attr(not(test), doc(hidden))]
70pub mod channels;
71#[cfg_attr(not(test), doc(hidden))]
72pub mod component_ops;
73#[cfg_attr(not(test), doc(hidden))]
74pub mod inspection;
75#[cfg_attr(not(test), doc(hidden))]
76pub mod lib_core;
77#[cfg_attr(not(test), doc(hidden))]
78pub mod lifecycle;
79#[cfg_attr(not(test), doc(hidden))]
80pub mod queries;
81#[cfg_attr(not(test), doc(hidden))]
82pub mod reactions;
83#[cfg_attr(not(test), doc(hidden))]
84pub mod sources;
85
86// Sub-modules for lib_core operations (split for maintainability)
87mod lib_core_ops;
88#[cfg_attr(not(test), doc(hidden))]
89pub mod managers;
90#[cfg_attr(not(test), doc(hidden))]
91pub mod state_guard;
92
93// Config module needs to be public for configuration types
94pub mod config;
95
96// Schema discovery types (separate from configuration)
97pub mod schema;
98
99// Indexes module for storage backend configuration
100pub mod indexes;
101
102// Profiling module for performance monitoring
103#[cfg_attr(not(test), doc(hidden))]
104pub mod profiling;
105
106#[cfg(test)]
107pub(crate) mod test_helpers;
108
109#[cfg(test)]
110mod lifecycle_events_tests;
111
112// ============================================================================
113// Clean Public API - Everything Users Need
114// ============================================================================
115
116/// Main server type - use `DrasiLib::builder()` to create instances
117///
118/// # Examples
119///
120/// ```no_run
121/// use drasi_lib::DrasiLib;
122///
123/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
124/// let core = DrasiLib::builder()
125/// .with_id("my-server")
126/// .build()
127/// .await?;
128/// core.start().await?;
129/// # Ok(())
130/// # }
131/// ```
132pub use lib_core::DrasiLib;
133
134/// Error types for drasi-lib
135pub use error::{DrasiError, Result};
136
137/// Recovery policy and error types for checkpoint-based recovery
138pub use recovery::{ReactionRecoveryPolicy, RecoveryError, RecoveryPolicy};
139
140/// Component status type for monitoring component states
141pub use channels::ComponentStatus;
142
143/// Component event for tracking lifecycle changes
144pub use channels::{ComponentEvent, ComponentType};
145
146/// Subscription response for source subscriptions
147pub use channels::SubscriptionResponse;
148
149/// Dispatch mode for configuring event routing (Broadcast or Channel)
150pub use channels::DispatchMode;
151
152/// Log level and log message types for component log streaming
153pub use managers::{LogLevel, LogMessage};
154
155/// Tracing initialization function - call to set up component log routing
156pub use managers::get_or_init_global_registry;
157
158/// Deprecated tracing initialization functions — use `get_or_init_global_registry()` instead.
159#[allow(deprecated)]
160pub use managers::{init_tracing, try_init_tracing};
161
162// ============================================================================
163// Configuration Types
164// ============================================================================
165
166pub use config::snapshot::QuerySnapshot;
167/// Configuration types
168pub use config::{
169 BootstrapSnapshot, ConfigurationSnapshot, DrasiLibConfig, QueryConfig, QueryLanguage,
170 QueryRuntime, ReactionRuntime, ReactionSnapshot, RuntimeConfig, SourceRuntime, SourceSnapshot,
171 SourceSubscriptionSettings,
172};
173
174/// Schema discovery types (also available via `config::` for backward compatibility)
175pub use schema::{
176 normalize_table_label, GraphNodeSchema, GraphRelationSchema, GraphSchema, NodeSchema,
177 PropertySchema, PropertyType, RelationSchema, SourceSchema,
178};
179
180/// Storage backend configuration types
181pub use indexes::{StorageBackendConfig, StorageBackendRef, StorageBackendSpec};
182
183// ============================================================================
184// Plugin Traits (for plugin development)
185// ============================================================================
186
187/// Source trait for implementing source plugins
188pub use sources::Source;
189
190/// Structured error type for source operations (e.g., replay position unavailable)
191pub use sources::SourceError;
192
193/// Reaction traits for implementing reaction plugins
194pub use reactions::Reaction;
195
196/// Bootstrap provider trait for implementing bootstrap plugins
197pub use bootstrap::BootstrapProvider;
198/// Bootstrap provider that generates data from the component graph
199pub use bootstrap::ComponentGraphBootstrapProvider;
200
201/// Index backend plugin trait for implementing storage backends
202pub use indexes::IndexBackendPlugin;
203
204/// State store provider traits and default implementation
205pub use state_store::{
206 MemoryStateStoreProvider, StateStoreError, StateStoreProvider, StateStoreResult,
207};
208
209/// Secret store provider traits and default implementation
210pub use secret_store::{MemorySecretStoreProvider, SecretStoreProvider};
211
212/// Write-Ahead Log plugin contract and configuration types
213pub use wal::{CapacityPolicy, WalError, WalProvider, WriteAheadLogConfig, MIN_MAX_EVENTS};
214
215/// Runtime context types for plugin initialization
216pub use context::{QueryRuntimeContext, ReactionRuntimeContext, SourceRuntimeContext};
217
218/// Checkpoint type for durable reaction progress tracking
219pub use reactions::ReactionCheckpoint;
220/// Runtime snapshot fetcher trait for on-demand query access
221pub use reactions::SnapshotFetcher;
222/// Base implementations for reaction plugins
223pub use reactions::{ReactionBase, ReactionBaseParams};
224/// Position comparison trait for per-subscriber replay filtering
225pub use sources::{ByteLexPositionComparator, PositionComparator};
226/// Base implementations for source plugins
227pub use sources::{SourceBase, SourceBaseParams};
228
229// ============================================================================
230// Builder Types (for fluent configuration)
231// ============================================================================
232
233/// Fluent builder for DrasiLib instances
234pub use builder::DrasiLibBuilder;
235
236/// Fluent builder for query configurations
237pub use builder::Query;
238
239/// Async helper to wait for a component to reach a target status without polling.
240pub use component_graph::wait_for_status;
241/// Component graph types for dependency tracking and configuration queries
242pub use component_graph::{
243 ComponentGraph, ComponentKind, ComponentNode, GraphEdge, GraphSnapshot, RelationshipKind,
244};
245
246// ============================================================================
247// API Module (backward compatibility alias)
248// ============================================================================
249
250/// Re-export builders as `api` module for backward compatibility with tests.
251/// This allows `use crate::api::{Query};` to work.
252pub mod api {
253 pub use crate::builder::Query;
254}