Skip to main content

drasi_reaction_application/
lib.rs

1#![allow(unexpected_cfgs)]
2// Copyright 2025 The Drasi Authors.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Application reaction plugin for Drasi
17//!
18//! This plugin implements Application reactions for Drasi.
19//!
20//! # Example
21//!
22//! ```rust,ignore
23//! use drasi_reaction_application::ApplicationReaction;
24//!
25//! let (reaction, handle) = ApplicationReaction::builder("my-app-reaction")
26//!     .with_queries(vec!["query1".to_string()])
27//!     .build();
28//!
29//! // Use handle to receive results
30//! let mut subscription = handle.subscribe_with_options(Default::default()).await?;
31//! while let Some(result) = subscription.recv().await {
32//!     println!("Result: {:?}", result);
33//! }
34//! ```
35
36pub mod application;
37pub mod config;
38pub mod descriptor;
39pub mod subscription;
40
41pub use application::ApplicationReaction;
42pub use application::ApplicationReactionHandle;
43pub use config::ApplicationReactionConfig;
44
45/// Builder for Application reaction
46pub struct ApplicationReactionBuilder {
47    id: String,
48    queries: Vec<String>,
49    priority_queue_capacity: Option<usize>,
50    auto_start: bool,
51}
52
53impl ApplicationReactionBuilder {
54    /// Create a new Application reaction builder with the given ID
55    pub fn new(id: impl Into<String>) -> Self {
56        Self {
57            id: id.into(),
58            queries: Vec::new(),
59            priority_queue_capacity: None,
60            auto_start: true,
61        }
62    }
63
64    /// Set the query IDs to subscribe to
65    pub fn with_queries(mut self, queries: Vec<String>) -> Self {
66        self.queries = queries;
67        self
68    }
69
70    /// Add a query ID to subscribe to
71    pub fn with_query(mut self, query_id: impl Into<String>) -> Self {
72        self.queries.push(query_id.into());
73        self
74    }
75
76    /// Set the priority queue capacity
77    pub fn with_priority_queue_capacity(mut self, capacity: usize) -> Self {
78        self.priority_queue_capacity = Some(capacity);
79        self
80    }
81
82    /// Set whether the reaction should auto-start
83    pub fn with_auto_start(mut self, auto_start: bool) -> Self {
84        self.auto_start = auto_start;
85        self
86    }
87
88    /// Build the Application reaction
89    ///
90    /// Returns a tuple of (reaction, handle) where the handle can be used
91    /// to receive query results in the application.
92    pub fn build(self) -> (ApplicationReaction, ApplicationReactionHandle) {
93        ApplicationReaction::from_builder(
94            self.id,
95            self.queries,
96            self.priority_queue_capacity,
97            self.auto_start,
98        )
99    }
100}
101
102#[cfg(test)]
103mod tests {
104    use super::*;
105    use drasi_lib::Reaction;
106
107    #[test]
108    fn test_application_builder_defaults() {
109        let (reaction, _handle) = ApplicationReactionBuilder::new("test-reaction").build();
110        assert_eq!(reaction.id(), "test-reaction");
111    }
112
113    #[test]
114    fn test_application_builder_custom() {
115        let (reaction, _handle) = ApplicationReaction::builder("test-reaction")
116            .with_queries(vec!["query1".to_string()])
117            .build();
118
119        assert_eq!(reaction.id(), "test-reaction");
120        assert_eq!(reaction.query_ids(), vec!["query1".to_string()]);
121    }
122
123    #[test]
124    fn test_application_new_constructor() {
125        let (reaction, _handle) =
126            ApplicationReaction::new("test-reaction", vec!["query1".to_string()]);
127        assert_eq!(reaction.id(), "test-reaction");
128    }
129}
130
131// Core plugin — registered statically by the server, not exported for dynamic loading.