Skip to content

Commit

Permalink
Merge pull request #23 from arrow-kt/all-ensure
Browse files Browse the repository at this point in the history
Name all methods ensure
  • Loading branch information
nomisRev authored Jun 5, 2023
2 parents cc9f2f8 + 916200c commit fd27cc9
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 17 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ import arrow.core.raise.Raise
import arrow.exact.Exact
import arrow.exact.ExactError
import arrow.exact.ensure
import arrow.exact.ensureExact
@JvmInline
value class NotBlankString private constructor(val value: String) {
Expand All @@ -92,7 +91,7 @@ value class NotBlankString private constructor(val value: String) {
value class NotBlankTrimmedString private constructor(val value: String) {
companion object : Exact<String, NotBlankTrimmedString> {
override fun Raise<ExactError>.spec(raw: String): NotBlankTrimmedString {
ensureExact(raw, NotBlankString)
ensure(raw, NotBlankString)
return NotBlankTrimmedString(raw.trim())
}
}
Expand Down
3 changes: 1 addition & 2 deletions guide/src/commonMain/kotlin/examples/example-exact-03.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import arrow.core.raise.Raise
import arrow.exact.Exact
import arrow.exact.ExactError
import arrow.exact.ensure
import arrow.exact.ensureExact

class NotBlankString private constructor(val value: String) {
companion object : Exact<String, NotBlankString> {
Expand All @@ -20,7 +19,7 @@ class NotBlankString private constructor(val value: String) {
value class NotBlankTrimmedString private constructor(val value: String) {
companion object : Exact<String, NotBlankTrimmedString> {
override fun Raise<ExactError>.spec(raw: String): NotBlankTrimmedString {
ensureExact(raw, NotBlankString)
ensure(raw, NotBlankString)
return NotBlankTrimmedString(raw.trim())
}
}
Expand Down
3 changes: 1 addition & 2 deletions guide/src/commonMain/kotlin/examples/example-exact-04.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import arrow.exact.Exact
import arrow.exact.ExactEither
import arrow.exact.ExactError
import arrow.exact.ensure
import arrow.exact.ensureExact

@JvmInline
value class NotBlankTrimmedString private constructor(val value: String) {
Expand All @@ -29,7 +28,7 @@ value class Username private constructor(val value: String) {
companion object : ExactEither<UsernameError, String, Username> {
override fun Raise<UsernameError>.spec(raw: String): Username {
val username =
ensureExact(raw, NotBlankTrimmedString) {
ensure(raw, NotBlankTrimmedString) {
UsernameError.Invalid
}.value
ensure(username.length < 100) { UsernameError.Invalid }
Expand Down
3 changes: 1 addition & 2 deletions guide/src/commonMain/kotlin/examples/example-readme-03.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import arrow.core.raise.Raise
import arrow.exact.Exact
import arrow.exact.ExactError
import arrow.exact.ensure
import arrow.exact.ensureExact

@JvmInline
value class NotBlankString private constructor(val value: String) {
Expand All @@ -21,7 +20,7 @@ value class NotBlankString private constructor(val value: String) {
value class NotBlankTrimmedString private constructor(val value: String) {
companion object : Exact<String, NotBlankTrimmedString> {
override fun Raise<ExactError>.spec(raw: String): NotBlankTrimmedString {
ensureExact(raw, NotBlankString)
ensure(raw, NotBlankString)
return NotBlankTrimmedString(raw.trim())
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/commonMain/kotlin/arrow/exact/Exact.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,13 @@ import arrow.core.raise.ensure
* <!--- KNIT example-exact-02.kt -->
*
* You can define a second type `NotBlankTrimmedString` that is a `NotBlankString` that is also
* trimmed. [ensureExact] allows us to compose [Exact] instances and easily
* trimmed. [ensure] allows us to compose [Exact] instances and easily
* reuse the `NotBlankString` type.
* <!--- INCLUDE
* import arrow.core.raise.Raise
* import arrow.exact.Exact
* import arrow.exact.ExactError
* import arrow.exact.ensure
* import arrow.exact.ensureExact
*
* class NotBlankString private constructor(val value: String) {
* companion object : Exact<String, NotBlankString> {
Expand All @@ -90,7 +89,7 @@ import arrow.core.raise.ensure
* value class NotBlankTrimmedString private constructor(val value: String) {
* companion object : Exact<String, NotBlankTrimmedString> {
* override fun Raise<ExactError>.spec(raw: String): NotBlankTrimmedString {
* ensureExact(raw, NotBlankString)
* ensure(raw, NotBlankString)
* return NotBlankTrimmedString(raw.trim())
* }
* }
Expand All @@ -117,7 +116,6 @@ public data class ExactError(val message: String)
* import arrow.exact.ExactEither
* import arrow.exact.ExactError
* import arrow.exact.ensure
* import arrow.exact.ensureExact
*
* @JvmInline
* value class NotBlankTrimmedString private constructor(val value: String) {
Expand All @@ -140,7 +138,7 @@ public data class ExactError(val message: String)
* companion object : ExactEither<UsernameError, String, Username> {
* override fun Raise<UsernameError>.spec(raw: String): Username {
* val username =
* ensureExact(raw, NotBlankTrimmedString) {
* ensure(raw, NotBlankTrimmedString) {
* UsernameError.Invalid
* }.value
* ensure(username.length < 100) { UsernameError.Invalid }
Expand Down
10 changes: 6 additions & 4 deletions src/commonMain/kotlin/arrow/exact/ExactDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ public inline fun Raise<ExactError>.ensure(condition: Boolean) {
}

@RaiseDSL
public inline fun <A, B> Raise<ExactError>.ensureExact(raw: A, exact: Exact<A, B>): B = ensureExact(raw, exact as ExactEither<*, A, B>)
public inline fun <A, B> Raise<ExactError>.ensure(raw: A, exact: Exact<A, B>): B = ensure(raw, exact as ExactEither<*, A, B>)

@RaiseDSL
public inline fun <A, B> Raise<ExactError>.ensureExact(raw: A, exact: ExactEither<*, A, B>): B = ensureExact(raw, exact) { ExactError("Failed to match Exact.") }
public inline fun <A, B> Raise<ExactError>.ensure(raw: A, exact: ExactEither<*, A, B>): B =
ensure(raw, exact) { ExactError("Failed to match Exact.") }

@RaiseDSL
public inline fun <A, B, Error : Any> Raise<Error>.ensureExact(raw: A, exact: Exact<A, B>, error: (ExactError) -> Error): B = ensureExact(raw, exact as ExactEither<ExactError, A, B>, error)
public inline fun <A, B, Error : Any> Raise<Error>.ensure(raw: A, exact: Exact<A, B>, error: (ExactError) -> Error): B =
ensure(raw, exact as ExactEither<ExactError, A, B>, error)

@RaiseDSL
public inline fun <A, B, Error : Any, E : Any> Raise<E>.ensureExact(raw: A, exact: ExactEither<Error, A, B>, error: (Error) -> E): B {
public inline fun <A, B, Error : Any, E : Any> Raise<E>.ensure(raw: A, exact: ExactEither<Error, A, B>, error: (Error) -> E): B {
return when (val result = exact.from(raw)) {
is Either.Left -> raise(error(result.value))
is Either.Right -> result.value
Expand Down

0 comments on commit fd27cc9

Please sign in to comment.