Skip to content

Commit c85555f

Browse files
sheridanchrisTheAngryByrd
authored andcommitted
Add TaskResult bindRequireSome and bindRequireNone
1 parent 8aa342f commit c85555f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/FsToolkit.ErrorHandling.TaskResult/TaskResult.fs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,13 @@ module TaskResult =
237237
let inline ofResult (x: Result<_, _>) =
238238
x
239239
|> Task.singleton
240+
241+
/// Bind the TaskResult and requireSome on the inner option value.
242+
let inline bindRequireSome error x =
243+
x
244+
|> bind (Result.requireSome error >> Task.singleton)
245+
246+
/// Bind the TaskResult and requireNone on the inner option value.
247+
let inline bindRequireNone error x =
248+
x
249+
|> bind (Result.requireNone error >> Task.singleton)

tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskResult.fs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,19 @@ let TaskResultOperatorTests =
823823
)
824824
|> Expect.hasTaskOkValueSync (PostId newPostId)
825825
]
826+
827+
[<Tests>]
828+
let TaskResultBindRequireTests =
829+
testList "TeskResult Bind + Require Tests" [
830+
testCase "bindRequireNone"
831+
<| fun _ ->
832+
let user = Some "john_doe" |> Ok |> Task.singleton
833+
let error = user |> TaskResult.bindRequireNone "User exists"
834+
error |> Expect.hasTaskErrorValueSync "User exists"
835+
836+
testCase "bindRequireSome"
837+
<| fun _ ->
838+
let user = Some "john_doe" |> Ok |> Task.singleton
839+
let error = user |> TaskResult.bindRequireSome "User doesn't exist"
840+
error |> Expect.hasTaskOkValueSync "john_doe"
841+
]

0 commit comments

Comments
 (0)