Add serializer for SemVersion

This commit is contained in:
Karlatemp 2020-09-18 12:37:40 +08:00
parent 001fac65cc
commit a49efda337
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8

View File

@ -14,8 +14,11 @@
package net.mamoe.mirai.console.util package net.mamoe.mirai.console.util
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient import kotlinx.serialization.Transient
import kotlinx.serialization.builtins.serializer
import net.mamoe.mirai.console.internal.data.map
import net.mamoe.mirai.console.internal.util.SemVersionInternal import net.mamoe.mirai.console.internal.util.SemVersionInternal
import net.mamoe.mirai.console.util.SemVersion.Companion.equals import net.mamoe.mirai.console.util.SemVersion.Companion.equals
@ -44,7 +47,7 @@ import net.mamoe.mirai.console.util.SemVersion.Companion.equals
* 但是不允许 0.0.0.0 之类的存在 * 但是不允许 0.0.0.0 之类的存在
* *
*/ */
@Serializable @Serializable(with = SemVersion.SemVersionAsStringSerializer::class)
public data class SemVersion internal constructor( public data class SemVersion internal constructor(
/** 核心版本号, 由主版本号, 次版本号和修订号组成, 其中修订号不一定存在 */ /** 核心版本号, 由主版本号, 次版本号和修订号组成, 其中修订号不一定存在 */
public val mainVersion: IntArray, public val mainVersion: IntArray,
@ -62,6 +65,11 @@ public data class SemVersion internal constructor(
public fun check(version: SemVersion): Boolean public fun check(version: SemVersion): Boolean
} }
public object SemVersionAsStringSerializer : KSerializer<SemVersion> by String.serializer().map(
serializer = { it.toString() },
deserializer = { parse(it) }
)
public companion object { public companion object {
private val SEM_VERSION_REGEX = private val SEM_VERSION_REGEX =
"""^(0|[1-9]\d*)\.(0|[1-9]\d*)(?:\.(0|[1-9]\d*))?(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$""".toRegex() """^(0|[1-9]\d*)\.(0|[1-9]\d*)(?:\.(0|[1-9]\d*))?(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$""".toRegex()