aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/config/migration/LoadResult.kt
blob: b65a8c9fef7f9c7ddbf24b6c6a6a2e3ed7068953 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package at.hannibal2.skyhanni.config.migration

sealed interface LoadResult<out T> {
    fun <V> map(mapper: (T?) -> V?): LoadResult<V> {
        if (this is Instance<T>) {
            return Instance(mapper(this.instance))
        }
        return this as LoadResult<V>
    }

    fun or(other: LoadResult<@UnsafeVariance T>): LoadResult<T>

    data class Instance<T>(val instance: T?) : LoadResult<T> {
        override fun or(other: LoadResult<T>): LoadResult<T> {
            return this
        }
    }

    data class Failure(val exception: Throwable, val path: ResolutionPath) : LoadResult<Nothing> {
        override fun or(other: LoadResult<Nothing>): LoadResult<Nothing> {
            if (other is Invalid || other is Failure) return this
            return other
        }
    }

    object UseDefault : LoadResult<Nothing> {
        override fun or(other: LoadResult<Nothing>): LoadResult<Nothing> {
            if (other is Instance) return other
            return this
        }
    }

    object Invalid : LoadResult<Nothing> {
        override fun or(other: LoadResult<Nothing>): LoadResult<Nothing> {
            return other
        }
    }
}