1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 03:58:44 +00:00

upfix of chatmember statuses serialization

This commit is contained in:
InsanusMokrassar 2023-09-29 22:38:22 +06:00
parent 38c9732da5
commit 284fe58848

View File

@ -7,6 +7,7 @@ import dev.inmo.tgbotapi.utils.nonstrictJsonFormat
import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.encoding.Encoder
@ -15,8 +16,11 @@ import kotlinx.serialization.json.jsonPrimitive
@Serializable(ChatMemberSerializer::class) @Serializable(ChatMemberSerializer::class)
sealed interface ChatMember : WithUser { sealed interface ChatMember : WithUser {
@Serializable @Serializable(StatusSerializer::class)
enum class Status(val status: String, val deserializationStrategy: DeserializationStrategy<ChatMember>) { enum class Status(
val status: String,
val deserializationStrategy: DeserializationStrategy<ChatMember>
) {
Creator("creator", OwnerChatMember.serializer()), Creator("creator", OwnerChatMember.serializer()),
Administrator("administrator", AdministratorChatMemberImpl.serializer()), Administrator("administrator", AdministratorChatMemberImpl.serializer()),
Member("member", MemberChatMemberImpl.serializer()), Member("member", MemberChatMemberImpl.serializer()),
@ -24,6 +28,23 @@ sealed interface ChatMember : WithUser {
Left("left", LeftChatMemberImpl.serializer()), Left("left", LeftChatMemberImpl.serializer()),
Kicked("kicked", KickedChatMember.serializer()) Kicked("kicked", KickedChatMember.serializer())
} }
object StatusSerializer : KSerializer<Status> {
override val descriptor: SerialDescriptor
get() = String.serializer().descriptor
override fun deserialize(decoder: Decoder): Status {
val status = decoder.decodeString()
return Status.values().first {
it.status == status
}
}
override fun serialize(encoder: Encoder, value: Status) {
encoder.encodeString(value.status)
}
}
val status: Status val status: Status
} }