Skip to main content

#
jellytau_lib/commands/
conversions.rs

    1//! Tauri commands for unit conversions and formatting
    2//!
    3//! TRACES: UR-005 | DR-009
    4//!
    5//! These commands expose conversion utilities to the frontend,
    6//! allowing centralized conversion logic in Rust.
    7
    8use crate::utils::conversions::{
    9    calculate_progress, format_time, format_time_long, percent_to_volume, ticks_to_seconds,
    10};
    11
    12/// Format time in seconds to MM:SS display string
    13///
    14/// # Arguments
    15/// * `seconds` - Time in seconds
    16///
    17/// # Returns
    18/// Formatted string like "3:45" or "12:09"
    19#[tauri::command]
    20#[specta::specta]
    21pub fn format_time_seconds(seconds: f64) -> String {
    22    format_time(seconds)
    23}
    24
    25/// Format time in seconds to HH:MM:SS or MM:SS display string
    26///
    27/// Automatically chooses format based on duration:
    28/// - Less than 1 hour: Returns MM:SS format
    29/// - 1 hour or more: Returns HH:MM:SS format
    30///
    31/// # Arguments
    32/// * `seconds` - Time in seconds
    33///
    34/// # Returns
    35/// Formatted string like "1:23:45" or "3:45"
    36#[tauri::command]
    37#[specta::specta]
    38pub fn format_time_seconds_long(seconds: f64) -> String {
    39    format_time_long(seconds)
    40}
    41
    42/// Convert Jellyfin ticks to seconds
    43///
    44/// # Arguments
    45/// * `ticks` - Time in Jellyfin ticks (10,000,000 ticks = 1 second)
    46///
    47/// # Returns
    48/// Time in seconds
    49#[tauri::command]
    50#[specta::specta]
    51pub fn convert_ticks_to_seconds(ticks: i64) -> f64 {
    52    ticks_to_seconds(ticks)
    53}
    54
    55/// Calculate progress percentage from position and duration
    56///
    57/// # Arguments
    58/// * `position` - Current position in seconds
    59/// * `duration` - Total duration in seconds
    60///
    61/// # Returns
    62/// Progress as percentage (0.0 to 100.0)
    63#[tauri::command]
    64#[specta::specta]
    65pub fn calc_progress(position: f64, duration: f64) -> f64 {
    66    calculate_progress(position, duration)
    67}
    68
    69/// Convert percentage volume (0-100) to normalized (0.0-1.0)
    70///
    71/// # Arguments
    72/// * `percent` - Volume as percentage (0 to 100)
    73///
    74/// # Returns
    75/// Normalized volume (0.0 to 1.0)
    76#[tauri::command]
    77#[specta::specta]
    78pub fn convert_percent_to_volume(percent: f64) -> f64 {
    79    percent_to_volume(percent)
    80}
    81
    82#[cfg(test)]
    83mod tests {
    84    use super::*;
    85
    86    #[test]
    87    fn test_format_time_seconds() {
    88        assert_eq!(format_time_seconds(0.0), "0:00");
    89        assert_eq!(format_time_seconds(59.0), "0:59");
    90        assert_eq!(format_time_seconds(60.0), "1:00");
    91        assert_eq!(format_time_seconds(125.0), "2:05");
    92        assert_eq!(format_time_seconds(3661.0), "61:01");
    93    }
    94
    95    #[test]
    96    fn test_format_time_seconds_long() {
    97        assert_eq!(format_time_seconds_long(0.0), "0:00");
    98        assert_eq!(format_time_seconds_long(59.0), "0:59");
    99        assert_eq!(format_time_seconds_long(3599.0), "59:59");
    100        assert_eq!(format_time_seconds_long(3600.0), "1:00:00");
    101        assert_eq!(format_time_seconds_long(3661.0), "1:01:01");
    102        assert_eq!(format_time_seconds_long(7384.0), "2:03:04");
    103    }
    104
    105    #[test]
    106    fn test_convert_ticks_to_seconds() {
    107        assert_eq!(convert_ticks_to_seconds(0), 0.0);
    108        assert_eq!(convert_ticks_to_seconds(10_000_000), 1.0);
    109        assert_eq!(convert_ticks_to_seconds(5_000_000), 0.5);
    110        assert_eq!(convert_ticks_to_seconds(60_000_000), 6.0);
    111    }
    112
    113    #[test]
    114    fn test_calc_progress() {
    115        assert_eq!(calc_progress(0.0, 100.0), 0.0);
    116        assert_eq!(calc_progress(50.0, 100.0), 50.0);
    117        assert_eq!(calc_progress(100.0, 100.0), 100.0);
    118        assert_eq!(calc_progress(25.0, 0.0), 0.0); // Invalid duration
    119        assert_eq!(calc_progress(150.0, 100.0), 100.0); // Clamped
    120    }
    121
    122    #[test]
    123    fn test_convert_percent_to_volume() {
    124        assert_eq!(convert_percent_to_volume(0.0), 0.0);
    125        assert_eq!(convert_percent_to_volume(50.0), 0.5);
    126        assert_eq!(convert_percent_to_volume(100.0), 1.0);
    127        assert_eq!(convert_percent_to_volume(150.0), 1.0); // Clamped
    128        assert_eq!(convert_percent_to_volume(-10.0), 0.0); // Clamped
    129    }
    130}