package com.cheng.blzb.net.model

/**
 * @date 2021-08-05 09:45
 * @desc 数据基本结构
 */
data class HttpBaseResult<T>(val code: Int, val data: T) {

    val status: Boolean
        get() {
            return code == 0
        }
    val message: String = ""
}

/**
 * @param
 * 获取数据转成 Result<T>
 */
fun <T> HttpBaseResult<T>.toResult(): Result<T> {
    return if (this.status) {
        Result.success(this.data)
    } else {
        Result.failure(RuntimeException(this.message))
    }
}

/**
 * @param
 * 获取数据转成 Result<List<T>>
 */
fun <T> HttpBaseResult<HttpListResult<T>>.toListResult(): Result<List<T>> {
    return if (this.status) {
        Result.success(this.data.items)
    } else {
        Result.failure(RuntimeException(this.message))
    }
}
