jellytau/src-tauri/src/commands/conversions.rs
Duncan Tourolle ada3ed64ab Workstream E (backend): wire tauri-specta — annotate commands, derive Type, generate-ready Builder
- Add #[specta::specta] to all 201 #[tauri::command] functions.
- Derive specta::Type on all IPC DTOs (repository/types, settings, player/storage/
  download command DTOs, player enums, jellyfin SessionInfo/NowPlayingItem/PlayState,
  ThumbnailCacheStats, DownloadInfo, CacheConfig, etc.).
- Replace tauri::generate_handler! with a tauri_specta::Builder + collect_commands!
  in lib.rs (exports bindings.ts in debug builds).

Two contract changes required by specta constraints (frontend migration follows):
- specta caps command arity at 10 args: download_item_and_start / download_item /
  download_video now take a single request struct (params bundled, body unchanged
  via destructuring).
- specta can't parse split serde rename_all: SessionInfo/NowPlayingItem/PlayState
  switched to rename_all = "PascalCase" (Jellyfin deserialization preserved; these
  now serialize PascalCase to the frontend).

cargo check --lib is clean (0 errors). Frontend migration to bindings.ts is the next step.
2026-06-20 18:20:25 +02:00

130 lines
3.8 KiB
Rust

//! Tauri commands for unit conversions and formatting
//!
//! These commands expose conversion utilities to the frontend,
//! allowing centralized conversion logic in Rust.
use crate::utils::conversions::{
format_time, format_time_long, calculate_progress,
ticks_to_seconds, percent_to_volume,
};
/// Format time in seconds to MM:SS display string
///
/// # Arguments
/// * `seconds` - Time in seconds
///
/// # Returns
/// Formatted string like "3:45" or "12:09"
#[tauri::command]
#[specta::specta]
pub fn format_time_seconds(seconds: f64) -> String {
format_time(seconds)
}
/// Format time in seconds to HH:MM:SS or MM:SS display string
///
/// Automatically chooses format based on duration:
/// - Less than 1 hour: Returns MM:SS format
/// - 1 hour or more: Returns HH:MM:SS format
///
/// # Arguments
/// * `seconds` - Time in seconds
///
/// # Returns
/// Formatted string like "1:23:45" or "3:45"
#[tauri::command]
#[specta::specta]
pub fn format_time_seconds_long(seconds: f64) -> String {
format_time_long(seconds)
}
/// Convert Jellyfin ticks to seconds
///
/// # Arguments
/// * `ticks` - Time in Jellyfin ticks (10,000,000 ticks = 1 second)
///
/// # Returns
/// Time in seconds
#[tauri::command]
#[specta::specta]
pub fn convert_ticks_to_seconds(ticks: i64) -> f64 {
ticks_to_seconds(ticks)
}
/// Calculate progress percentage from position and duration
///
/// # Arguments
/// * `position` - Current position in seconds
/// * `duration` - Total duration in seconds
///
/// # Returns
/// Progress as percentage (0.0 to 100.0)
#[tauri::command]
#[specta::specta]
pub fn calc_progress(position: f64, duration: f64) -> f64 {
calculate_progress(position, duration)
}
/// Convert percentage volume (0-100) to normalized (0.0-1.0)
///
/// # Arguments
/// * `percent` - Volume as percentage (0 to 100)
///
/// # Returns
/// Normalized volume (0.0 to 1.0)
#[tauri::command]
#[specta::specta]
pub fn convert_percent_to_volume(percent: f64) -> f64 {
percent_to_volume(percent)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_format_time_seconds() {
assert_eq!(format_time_seconds(0.0), "0:00");
assert_eq!(format_time_seconds(59.0), "0:59");
assert_eq!(format_time_seconds(60.0), "1:00");
assert_eq!(format_time_seconds(125.0), "2:05");
assert_eq!(format_time_seconds(3661.0), "61:01");
}
#[test]
fn test_format_time_seconds_long() {
assert_eq!(format_time_seconds_long(0.0), "0:00");
assert_eq!(format_time_seconds_long(59.0), "0:59");
assert_eq!(format_time_seconds_long(3599.0), "59:59");
assert_eq!(format_time_seconds_long(3600.0), "1:00:00");
assert_eq!(format_time_seconds_long(3661.0), "1:01:01");
assert_eq!(format_time_seconds_long(7384.0), "2:03:04");
}
#[test]
fn test_convert_ticks_to_seconds() {
assert_eq!(convert_ticks_to_seconds(0), 0.0);
assert_eq!(convert_ticks_to_seconds(10_000_000), 1.0);
assert_eq!(convert_ticks_to_seconds(5_000_000), 0.5);
assert_eq!(convert_ticks_to_seconds(60_000_000), 6.0);
}
#[test]
fn test_calc_progress() {
assert_eq!(calc_progress(0.0, 100.0), 0.0);
assert_eq!(calc_progress(50.0, 100.0), 50.0);
assert_eq!(calc_progress(100.0, 100.0), 100.0);
assert_eq!(calc_progress(25.0, 0.0), 0.0); // Invalid duration
assert_eq!(calc_progress(150.0, 100.0), 100.0); // Clamped
}
#[test]
fn test_convert_percent_to_volume() {
assert_eq!(convert_percent_to_volume(0.0), 0.0);
assert_eq!(convert_percent_to_volume(50.0), 0.5);
assert_eq!(convert_percent_to_volume(100.0), 1.0);
assert_eq!(convert_percent_to_volume(150.0), 1.0); // Clamped
assert_eq!(convert_percent_to_volume(-10.0), 0.0); // Clamped
}
}