feat: define Conductor trait to impl environment specific side effects

This commit is contained in:
Aravinth Manivannan 2022-10-05 16:56:42 +05:30
parent 1b6af36636
commit 20d02742b3
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
4 changed files with 194 additions and 0 deletions

2
env/libconductor/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
.env.local

101
env/libconductor/Cargo.lock generated vendored Normal file
View file

@ -0,0 +1,101 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "async-trait"
version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76464446b8bc32758d7e88ee1a804d9914cd9b1cb264c029899680b0be29826f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "itoa"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754"
[[package]]
name = "libconductor"
version = "0.1.0"
dependencies = [
"async-trait",
"serde",
"serde_json",
]
[[package]]
name = "proc-macro2"
version = "1.0.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94e2ef8dbfc347b10c094890f778ee2e36ca9bb4262e86dc99cd217e35f3470b"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
[[package]]
name = "serde"
version = "1.0.145"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.145"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81fa1584d3d1bcacd84c277a0dfe21f5b0f6accf4a23d04d4c6d61f1af522b4c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "1.0.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e90cde112c4b9690b8cbe810cba9ddd8bc1d7472e2cae317b69e9438c1cba7d2"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd"

11
env/libconductor/Cargo.toml vendored Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "libconductor"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1", features=["derive"]}
serde_json = { version ="1", features = ["raw_value"]}
async-trait = "0.1.57"

80
env/libconductor/src/lib.rs vendored Normal file
View file

@ -0,0 +1,80 @@
/*
* 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);
}
}