vidsage-core 0.1.0

Core functionality for VidSage video processing and AI commentary generation
Documentation
//! Conversion functions

use crate::commentary::CommentaryStyle;
use crate::video::metadata::{VideoFormat, VideoQuality};
use crate::video::processor::CompressionLevel;

/// Convert a string to a VideoFormat enum
pub fn string_to_video_format(s: &str) -> Option<VideoFormat> {
    match s.to_lowercase().as_str() {
        "mp4" => Some(VideoFormat::MP4),
        "webm" => Some(VideoFormat::WebM),
        "avi" => Some(VideoFormat::AVI),
        "mov" => Some(VideoFormat::MOV),
        "mkv" => Some(VideoFormat::MKV),
        "flv" => Some(VideoFormat::FLV),
        _ => Some(VideoFormat::Other),
    }
}

/// Convert a VideoFormat enum to a string
pub fn video_format_to_string(format: VideoFormat) -> String {
    match format {
        VideoFormat::MP4 => "mp4".to_string(),
        VideoFormat::WebM => "webm".to_string(),
        VideoFormat::AVI => "avi".to_string(),
        VideoFormat::MOV => "mov".to_string(),
        VideoFormat::MKV => "mkv".to_string(),
        VideoFormat::FLV => "flv".to_string(),
        VideoFormat::Other => "other".to_string(),
    }
}

/// Convert a string to a CommentaryStyle enum
pub fn string_to_commentary_style(s: &str) -> Option<CommentaryStyle> {
    match s.to_lowercase().as_str() {
        "professional" => Some(CommentaryStyle::Professional),
        "casual" => Some(CommentaryStyle::Casual),
        "educational" => Some(CommentaryStyle::Educational),
        "entertaining" => Some(CommentaryStyle::Entertaining),
        "analytical" => Some(CommentaryStyle::Analytical),
        "storytelling" => Some(CommentaryStyle::Storytelling),
        "poetic" => Some(CommentaryStyle::Poetic),
        "technical" => Some(CommentaryStyle::Technical),
        _ => None,
    }
}

/// Convert a CommentaryStyle enum to a string
pub fn commentary_style_to_string(style: CommentaryStyle) -> String {
    match style {
        CommentaryStyle::Professional => "professional".to_string(),
        CommentaryStyle::Casual => "casual".to_string(),
        CommentaryStyle::Educational => "educational".to_string(),
        CommentaryStyle::Entertaining => "entertaining".to_string(),
        CommentaryStyle::Analytical => "analytical".to_string(),
        CommentaryStyle::Storytelling => "storytelling".to_string(),
        CommentaryStyle::Poetic => "poetic".to_string(),
        CommentaryStyle::Technical => "technical".to_string(),
    }
}

/// Convert a string to a VideoQuality enum
pub fn string_to_video_quality(s: &str) -> Option<VideoQuality> {
    match s.to_lowercase().as_str() {
        "low" => Some(VideoQuality::Low),
        "medium" => Some(VideoQuality::Medium),
        "high" => Some(VideoQuality::High),
        "ultra" => Some(VideoQuality::Ultra),
        _ => {
            // Try to parse custom quality
            s.parse::<u32>().ok().map(VideoQuality::Custom)
        },
    }
}

/// Convert a VideoQuality enum to a string
pub fn video_quality_to_string(quality: VideoQuality) -> String {
    match quality {
        VideoQuality::Low => "low".to_string(),
        VideoQuality::Medium => "medium".to_string(),
        VideoQuality::High => "high".to_string(),
        VideoQuality::Ultra => "ultra".to_string(),
        VideoQuality::Custom(bitrate) => bitrate.to_string(),
    }
}

/// Convert a string to a CompressionLevel enum
pub fn string_to_compression_level(s: &str) -> Option<CompressionLevel> {
    match s.to_lowercase().as_str() {
        "low" => Some(CompressionLevel::Low),
        "medium" => Some(CompressionLevel::Medium),
        "high" => Some(CompressionLevel::High),
        "ultra" => Some(CompressionLevel::Ultra),
        _ => {
            // Try to parse custom compression level
            s.parse::<f32>()
                .ok()
                .filter(|&x| (0.1..=1.0).contains(&x))
                .map(CompressionLevel::Custom)
        },
    }
}

/// Convert a CompressionLevel enum to a string
pub fn compression_level_to_string(level: CompressionLevel) -> String {
    match level {
        CompressionLevel::Low => "low".to_string(),
        CompressionLevel::Medium => "medium".to_string(),
        CompressionLevel::High => "high".to_string(),
        CompressionLevel::Ultra => "ultra".to_string(),
        CompressionLevel::Custom(ratio) => ratio.to_string(),
    }
}

/// Convert a string to a boolean
pub fn string_to_bool(s: &str) -> Option<bool> {
    match s.to_lowercase().as_str() {
        "true" | "yes" | "1" => Some(true),
        "false" | "no" | "0" => Some(false),
        _ => None,
    }
}

/// Convert a boolean to a string
pub fn bool_to_string(value: bool) -> String {
    if value {
        "true".to_string()
    } else {
        "false".to_string()
    }
}