package wang.yaojia.webterm.nav

import kotlin.coroutines.cancellation.CancellationException

/**
 * Like [runCatching] but NEVER swallows structured-concurrency cancellation: a [CancellationException]
 * is rethrown so a cancelled `produceState` / `LaunchedEffect` producer tears down cleanly, while any
 * real failure (a DataStore/transport error) degrades to a [Result.failure] the caller maps to a null /
 * fallback UI state. `inline` so the suspend calls in [block] inline into the caller's suspend context.
 */
internal inline fun <T> runCatchingCancellable(block: () -> T): Result<T> =
    try {
        Result.success(block())
    } catch (cancel: CancellationException) {
        throw cancel
    } catch (error: Throwable) {
        Result.failure(error)
    }
