Skip to content

Commit

Permalink
Restructure (#1)
Browse files Browse the repository at this point in the history
* Restructure

* clippy

* restructure
  • Loading branch information
klaatu01 authored Sep 2, 2024
1 parent 5119a3a commit fb92072
Show file tree
Hide file tree
Showing 13 changed files with 567 additions and 498 deletions.
12 changes: 12 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::{
handler::SkedgyHandler,
scheduler::{SkedgyState, SkedgyTask},
};

pub(crate) enum SkedgyCommand<T: SkedgyHandler> {
Add(SkedgyTask<T>),
Remove(String),
Update(SkedgyTask<T>),
GetState(async_channel::Sender<SkedgyState<T>>),
LoadState(SkedgyState<T>),
}
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use std::time::Duration;

/// Configuration for the `Skedgy` scheduler.
#[derive(Debug, Clone)]
pub struct SkedgyConfig {
pub tick_interval: Duration,
}
3 changes: 3 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub trait SkedgyContext: Clone {}

impl<T: Clone> SkedgyContext for T {}
27 changes: 27 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::error::Error;

/// Errors that can occur during scheduling or running tasks with the `Skedgy` scheduler.
#[derive(Debug)]
pub enum SkedgyError {
/// Error sending a message to the scheduler.
SendError,
/// Error receiving a message from the scheduler.
RecvError,
/// An invalid cron expression was provided.
InvalidCron,
/// An error occurred during a scheduler tick.
TickError,
}

impl Error for SkedgyError {}

impl std::fmt::Display for SkedgyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SkedgyError::SendError => write!(f, "Error sending message to scheduler"),
SkedgyError::RecvError => write!(f, "Error receiving message from scheduler"),
SkedgyError::InvalidCron => write!(f, "Invalid cron expression"),
SkedgyError::TickError => write!(f, "Error during scheduler tick"),
}
}
}
8 changes: 8 additions & 0 deletions src/handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::context::SkedgyContext;

/// A trait for defining task handlers that can be scheduled by the `Skedgy` scheduler.
/// Implement this trait for your task handler and define the task's behavior in the `handle` method.
pub trait SkedgyHandler: Clone + Send + Sync + 'static {
type Context: SkedgyContext + Send + Sync + 'static;
fn handle(&self, ctx: Self::Context) -> impl std::future::Future<Output = ()> + Send;
}
Loading

0 comments on commit fb92072

Please sign in to comment.