Skip to content

Commit

Permalink
Replace coerceIn with check
Browse files Browse the repository at this point in the history
  • Loading branch information
zTrap committed Oct 24, 2023
1 parent 4e0c1c7 commit 0005947
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
10 changes: 7 additions & 3 deletions common/src/commonMain/kotlin/entity/Snowflake.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@ import kotlin.time.TimeMark
*
* Snowflakes are IDs with a [timestamp], which makes them [comparable][compareTo] based on their timestamp.
*
* @constructor Values are [coerced in][coerceIn] [validValues].
* @param value The raw value of this Snowflake as specified by the
* [Discord Developer Documentation](https://discord.com/developers/docs/reference#snowflakes).
* @throws IllegalStateException if provided value isn't in [validValues]
*/
@JvmInline
@Serializable(with = Snowflake.Serializer::class)
public value class Snowflake(public val value: ULong) : Comparable<Snowflake> {

init {
value.coerceIn(validValues)
check(value in validValues) {
"Value must be in $validValues, but gave $value"
}
}

/**
* Creates a Snowflake from a given String [value], parsing it as a [ULong] value.
*
* Values are [coerced in][coerceIn] [validValues].
* @throws IllegalStateException if provided value isn't in [validValues]
*/
public constructor(value: String) : this(value.toULong())

Expand Down
5 changes: 2 additions & 3 deletions common/src/commonTest/kotlin/entity/SnowflakeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ class SnowflakeTest {

@Test
@JsName("test4")
fun `Snowflake created from ULong MAX_VALUE has timestamp equal to endOfTime`() {
val snowflake = Snowflake(ULong.MAX_VALUE)
assertEquals(Snowflake.endOfTime, snowflake.timestamp)
fun `Snowflake throws if invalid value is provided`() {
assertFailsWith<IllegalStateException> { Snowflake(ULong.MAX_VALUE) }
}

@Test
Expand Down

0 comments on commit 0005947

Please sign in to comment.