feat: now with to owned as well
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-03-30 23:33:44 +01:00
parent 20ea9fd3c6
commit 8690a06aa4
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394

View File

@ -1,16 +1,14 @@
use std::{ use std::{
collections::BTreeMap, collections::BTreeMap,
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
sync::Arc,
}; };
use async_trait::async_trait; use async_trait::async_trait;
use tokio::sync::Mutex;
pub struct CuddleCI { pub struct CuddleCI {
pr_action: Vec<Arc<Mutex<dyn PullRequestAction + Send + Sync>>>, pr_action: Vec<Box<dyn PullRequestAction + Send + Sync>>,
main_action: Vec<Arc<Mutex<dyn MainAction + Send + Sync>>>, main_action: Vec<Box<dyn MainAction + Send + Sync>>,
release_action: Vec<Arc<Mutex<dyn ReleaseAction + Send + Sync>>>, release_action: Vec<Box<dyn ReleaseAction + Send + Sync>>,
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]
@ -34,9 +32,9 @@ impl DerefMut for Context {
impl CuddleCI { impl CuddleCI {
pub fn new( pub fn new(
pr: Arc<Mutex<dyn PullRequestAction + Send + Sync>>, pr: Box<dyn PullRequestAction + Send + Sync>,
main: Arc<Mutex<dyn MainAction + Send + Sync>>, main: Box<dyn MainAction + Send + Sync>,
release: Arc<Mutex<dyn ReleaseAction + Send + Sync>>, release: Box<dyn ReleaseAction + Send + Sync>,
) -> Self { ) -> Self {
Self { Self {
pr_action: vec![pr], pr_action: vec![pr],
@ -45,25 +43,27 @@ impl CuddleCI {
} }
} }
pub fn with_pull_request<T: PullRequestAction + Send + Sync + 'static>( pub fn with_pull_request<T>(&mut self, pr: T) -> &mut Self
&mut self, where
pr: T, T: PullRequestAction + ToOwned + Send + Sync + 'static,
) -> &mut Self { T: ToOwned<Owned = T>,
self.pr_action.push(Arc::new(Mutex::new(pr))); {
self.pr_action.push(Box::new(pr.to_owned()));
self self
} }
pub fn with_main<T: MainAction + Send + Sync + 'static>(&mut self, main: T) -> &mut Self { pub fn with_main<T>(&mut self, main: T) -> &mut Self
self.main_action.push(Arc::new(Mutex::new(main))); where
T: MainAction + Send + Sync + 'static,
T: ToOwned<Owned = T>,
{
self.main_action.push(Box::new(main));
self self
} }
pub fn with_release( pub fn with_release(&mut self, release: Box<dyn ReleaseAction + Send + Sync>) -> &mut Self {
&mut self,
release: Arc<Mutex<dyn ReleaseAction + Send + Sync>>,
) -> &mut Self {
self.release_action.push(release); self.release_action.push(release);
self self
@ -88,29 +88,21 @@ impl CuddleCI {
("pr", _args) => { ("pr", _args) => {
eprintln!("starting pr validate"); eprintln!("starting pr validate");
for pr_action in self.pr_action.iter() { for pr_action in self.pr_action.iter() {
pr_action pr_action.execute_pull_request(&mut context).await?;
.lock()
.await
.execute_pull_request(&mut context)
.await?;
} }
eprintln!("finished pr validate"); eprintln!("finished pr validate");
} }
("main", _args) => { ("main", _args) => {
eprintln!("starting main validate"); eprintln!("starting main validate");
for main_action in self.main_action.iter() { for main_action in self.main_action.iter() {
main_action.lock().await.execute_main(&mut context).await?; main_action.execute_main(&mut context).await?;
} }
eprintln!("finished main validate"); eprintln!("finished main validate");
} }
("release", _args) => { ("release", _args) => {
eprintln!("starting release validate"); eprintln!("starting release validate");
for release_action in self.release_action.iter() { for release_action in self.release_action.iter() {
release_action release_action.execute_release(&mut context).await?;
.lock()
.await
.execute_release(&mut context)
.await?;
} }
eprintln!("finished release validate"); eprintln!("finished release validate");
} }
@ -128,9 +120,9 @@ impl CuddleCI {
impl Default for CuddleCI { impl Default for CuddleCI {
fn default() -> Self { fn default() -> Self {
Self::new( Self::new(
Arc::new(Mutex::new(DefaultPullRequestAction {})), Box::new(DefaultPullRequestAction {}),
Arc::new(Mutex::new(DefaultMainAction {})), Box::new(DefaultMainAction {}),
Arc::new(Mutex::new(DefaultReleaseAction {})), Box::new(DefaultReleaseAction {}),
) )
} }
} }