11package com.eignex.kencode
22
3- import PackedFormat
43import kotlinx.serialization.*
54import kotlinx.serialization.modules.SerializersModule
65
6+ /* *
7+ * Holds the configuration for an [EncodedFormat] instance.
8+ *
9+ * @property codec The ASCII-safe byte codec used to turn raw bytes into text (e.g., Base62, Base64).
10+ * @property checksum An optional checksum appended to the binary payload and verified upon decoding.
11+ * @property binaryFormat The underlying binary serialization format used before encoding to text.
12+ */
713data class EncodedConfiguration (
814 val codec : ByteEncoding = Base62 ,
915 val checksum : Checksum ? = null ,
1016 val binaryFormat : BinaryFormat = PackedFormat .Default
1117)
1218
1319/* *
14- * Text `StringFormat` that combines :
20+ * Text `StringFormat` that produces short, predictable string tokens by composing :
1521 *
16- * - A binary format (e.g. [PackedFormat], `ProtoBuf`)
17- * - An optional checksum
18- * - An ASCII-safe byte encoding (e.g. [Base62], [Base64], [Base36], [Base85])
22+ * 1. A binary format (e.g. [PackedFormat], `ProtoBuf`).
23+ * 2. An optional checksum.
24+ * 3. An ASCII-safe byte encoding (e.g. [Base62], [Base64], [Base36], [Base85]).
1925 *
2026 * Typical use:
27+ * - `encodeToString`: serialize -> (optionally) append checksum -> encode bytes to text.
28+ * - `decodeFromString`: decode text to bytes -> (optionally) verify checksum -> deserialize.
2129 *
22- * - `encodeToString`: serialize → (optionally) append checksum → encode bytes
23- * - `decodeFromString`: decode bytes → (optionally) verify checksum → deserialize
30+ * Use the [EncodedFormat] builder function to create a customized instance.
2431 *
25- * This is intended for short, predictable tokens for URLs, headers, file names, etc.
26- *
27- * @property codec ASCII-safe byte codec used to turn raw bytes into text.
28- * @property checksum Optional checksum appended to the binary payload and verified on decode.
29- * @property binaryFormat Binary serialization format used before encoding.
32+ * @property configuration The active configuration dictating the codec, checksum, and binary format.
3033 */
3134@OptIn(ExperimentalSerializationApi ::class )
3235open class EncodedFormat (
3336 val configuration : EncodedConfiguration ,
3437) : StringFormat {
3538
39+ /* *
40+ * Secondary constructor for direct instantiation without the builder.
41+ */
3642 constructor (
3743 codec: ByteEncoding = Base62 ,
3844 checksum: Checksum ? = null ,
3945 binaryFormat: BinaryFormat = PackedFormat ,
4046 ) : this (EncodedConfiguration (codec, checksum, binaryFormat))
4147
48+ /* *
49+ * Delegates to the underlying [BinaryFormat]'s serializers module.
50+ */
4251 override val serializersModule: SerializersModule get() = configuration.binaryFormat.serializersModule
4352
4453 /* *
45- * Default format: `PackedFormat` + `Base62` without checksum.
54+ * Default format: `PackedFormat` + `Base62` without a checksum.
4655 */
4756 companion object Default : EncodedFormat()
4857
4958 /* *
50- * Serializes [value] with [binaryFormat] , optionally appends [ checksum] ,
51- * and encodes the result using [ codec] .
59+ * Serializes [value] with the configured binary format , optionally appends a checksum,
60+ * and encodes the resulting byte array using the text codec.
5261 */
5362 override fun <T > encodeToString (
5463 serializer : SerializationStrategy <T >, value : T
@@ -61,10 +70,10 @@ open class EncodedFormat(
6170 }
6271
6372 /* *
64- * Decodes [string] using [ codec] , optionally verifies [ checksum] ,
65- * then deserializes the remaining bytes with [binaryFormat] .
73+ * Decodes [string] using the text codec, optionally verifies and strips the checksum,
74+ * then deserializes the remaining bytes with the configured binary format .
6675 *
67- * @throws IllegalArgumentException if a checksum is configured and does not match .
76+ * @throws IllegalArgumentException if a checksum is configured and the verification fails .
6877 */
6978 override fun <T > decodeFromString (
7079 deserializer : DeserializationStrategy <T >, string : String
@@ -85,12 +94,39 @@ open class EncodedFormat(
8594 }
8695}
8796
97+ / **
98+ * Builder for configuring [EncodedFormat ] instances.
99+ * /
88100class EncodedFormatBuilder {
101+ / **
102+ * The ASCII - safe byte codec used to turn raw bytes into text. Defaults to [Base62 ].
103+ * /
89104 var codec: ByteEncoding = Base62
105+
106+ / **
107+ * An optional checksum appended to the binary payload. Defaults to `null`.
108+ * /
90109 var checksum: Checksum ? = null
110+
111+ / **
112+ * The underlying binary serialization format. Defaults to [PackedFormat .Default ].
113+ * /
91114 var binaryFormat: BinaryFormat = PackedFormat .Default
92115}
93116
117+ / **
118+ * Creates a customized [EncodedFormat ] instance.
119+ *
120+ * ```
121+ * val format = EncodedFormat {
122+ * codec = Base36
123+ * checksum = Crc16
124+ * }
125+ * ```
126+ *
127+ * @param from An existing [EncodedFormat ] instance to copy defaults from.
128+ * @param builderAction The configuration block.
129+ * /
94130fun EncodedFormat (
95131 from: EncodedFormat = EncodedFormat .Default ,
96132 builderAction: EncodedFormatBuilder .() - > Unit
0 commit comments