Giter Club home page Giter Club logo

croon's Introduction

croon

croon is a CLI written in rust and provides provides parsing and formatting of standard cron format with five time fields plus a command:

* * * * * command
- - - - -
| | | | |
| | | | +----- day of week    (0 - 6)  (Sunday=0)
| | | +------- month          (1 - 12)
| | +--------- day of month   (1 - 31)
| +----------- hour           (0 - 23)
+------------- minute         (0 - 59)

Usage

Windows, Linux and macOS

Prerequisites

Running
cargo run "*/10 0 1,15 * 1-5 /usr/bin/find"

Testing

cargo test

Design

croon consists of two main parts:

  • CLI entry point
  • Library to parse cron expressions

It has two external dependencies for development convenience: nom, linked_hash_set and lazy_static.

How it works

croon expects only a single input argument and it parses the argument as in the format of minute hour day_of_month month day_of_week command. Everything except command MUST be a valid cron expression otherwise parsing will fail. command is a "free-text" area. Users can write valid/invalid commands and it is not the responsibility of croon to check whether the command could run.

Parser type in parser.rs is just a thin wrapper around cron_table parser combinator. The type is not really necessary to have but it provides a logical distinction between a parser and cron table.

CronTab in cron_table.rs is instantiated by Parser when the parsing is successful. from_cron_expression_list function is a constructor-like function that converts all parsed CronExpressions to lists of u32 values.

When the input argument is parsed successfully, CLI produces an output according the requirement of a table with the field taking the first 14 columns and the times as a space-separated list.

minute        0 10 20 30 40 50
hour          0
day of month  1 15
month         1 2 3 4 5 6 7 8 9 10 11 12
day of week   1 2 3 4 5
command       "/usr/bin/find"

If there is any failure during the parsing, croon will write to stderr and exits with code 1.

CLI

main.rs is the entry point of cron. It is just a thin wrapper around the library and does a few basic checks for error handling and formats crontab output as requested.

Library

Library is where the main functionality of croon is implemented. It uses nom to combine multiple parsers and parses cron expressions according to the standard cron format. There are only tests in the parser which could be considered as functional tests.

Error type is created to enable FromStr trait on CronTab type. It does not do a good job about giving details of the error ocurred during parsing or validation.

The purpose of using linked_hash_set dependency is solely to have HashSet that preserves the order of insertion.

Choosing Rust

As a software engineer, I didn't have any chance to use parsers or parser combinators during my career. However, since I started playing with rust, I found myself implementing parsers for the most of the projects that I delivered. I find rust really comfortable to build parsers because of the ergonomics it provides with enums (union type functionality but in the form of algebraic data type), powerful pattern matching and absence of null.

License

Licensed under MIT license (LICENSE or http://opensource.org/licenses/MIT)

croon's People

Contributors

onatm avatar smoelius avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

croon's Issues

Expand set of characters command can contain?

Would it be possible to allow the command contain everything up to the end of the line? The presently accepted set of character is rather restrictive.

command: take_while!(|c: char| c.is_alphanumeric() || c == '\'' || c == '/' || c.is_whitespace()) >> (String::from(command.0))

Sorry, I don't know much about nom parsers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.