import { DOWNLOAD_OUTCOME_FILENAME, DOWNLOAD_OUTCOME_STATUS_VALUES, type DownloadOutcomeRecord, type DownloadOutcomeStatus, } from "./downloadOutcome"; import { sidecar, sidecarField } from "./sidecar-server"; export function coerceDownloadOutcome( value: unknown, ): DownloadOutcomeRecord | null { const parsed = value as Partial | null; if ( typeof parsed?.status === "string" && (DOWNLOAD_OUTCOME_STATUS_VALUES as string[]).includes(parsed.status) && typeof parsed.videoId === "string" && typeof parsed.startedAt === "string" && typeof parsed.finishedAt === "string" && Array.isArray(parsed.attempts) ) { // Cast: status is already narrowed to a member of the enum tuple. return parsed as DownloadOutcomeRecord; } return null; } export const downloadOutcomeSidecar = sidecar( DOWNLOAD_OUTCOME_FILENAME, sidecarField(coerceDownloadOutcome), ); export const { path: downloadOutcomePath, load: loadDownloadOutcome, write: writeDownloadOutcome, } = downloadOutcomeSidecar; // Narrow re-export so callers don't have to import from both modules. export type { DownloadOutcomeStatus };