Files
Owlerlay/src-tauri/src/countdown/commands.rs
s0wlz (Matthias Puchstein) 74c9ded46b [bugfix] harden countdown snapshot serialization and error messaging
- guard instant-to-epoch conversion against pre-boot instants
- serialize snapshot duration as milliseconds (u128) in DTO/commands
- derive Display/Error for CountdownError and return user-facing error strings
- remove unused command result alias and minor snapshot lock cleanup
2026-02-26 17:44:07 +01:00

65 lines
1.8 KiB
Rust

use crate::countdown::dto::CountdownSnapshotDto;
use crate::countdown::errors::CountdownError;
use crate::AppState;
use tauri::{command, State};
use tokio::time::Instant;
#[command]
pub async fn countdown_start(state: State<'_, AppState>) -> Result<(), String> {
state
.countdown_service
.start(Instant::now())
.await
.map_err(|e: CountdownError| e.to_string())?;
Ok(())
}
#[command]
pub async fn countdown_reset(state: State<'_, AppState>) -> Result<(), String> {
state.countdown_service.reset().await;
Ok(())
}
#[command]
pub async fn countdown_pause(state: State<'_, AppState>) -> Result<(), String> {
state
.countdown_service
.pause(Instant::now())
.await
.map_err(|e: CountdownError| e.to_string())?;
Ok(())
}
#[command]
pub async fn countdown_resume(state: State<'_, AppState>) -> Result<(), String> {
state
.countdown_service
.resume(Instant::now())
.await
.map_err(|e: CountdownError| e.to_string())?;
Ok(())
}
#[command]
pub async fn countdown_snapshot(
state: State<'_, AppState>,
) -> Result<CountdownSnapshotDto, String> {
let countdown_snapshot = state.countdown_service.snapshot(Instant::now()).await;
let start = match countdown_snapshot.start_instant {
Some(instant) => Some(state.clock_anchor.instant_to_epoch_ms(instant)),
None => None,
};
let target = match countdown_snapshot.target_instant {
Some(instant) => Some(state.clock_anchor.instant_to_epoch_ms(instant)),
None => None,
};
Ok(CountdownSnapshotDto {
id: countdown_snapshot.id,
label: countdown_snapshot.label,
duration: countdown_snapshot.duration.as_millis(),
state: countdown_snapshot.state,
start_epoch_ms: start,
target_epoch_ms: target,
})
}