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
22#[derive(Error, Debug)]
23pub enum MiddlewareError {
24    #[error("Error processing source change: {0}")]
25    SourceChangeError(String),
26
27    #[error("Unknown middleware {0}")]
28    UnknownKind(String),
29}
30
31#[derive(Error, Debug)]
32pub enum MiddlewareSetupError {
33    #[error("Invalid configuration: {0}")]
34    InvalidConfiguration(String),
35    #[error("No registry found for middleware")]
36    NoRegistry,
37}
38
39#[async_trait]
40pub trait SourceMiddleware: Send + Sync {
41    async fn process(
42        &self,
43        source_change: SourceChange,
44    ) -> Result<Vec<SourceChange>, MiddlewareError>;
45}
46
47pub trait SourceMiddlewareFactory: Send + Sync {
48    fn name(&self) -> String;
49    fn create(
50        &self,
51        config: &SourceMiddlewareConfig,
52    ) -> Result<Arc<dyn SourceMiddleware>, MiddlewareSetupError>;
53    //todo: inject dependencies such as element index, expression evaluator, etc.
54}