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