Crate time

source ·
Expand description

⏳ time-lib

Rust library to manange time.

The core concept is the Timer, which gathers information about the cycle and the state. The Server runs the timer and accepts connection from Clients using ServerBinders. The clients communicate with the server using Requests and Responses, which allow them to control the timer. The timer can be customized using TimerConfig and TimerCycle.

┌────────────────────────┐
│Server                  │
│             ┌────────┐ │ Request ┌────────┐
│             │        │◄├─────────┤        │
│    ┌────────┤Binder A│ │         │Client A│
│    │        │        ├─┼────────►│        │
│    │        └────────┘ │Response └────────┘
│    │                   │
│    ▼        ┌────────┐ │         ┌────────┐
│ ┌─────┐     │        │◄├─────────┤        │
│ │Timer│◄────┤Binder B│ │         │Client B│
│ └─────┘     │        ├─┼────────►│        │
│    ▲        └────────┘ │         └────────┘
│    │                   │
│    │        ┌────────┐ │         ┌────────┐
│    │        │        │◄├─────────┤        │
│    └────────┤Binder C│ │         │Client C│
│             │        ├─┼────────►│        │
│             └────────┘ │         └────────┘
│                        │
└────────────────────────┘

Example using TCP and the Pomodoro technique:

$ cargo run --example pomodoro-tcp
use std::{thread, time::Duration};
use time::{ServerBuilder, ServerEvent, TcpBind, TcpClient, TimerEvent};

const HOST: &str = "127.0.0.1";
const PORT: u16 = 3000;

fn main() {
    let server = ServerBuilder::new()
        .with_server_handler(|event: ServerEvent| {
            println!("server event: {event:?}");
            Ok(())
        })
        .with_timer_handler(|event: TimerEvent| {
            println!("timer event: {event:?}");
            Ok(())
        })
        .with_binder(TcpBind::new(HOST, PORT))
        .with_pomodoro_config()
        .build()
        .unwrap();

    server
        .bind_with(|| {
            // wait for the binder to be ready
            thread::sleep(Duration::from_secs(1));

            let client = TcpClient::new(HOST, PORT);

            client.start().unwrap();
            thread::sleep(Duration::from_secs(1));

            client.pause().unwrap();
            thread::sleep(Duration::from_secs(1));

            let timer = client.get().unwrap();
            println!("current timer: {timer:?}");

            Ok(())
        })
        .unwrap();
}

See more examples.

Modules

Structs

Enums

Traits

Type Aliases