feat: feature gate libconductor::Conductor trait implementation to ship
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

minimal event types only version
This commit is contained in:
Aravinth Manivannan 2022-11-16 15:39:46 +05:30
parent 45a5d5ce9f
commit 2e3d6999ff
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
4 changed files with 115 additions and 63 deletions

View file

@ -8,4 +8,13 @@ edition = "2021"
[dependencies] [dependencies]
serde = { version = "1", features=["derive"]} serde = { version = "1", features=["derive"]}
serde_json = { version ="1", features = ["raw_value"]} serde_json = { version ="1", features = ["raw_value"]}
async-trait = "0.1.57" async-trait = { version = "0.1.57", optional = true}
[features]
default = [
"min",
"full",
]
full = ["async-trait", "min"]
min = []

74
env/libconductor/src/conductor.rs vendored Normal file
View file

@ -0,0 +1,74 @@
/*
* 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 crate::event_types::EventType;
use async_trait::async_trait;
#[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);
}
}

23
env/libconductor/src/event_types.rs vendored Normal file
View file

@ -0,0 +1,23 @@
/*
* 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 serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
#[serde(untagged)]
pub enum EventType {
NewHostname(String),
}

View file

@ -14,67 +14,13 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * 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)] #[cfg(feature = "min")]
#[serde(untagged)] pub mod event_types;
pub enum EventType { #[cfg(feature = "min")]
NewHostname(String), pub use event_types::*;
}
#[async_trait] #[cfg(feature = "full")]
pub trait Conductor: std::marker::Send + std::marker::Sync + CloneConductor { pub mod conductor;
async fn process(&self, event: EventType); #[cfg(feature = "full")]
async fn health(&self) -> bool; pub use conductor::*;
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);
}
}