drasi_core/interface/
source_middleware.rs

1// Copyright 2024 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
15use std::sync::Arc;
16
17use async_trait::async_trait;
18use thiserror::Error;
19
20use crate::models::{SourceChange, SourceMiddlewareConfig};
21
22use super::{ElementIndex, IndexError};
23
24#[derive(Error, Debug)]
25pub enum MiddlewareError {
26    #[error("Error processing source change: {0}")]
27    SourceChangeError(String),
28
29    #[error("Unknown middleware {0}")]
30    UnknownKind(String),
31
32    #[error("Error processing source change: {0}")]
33    IndexError(IndexError),
34}
35
36impl From<IndexError> for MiddlewareError {
37    fn from(val: IndexError) -> Self {
38        MiddlewareError::IndexError(val)
39    }
40}
41
42#[derive(Error, Debug)]
43pub enum MiddlewareSetupError {
44    #[error("Invalid configuration: {0}")]
45    InvalidConfiguration(String),
46    #[error("No registry found for middleware")]
47    NoRegistry,
48}
49
50#[async_trait]
51pub trait SourceMiddleware: Send + Sync {
52    async fn process(
53        &self,
54        source_change: SourceChange,
55        element_index: &dyn ElementIndex,
56    ) -> Result<Vec<SourceChange>, MiddlewareError>;
57}
58
59pub trait SourceMiddlewareFactory: Send + Sync {
60    fn name(&self) -> String;
61    fn create(
62        &self,
63        config: &SourceMiddlewareConfig,
64    ) -> Result<Arc<dyn SourceMiddleware>, MiddlewareSetupError>;
65    //todo: inject dependencies such as element index, expression evaluator, etc.
66}