diff --git a/env/libconductor/.gitignore b/env/libconductor/.gitignore new file mode 100644 index 0000000..5f6d1fa --- /dev/null +++ b/env/libconductor/.gitignore @@ -0,0 +1,2 @@ +/target +.env.local diff --git a/env/libconductor/Cargo.lock b/env/libconductor/Cargo.lock new file mode 100644 index 0000000..4eb2c34 --- /dev/null +++ b/env/libconductor/Cargo.lock @@ -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" diff --git a/env/libconductor/Cargo.toml b/env/libconductor/Cargo.toml new file mode 100644 index 0000000..a7251ed --- /dev/null +++ b/env/libconductor/Cargo.toml @@ -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" diff --git a/env/libconductor/src/lib.rs b/env/libconductor/src/lib.rs new file mode 100644 index 0000000..c61a9d4 --- /dev/null +++ b/env/libconductor/src/lib.rs @@ -0,0 +1,80 @@ +/* + * 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 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; +} + +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); + } +}