diff options
| author | David Göransson <david.goransson@mullvad.net> | 2024-06-04 08:27:30 +0200 |
|---|---|---|
| committer | David Göransson <david.goransson@mullvad.net> | 2024-06-04 08:27:30 +0200 |
| commit | 45c369fe355a5eadc29dd3e6f492ff6d7430ee5d (patch) | |
| tree | 70925bd8ab3348533ca7217606ac4c2d42356453 /android/lib/model/src | |
| parent | 3ac0f264402e42a16f0bd6c76e1a6a67e01ed4ef (diff) | |
| parent | 571c620b51cdc6b4338a3244b500f70e79b3ca7d (diff) | |
| download | mullvadvpn-45c369fe355a5eadc29dd3e6f492ff6d7430ee5d.tar.xz mullvadvpn-45c369fe355a5eadc29dd3e6f492ff6d7430ee5d.zip | |
Merge branch 'add-setting-for-udp2tcp-port-droid-249'
Diffstat (limited to 'android/lib/model/src')
| -rw-r--r-- | android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/lib/model/Port.kt | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/lib/model/Port.kt b/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/lib/model/Port.kt index bcb5a8dd99..5ce44d0565 100644 --- a/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/lib/model/Port.kt +++ b/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/lib/model/Port.kt @@ -1,6 +1,28 @@ package net.mullvad.mullvadvpn.lib.model import android.os.Parcelable +import arrow.core.Either +import arrow.core.raise.either +import arrow.core.raise.ensure import kotlinx.parcelize.Parcelize -@JvmInline @Parcelize value class Port(val value: Int) : Parcelable +@JvmInline +@Parcelize +value class Port(val value: Int) : Parcelable { + companion object { + fun fromString(value: String): Either<ParsePortError, Port> = either { + val number = value.toIntOrNull() ?: raise(ParsePortError.NotANumber(value)) + ensure(number in MIN_VALUE..MAX_VALUE) { ParsePortError.OutOfRange(number) } + Port(number) + } + + private const val MIN_VALUE = 0 + private const val MAX_VALUE = 65535 + } +} + +sealed interface ParsePortError { + data class NotANumber(val input: String) : ParsePortError + + data class OutOfRange(val value: Int) : ParsePortError +} |
