use std::collections::HashMap;
use std::path::PathBuf;
use std::time::Duration;
use serde::{Deserialize, Serialize};
use crate::time::{TimeRange, Timestamp};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ChunkId(pub String);
impl ChunkId {
pub fn from_index(index: usize) -> Self {
Self(format!("chunk-{index:04}"))
}
}
impl std::fmt::Display for ChunkId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct ChunkBoundary {
pub timestamp: Timestamp,
pub is_keyframe: bool,
pub quality: f32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChunkPlan {
pub id: ChunkId,
pub index: usize,
pub range: TimeRange,
pub start_is_keyframe: bool,
pub suggested_timeout: Duration,
}
impl ChunkPlan {
pub fn duration(&self) -> Duration {
self.range.duration()
}
}
#[derive(Debug, Clone)]
pub struct ChunkResult {
pub id: ChunkId,
pub index: usize,
pub range: TimeRange,
pub status: ChunkStatus,
pub output_path: Option<PathBuf>,
pub processing_duration: Duration,
pub metadata: HashMap<String, serde_json::Value>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ChunkStatus {
Completed,
Failed {
message: String,
retryable: bool,
},
Cancelled,
}
impl ChunkStatus {
pub fn is_success(&self) -> bool {
matches!(self, Self::Completed)
}
}
#[derive(Debug, Clone)]
pub struct ChunkProgress {
pub chunk_id: ChunkId,
pub chunk_index: usize,
pub total_chunks: usize,
pub chunk_percent: f32,
pub overall_percent: f32,
pub eta: Option<Duration>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChunkedOperation {
pub chunks: Vec<ChunkPlan>,
pub reassembly: ReassemblyPlan,
pub total_duration: Duration,
pub strategy_name: String,
}
impl ChunkedOperation {
pub fn chunk_count(&self) -> usize {
self.chunks.len()
}
pub fn is_single_chunk(&self) -> bool {
self.chunks.len() <= 1
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub enum ReassemblyPlan {
Concat,
MergeText {
separator: String,
},
Collect,
None,
}