package com.quillstudios.pokegoalshelper.ml /** * Result wrapper for ML operations to standardize error handling */ sealed class MLResult { data class Success(val data: T) : MLResult() data class Error(val exception: Throwable, val message: String) : MLResult() /** * Returns the data if successful, or null if error */ fun getOrNull(): T? = when (this) { is Success -> data is Error -> null } /** * Returns the data if successful, or the default value if error */ fun getOrDefault(default: T): T = when (this) { is Success -> data is Error -> default } /** * Execute block if successful */ inline fun onSuccess(block: (T) -> Unit): MLResult { if (this is Success) block(data) return this } /** * Execute block if error */ inline fun onError(block: (Throwable, String) -> Unit): MLResult { if (this is Error) block(exception, message) return this } /** * Transform the data if successful */ inline fun map(transform: (T) -> R): MLResult = when (this) { is Success -> try { Success(transform(data)) } catch (e: Exception) { Error(e, "Transform failed: ${e.message}") } is Error -> this } } /** * Helper functions for creating results */ inline fun mlTry(operation: () -> T): MLResult { return try { MLResult.Success(operation()) } catch (e: Exception) { MLResult.Error(e, "Operation failed: ${e.message}") } } inline fun mlTryWithMessage(message: String, operation: () -> T): MLResult { return try { MLResult.Success(operation()) } catch (e: Exception) { MLResult.Error(e, "$message: ${e.message}") } }