Skip to content

Commit

Permalink
Tests (#12)
Browse files Browse the repository at this point in the history
* bump version

* Add dev deps

* Fix badge

* Split into modules

* Clean up README and doc comment

* Job unit tests
  • Loading branch information
deciduously committed Sep 7, 2021
1 parent 83bc28a commit a3f92d3
Show file tree
Hide file tree
Showing 8 changed files with 991 additions and 700 deletions.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "skedge"
description = "Ergonomic single-process job scheduling for Rust programs."
version = "0.0.4"
version = "0.0.5"
edition = "2018"
authors = ["Ben Lovy <ben@deciduously.com>"]
documentation = "https://docs.rs/skedge"
Expand All @@ -22,6 +22,11 @@ rand = "0.8"
regex = "1.5"
thiserror = "1"

[dev-dependencies]

mockall = "0.10"
pretty_assertions = "0.7"

[profile]
[profile.release]
lto = true
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# skedge

[![Crates.io](https://img.shields.io/crates/v/skedge.svg)](https://crates.io/crates/skedge)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/deciduously/skedge/rust)](https://github.com/deciduously/skedge/actions/workflows/rust.yml)

**WIP - USE AT OWN RISK**
[![rust action](https://github.com/deciduously/skedge/actions/workflows/rust.yml/badge.svg)](https://github.com/deciduously/skedge/actions/workflows/rust.yml)

Rust single-process scheduling. Ported from [`schedule`](https://github.com/dbader/schedule) for Python, in turn inspired by [`clockwork`](https://github.com/Rykian/clockwork) (Ruby), and ["Rethinking Cron"](https://adam.herokuapp.com/past/2010/4/13/rethinking_cron/) by [Adam Wiggins](https://github.com/adamwiggins).

While most of it should work, only jobs which take no parameters and return nothing can be scheduled. Also, I haven't written tests yet, so there's really no guarantee any of it works like it should. This is a pre-release. Stay tuned.
**NOTE**: This library is currently limited to jobs which take no arguments and return nothing.

## Usage

Expand Down Expand Up @@ -71,3 +69,8 @@ Clone this repo. See [`CONTRIBUTING.md`](https://github.com/deciduously/skedge/
* [rand](https://rust-random.github.io/book/) - Random number generation
* [regex](https://github.com/rust-lang/regex) - Regular expressions
* [thiserror](https://github.com/dtolnay/thiserror) - Error derive macro

#### Development-Only

* [mockall](https://github.com/asomers/mockall) - Mock objects
* [pretty_assertions](https://github.com/colin-kiegel/rust-pretty-assertions) - Colorful assertion output
47 changes: 47 additions & 0 deletions src/callable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! The work functions that can be scheduled must implement the `Callable` trait.

use std::fmt;

/// A job is anything that implements this trait
pub(crate) trait Callable: fmt::Debug {
/// Execute this callable
fn call(&self) -> Option<bool>;
/// Get the name of this callable
fn name(&self) -> &str;
}

impl PartialEq for dyn Callable {
fn eq(&self, other: &Self) -> bool {
// Callable objects are equal if their names are equal
// FIXME: this seems fishy
self.name() == other.name()
}
}

impl Eq for dyn Callable {}

/// A named callable function taking no parameters and returning nothing.
#[derive(Debug)]
pub struct UnitToUnit {
name: String,
work: fn() -> (),
}

impl UnitToUnit {
pub fn new(name: &str, work: fn() -> ()) -> Self {
Self {
name: name.into(),
work,
}
}
}

impl Callable for UnitToUnit {
fn call(&self) -> Option<bool> {
(self.work)();
None
}
fn name(&self) -> &str {
&self.name
}
}
10 changes: 5 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! This module defines the error type and Result alias.

use super::TimeUnit;
use crate::TimeUnit;
use chrono::Weekday;
use thiserror::Error;

#[derive(Debug, Error)]
#[derive(Debug, PartialEq, Error)]
pub enum SkedgeError {
#[error("Use {0}s() instead of {0}()")]
Interval(TimeUnit),
Expand All @@ -16,11 +16,11 @@ pub enum SkedgeError {
InvalidUnit,
#[error("Invalid hour ({0} is not between 0 and 23)")]
InvalidHour(u32),
#[error("Invalid time format for daily job (valid format is HH:MM(:SS)?")]
#[error("Invalid time format for daily job (valid format is HH:MM(:SS)?)")]
InvalidDailyAtStr,
#[error("Invalid time format for hourly job (valid format is (MM)?:SS")]
#[error("Invalid time format for hourly job (valid format is (MM)?:SS)")]
InvalidHourlyAtStr,
#[error("Invalid time format for minutely job (valid format is :SS")]
#[error("Invalid time format for minutely job (valid format is :SS)")]
InvalidMinuteAtStr,
#[error("Invalid string format for until()")]
InvalidUntilStr,
Expand Down
Loading

0 comments on commit a3f92d3

Please sign in to comment.