-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
841 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
plugins { | ||
alias(mystere.plugins.kotlin.multiplatform) | ||
alias(mystere.plugins.kotlin.plugin.serialization) | ||
alias(mystere.plugins.buildkonfig) | ||
} | ||
|
||
kotlin { | ||
jvm { | ||
jvmToolchain(17) | ||
testRuns.named("test") { | ||
executionTask.configure { | ||
useJUnitPlatform() | ||
} | ||
} | ||
} | ||
macosArm64() | ||
macosX64() | ||
linuxArm64() | ||
linuxX64() | ||
mingwX64() | ||
|
||
applyDefaultHierarchyTemplate() | ||
|
||
sourceSets { | ||
// common | ||
val commonMain by getting { | ||
dependencies { | ||
implementation(mystere.kotlin.reflect) | ||
implementation(mystere.kotlin.stdlib) | ||
implementation(mystere.kotlinx.serialization.core) | ||
implementation(mystere.kotlinx.serialization.json) | ||
} | ||
} | ||
|
||
// jvm | ||
val jvmMain by getting { | ||
dependencies { | ||
|
||
} | ||
} | ||
|
||
// macos | ||
val macosArm64Main by getting { | ||
dependencies { | ||
|
||
} | ||
} | ||
val macosX64Main by getting { | ||
dependencies { | ||
|
||
} | ||
} | ||
val macosMain by getting { | ||
dependencies { | ||
|
||
} | ||
} | ||
|
||
// linux | ||
val linuxX64Main by getting { | ||
dependencies { | ||
|
||
} | ||
} | ||
val linuxArm64Main by getting { | ||
dependencies { | ||
|
||
} | ||
} | ||
val linuxMain by getting { | ||
dependencies { | ||
|
||
} | ||
} | ||
|
||
// windows | ||
val mingwX64Main by getting { | ||
dependencies { | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
buildkonfig { | ||
packageName = findProperty("mystere.lib.serialization.cqcode.pkgName")!!.toString() | ||
|
||
defaultConfigs { | ||
|
||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...rialization-cqcode/src/commonMain/kotlin/io/github/mystere/serialization/cqcode/CQCode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.github.mystere.serialization.cqcode | ||
|
||
import kotlinx.serialization.DeserializationStrategy | ||
import kotlinx.serialization.SerializationStrategy | ||
import kotlinx.serialization.StringFormat | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.modules.EmptySerializersModule | ||
import kotlinx.serialization.modules.SerializersModule | ||
|
||
sealed class CQCode( | ||
override val serializersModule: SerializersModule | ||
): StringFormat { | ||
override fun <T> decodeFromString(deserializer: DeserializationStrategy<T>, string: String): T { | ||
TODO() | ||
} | ||
override fun <T> encodeToString(serializer: SerializationStrategy<T>, value: T): String { | ||
TODO() | ||
} | ||
|
||
companion object: CQCode( | ||
serializersModule = EmptySerializersModule(), | ||
) { | ||
|
||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
...tion-cqcode/src/commonMain/kotlin/io/github/mystere/serialization/cqcode/CQCodeMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package io.github.mystere.serialization.cqcode | ||
|
||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.SerializationException | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.buildClassSerialDescriptor | ||
import kotlinx.serialization.descriptors.element | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.json.* | ||
|
||
@Serializable(with = CQCodeMessageSerializer::class) | ||
data class CQCodeMessage internal constructor( | ||
internal val chain: ArrayDeque<CQCodeMessageItem> = ArrayDeque() | ||
): List<CQCodeMessageItem> by chain { | ||
operator fun plus(next: CQCodeMessage): CQCodeMessage { | ||
return CQCodeMessage(ArrayDeque(chain).also { | ||
it.addAll(next.chain) | ||
}) | ||
} | ||
operator fun plus(next: CQCodeMessageItem): CQCodeMessage { | ||
return CQCodeMessage(ArrayDeque(chain).also { | ||
it.add(next) | ||
}) | ||
} | ||
|
||
override fun toString(): String { | ||
return with(StringBuilder()) { | ||
for (item in chain) { | ||
append(item.toString()) | ||
} | ||
}.toString() | ||
} | ||
} | ||
|
||
object CQCodeMessageSerializer: KSerializer<CQCodeMessage> { | ||
override val descriptor: SerialDescriptor = buildClassSerialDescriptor( | ||
"io.github.mystere.serializer.cqcode.CQCodeMessage" | ||
) { | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): CQCodeMessage { | ||
try { | ||
when (decoder) { | ||
is JsonDecoder -> { | ||
val jsonObj = decoder.decodeJsonElement().jsonObject | ||
return CQCodeJson.decodeFromJsonElement( | ||
jsonObj["type"]!!.jsonPrimitive.content.cqCodeSerializer(), | ||
jsonObj["data"]!!.jsonObject, | ||
) | ||
} | ||
is CQCodeMessageItemDecoder -> { | ||
val string = decoder.decodeString() | ||
if (string.contains("[(.*?)]".toRegex())) { | ||
if (!string.startsWith("[CQ:") || !string.endsWith("]")) { | ||
throw SerializationException("Not a valid CQCode: $string") | ||
} | ||
val typeEndIndex = if (string.contains(",")) { | ||
string.indexOf(",") | ||
} else { | ||
string.length - 4 | ||
} | ||
return CQCode.decodeFromString( | ||
string.substring(4, typeEndIndex).cqCodeSerializer(), | ||
string, | ||
) | ||
} else { | ||
return CQCodeMessageItem.RawText(string) | ||
} | ||
} | ||
else -> throw SerializationException("Unsupported decoder type: ${decoder::class}") | ||
} | ||
} catch (e: Exception) { | ||
throw SerializationException("Failed to decoder content", e) | ||
} | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: CQCodeMessage) { | ||
try { | ||
when (encoder) { | ||
is JsonEncoder -> { | ||
encoder.encodeJsonElement(buildJsonArray { | ||
for (item in value.chain) { | ||
buildJsonObject { | ||
put("type", JsonPrimitive(item.type)) | ||
put("data", CQCodeJson.encodeToJsonElement(value)) | ||
} | ||
} | ||
}) | ||
} | ||
is CQCodeMessageItemEncoder -> { | ||
encoder.encodeString(with(StringBuilder()) { | ||
for (item in value.chain) { | ||
append(CQCode.encodeToString(item.type.cqCodeSerializer(), item)) | ||
} | ||
}.toString()) | ||
} | ||
else -> throw SerializationException("Unsupported encoder type: ${encoder::class}") | ||
} | ||
} catch (e: Exception) { | ||
throw SerializationException("Failed to encoder content: $value", e) | ||
} | ||
} | ||
} |
Oops, something went wrong.