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
- Server
serverThe server struct. - ServerBuilder
serverThe server builder. - ServerConfig
serverThe server configuration. - TcpBind
tcp-binderandserverThe TCP server binder. - TcpClient
tcp-clientandclientThe TCP client. - ThreadSafeState
serverThread safe version of theServerState. - Thread safe version of the
Timer. - The timer struct.
- The timer configuration.
- The timer cycle.
- The timer cycles list.
Enums
- The request struct.
- The response struct.
- ServerEvent
serverThe server state changed event. - ServerState
serverThe server state enum. - The timer event.
- The timer loop.
- The timer state.
Traits
- Client
clientThe client trait. - ClientStream
clientThe client stream trait. - ServerBind
serverThe server bind trait. - ServerStream
serverThe server stream trait.
Type Aliases
- The server state changed handler alias.
- The timer changed handler.