Skip to main content

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