81 lines
2.1 KiB
Rust
81 lines
2.1 KiB
Rust
|
/*
|
||
|
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU Affero General Public License as
|
||
|
* published by the Free Software Foundation, either version 3 of the
|
||
|
* License, or (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU Affero General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Affero General Public License
|
||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
use async_trait::async_trait;
|
||
|
use serde::{Deserialize, Serialize};
|
||
|
|
||
|
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
|
||
|
#[serde(untagged)]
|
||
|
pub enum EventType {
|
||
|
NewHostname(String),
|
||
|
}
|
||
|
|
||
|
#[async_trait]
|
||
|
pub trait Conductor: std::marker::Send + std::marker::Sync + CloneConductor {
|
||
|
async fn process(&self, event: EventType);
|
||
|
async fn health(&self) -> bool;
|
||
|
fn name(&self) -> &'static str;
|
||
|
}
|
||
|
|
||
|
/// Trait to clone Conductor
|
||
|
pub trait CloneConductor {
|
||
|
/// clone DB
|
||
|
fn clone_conductor(&self) -> Box<dyn Conductor>;
|
||
|
}
|
||
|
|
||
|
impl<T> CloneConductor for T
|
||
|
where
|
||
|
T: Conductor + Clone + 'static,
|
||
|
{
|
||
|
fn clone_conductor(&self) -> Box<dyn Conductor> {
|
||
|
Box::new(self.clone())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Clone for Box<dyn Conductor> {
|
||
|
fn clone(&self) -> Self {
|
||
|
(**self).clone_conductor()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
use super::*;
|
||
|
|
||
|
#[derive(Clone)]
|
||
|
struct TestConductor;
|
||
|
|
||
|
const TEST_CONDUCTOR_NAME: &str = "TEST_CONDUCTOR";
|
||
|
|
||
|
#[async_trait]
|
||
|
impl Conductor for TestConductor {
|
||
|
async fn process(&self, _event: EventType) {}
|
||
|
fn name(&self) -> &'static str {
|
||
|
TEST_CONDUCTOR_NAME
|
||
|
}
|
||
|
async fn health(&self) -> bool {
|
||
|
true
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn all_good() {
|
||
|
let c = TestConductor {};
|
||
|
assert_eq!(c.name(), TEST_CONDUCTOR_NAME);
|
||
|
assert!(c.health().await);
|
||
|
}
|
||
|
}
|