package org.thoughtcrime.securesms.repository import kotlinx.coroutines.CancellationException sealed class ResultOf { data class Success(val value: R) : ResultOf() data class Failure(val throwable: Throwable) : ResultOf() inline fun onFailure(block: (throwable: Throwable) -> Unit) = this.also { if (this is Failure) { block(throwable) } } inline fun onSuccess(block: (value: T) -> Unit) = this.also { if (this is Success) { block(value) } } inline fun flatMap(mapper: (T) -> R): ResultOf = when (this) { is Success -> wrap { mapper(value) } is Failure -> Failure(throwable) } fun getOrThrow(): T = when (this) { is Success -> value is Failure -> throw throwable } companion object { inline fun wrap(block: () -> T): ResultOf = try { Success(block()) } catch (e: CancellationException) { throw e } catch (e: Exception) { Failure(e) } } }