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