Skip to content

Commit a9e30d8

Browse files
committed
feat: Enum codec helpers
1 parent f1ea2f0 commit a9e30d8

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

module-ped-bson/src/commonMain/kotlin/BsonCodecs.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ fun <I> BsonCodec(block: context(BsonCodecBuilder<I>) () -> Unit): BsonCodec<I>
3838
return Codec(block)
3939
}
4040

41+
inline fun <reified I : Enum<I>> BsonEnumCodec(block: (I) -> BsonElement): BsonEnumCodec<I> {
42+
return EnumCodec(block)
43+
}
44+
4145
/* ============= ------------------ ============= */
4246

4347
/**

module-ped-core/src/commonMain/kotlin/Codecs.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ package org.cufy.ped
22

33
import kotlin.Result.Companion.failure
44
import kotlin.Result.Companion.success
5+
import kotlin.enums.enumEntries
56

67
/* ============= ------------------ ============= */
78

9+
inline fun <reified I : Enum<I>, O> EnumCodec(block: (I) -> O): EnumCodec<I, O> {
10+
return EnumCodec(enumEntries<I>().map { it to block(it) })
11+
}
12+
813
/**
914
* A codec simplifying enum encoding.
1015
*/

0 commit comments

Comments
 (0)