diff --git a/env/libconductor/Cargo.toml b/env/libconductor/Cargo.toml index a7251ed..70898ab 100644 --- a/env/libconductor/Cargo.toml +++ b/env/libconductor/Cargo.toml @@ -8,4 +8,13 @@ edition = "2021" [dependencies] serde = { version = "1", features=["derive"]} 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 = [] diff --git a/env/libconductor/src/conductor.rs b/env/libconductor/src/conductor.rs new file mode 100644 index 0000000..48a60bd --- /dev/null +++ b/env/libconductor/src/conductor.rs @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2022 Aravinth Manivannan + * + * 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 . + */ +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; +} + +impl CloneConductor for T +where + T: Conductor + Clone + 'static, +{ + fn clone_conductor(&self) -> Box { + Box::new(self.clone()) + } +} + +impl Clone for Box { + 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); + } +} diff --git a/env/libconductor/src/event_types.rs b/env/libconductor/src/event_types.rs new file mode 100644 index 0000000..138821b --- /dev/null +++ b/env/libconductor/src/event_types.rs @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2022 Aravinth Manivannan + * + * 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 . + */ +use serde::{Deserialize, Serialize}; + +#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)] +#[serde(untagged)] +pub enum EventType { + NewHostname(String), +} diff --git a/env/libconductor/src/lib.rs b/env/libconductor/src/lib.rs index c61a9d4..8ba2a03 100644 --- a/env/libconductor/src/lib.rs +++ b/env/libconductor/src/lib.rs @@ -14,67 +14,13 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -use async_trait::async_trait; -use serde::{Deserialize, Serialize}; -#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)] -#[serde(untagged)] -pub enum EventType { - NewHostname(String), -} +#[cfg(feature = "min")] +pub mod event_types; +#[cfg(feature = "min")] +pub use event_types::*; -#[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; -} - -impl CloneConductor for T -where - T: Conductor + Clone + 'static, -{ - fn clone_conductor(&self) -> Box { - Box::new(self.clone()) - } -} - -impl Clone for Box { - 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); - } -} +#[cfg(feature = "full")] +pub mod conductor; +#[cfg(feature = "full")] +pub use conductor::*;