diff --git a/gitbook/asyncResult/others.md b/gitbook/asyncResult/others.md index 9e61c9bb..8f4bdf2e 100644 --- a/gitbook/asyncResult/others.md +++ b/gitbook/asyncResult/others.md @@ -20,6 +20,12 @@ Converts an async-wrapped Option to a Result, using the given error if None. ```fsharp 'a -> Async<'b option> -> Async>` ``` +### requireSomeWith + +Converts an async-wrapped Option to a Result, using the given error factory if None. The error factory is only called when the value is `None`. +```fsharp +(unit -> 'a) -> Async<'b option> -> Async> +``` ### requireNone Converts an async-wrapped Option to a Result, using the given error if Some. @@ -28,6 +34,14 @@ Converts an async-wrapped Option to a Result, using the given error if Some. 'a -> Async<'b option> -> Async>` ``` +### requireNoneWith + +Converts an async-wrapped Option to a Result, using the given error factory if Some. The error factory is only called when the value is `Some`. + +```fsharp +(unit -> 'a) -> Async<'b option> -> Async> +``` + ### requireValueSome Converts an async-wrapped ValueOption to a Result, using the given error if ValueNone. diff --git a/gitbook/jobResult/others.md b/gitbook/jobResult/others.md index 6caac10f..3e342f6b 100644 --- a/gitbook/jobResult/others.md +++ b/gitbook/jobResult/others.md @@ -20,6 +20,12 @@ Converts an job-wrapped Option to a Result, using the given error if None. ```fsharp 'a -> job<'b option> -> job>` ``` +### requireSomeWith + +Converts an job-wrapped Option to a Result, using the given error factory if None. The error factory is only called when the value is `None`. +```fsharp +(unit -> 'a) -> job<'b option> -> job> +``` ### requireNone Converts an job-wrapped Option to a Result, using the given error if Some. @@ -28,6 +34,14 @@ Converts an job-wrapped Option to a Result, using the given error if Some. 'a -> job<'b option> -> job>` ``` +### requireNoneWith + +Converts an job-wrapped Option to a Result, using the given error factory if Some. The error factory is only called when the value is `Some`. + +```fsharp +(unit -> 'a) -> job<'b option> -> job> +``` + ### requireValueSome Converts an job-wrapped ValueOption to a Result, using the given error if ValueNone. diff --git a/gitbook/result/requireFunctions.md b/gitbook/result/requireFunctions.md index 31b152dd..bc3c41dd 100644 --- a/gitbook/result/requireFunctions.md +++ b/gitbook/result/requireFunctions.md @@ -96,6 +96,38 @@ let result : Result = // Error "Value must be Some" ``` +## requireSomeWith + +Converts an Option to a Result, using the given error factory if None. The error factory is only called when the value is `None`. + +### Function Signature + +```fsharp +(unit -> 'a) -> 'b option -> Result<'b, 'a> +``` + +### Examples + +#### Example 1 + +```fsharp +let result : Result = + Some 1 + |> Result.requireSomeWith (fun () -> "Value must be Some") + +// Ok 1 +``` + +#### Example 2 + +```fsharp +let result : Result = + None + |> Result.requireSomeWith (fun () -> "Value must be Some") + +// Error "Value must be Some" +``` + ## requireNone Converts an Option to a Result, using the given error if Some. @@ -128,6 +160,38 @@ let result : Result = // Error "Value must be None" ``` +## requireNoneWith + +Converts an Option to a Result, using the given error factory if Some. The error factory is only called when the value is `Some`. + +### Function Signature + +```fsharp +(unit -> 'a) -> 'b option -> Result +``` + +### Examples + +#### Example 1 + +```fsharp +let result : Result = + None + |> Result.requireNoneWith (fun () -> "Value must be None") + +// Ok () +``` + +#### Example 2 + +```fsharp +let result : Result = + Some 1 + |> Result.requireNoneWith (fun () -> "Value must be None") + +// Error "Value must be None" +``` + ## requireValueSome Converts an ValueOption to a Result, using the given error if ValueNone. diff --git a/gitbook/taskResult/others.md b/gitbook/taskResult/others.md index 24d07811..d891dfe7 100644 --- a/gitbook/taskResult/others.md +++ b/gitbook/taskResult/others.md @@ -20,6 +20,12 @@ Converts an task-wrapped Option to a Result, using the given error if None. ```fsharp 'a -> Task<'b option> -> Task>` ``` +### requireSomeWith + +Converts an task-wrapped Option to a Result, using the given error factory if None. The error factory is only called when the value is `None`. +```fsharp +(unit -> 'a) -> Task<'b option> -> Task> +``` ### requireNone Converts an task-wrapped Option to a Result, using the given error if Some. @@ -28,6 +34,14 @@ Converts an task-wrapped Option to a Result, using the given error if Some. 'a -> Task<'b option> -> Task>` ``` +### requireNoneWith + +Converts an task-wrapped Option to a Result, using the given error factory if Some. The error factory is only called when the value is `Some`. + +```fsharp +(unit -> 'a) -> Task<'b option> -> Task> +``` + ### requireValueSome Converts an task-wrapped ValueOption to a Result, using the given error if ValueNone. diff --git a/src/FsToolkit.ErrorHandling.JobResult/JobResult.fs b/src/FsToolkit.ErrorHandling.JobResult/JobResult.fs index dfbc2937..de5565d6 100644 --- a/src/FsToolkit.ErrorHandling.JobResult/JobResult.fs +++ b/src/FsToolkit.ErrorHandling.JobResult/JobResult.fs @@ -122,11 +122,21 @@ module JobResult = option |> Job.map (Result.requireSome error) + // Converts an job-wrapped Option to a Result, using the given error factory if None. + let inline requireSomeWith ([] errorFactory: unit -> 'error) option = + option + |> Job.map (Result.requireSomeWith errorFactory) + // Converts an job-wrapped Option to a Result, using the given error if Some. let inline requireNone error option = option |> Job.map (Result.requireNone error) + // Converts an job-wrapped Option to a Result, using the given error factory if Some. + let inline requireNoneWith ([] errorFactory: unit -> 'error) option = + option + |> Job.map (Result.requireNoneWith errorFactory) + // Converts an job-wrapped ValueOption to a Result, using the given error if ValueNone. let inline requireValueSome error voption = voption diff --git a/src/FsToolkit.ErrorHandling/AsyncResult.fs b/src/FsToolkit.ErrorHandling/AsyncResult.fs index b4633422..74b64bbf 100644 --- a/src/FsToolkit.ErrorHandling/AsyncResult.fs +++ b/src/FsToolkit.ErrorHandling/AsyncResult.fs @@ -158,6 +158,14 @@ module AsyncResult = value |> Async.map (Result.requireSome error) + // Converts an async-wrapped Option to a Result, using the given error factory if None. + let inline requireSomeWith + ([] errorFactory: unit -> 'error) + (value: Async<'ok option>) + : Async> = + value + |> Async.map (Result.requireSomeWith errorFactory) + // Converts an async-wrapped Option to a Result, using the given error if Some. let inline requireNone (error: 'error) @@ -166,6 +174,14 @@ module AsyncResult = value |> Async.map (Result.requireNone error) + // Converts an async-wrapped Option to a Result, using the given error factory if Some. + let inline requireNoneWith + ([] errorFactory: unit -> 'error) + (value: Async<'ok option>) + : Async> = + value + |> Async.map (Result.requireNoneWith errorFactory) + // Converts an async-wrapped ValueOption to a Result, using the given error if ValueNone. let inline requireValueSome (error: 'error) diff --git a/src/FsToolkit.ErrorHandling/Result.fs b/src/FsToolkit.ErrorHandling/Result.fs index fc2f6261..1d416338 100644 --- a/src/FsToolkit.ErrorHandling/Result.fs +++ b/src/FsToolkit.ErrorHandling/Result.fs @@ -307,6 +307,20 @@ module Result = | Some x -> Ok x | None -> Error error + /// + /// Requires a value to be Some, otherwise returns an error result using the given error factory. + /// + /// A function to produce the error value if the value is None. + /// The Option value to check. + /// An Ok result if the value is Some, otherwise an Error result with the value produced by . + let inline requireSomeWith + ([] errorFactory: unit -> 'error) + (option: 'ok option) + : Result<'ok, 'error> = + match option with + | Some x -> Ok x + | None -> Error(errorFactory ()) + /// /// Requires a value to be None, otherwise returns an error result. /// @@ -320,6 +334,20 @@ module Result = | Some _ -> Error error | None -> Ok() + /// + /// Requires a value to be None, otherwise returns an error result using the given error factory. + /// + /// A function to produce the error value if the value is Some. + /// The Option value to check. + /// An Ok result if the value is None, otherwise an Error result with the value produced by . + let inline requireNoneWith + ([] errorFactory: unit -> 'error) + (option: 'value option) + : Result = + match option with + | Some _ -> Error(errorFactory ()) + | None -> Ok() + /// /// Requires a value to be ValueSome, otherwise returns an error result. /// diff --git a/src/FsToolkit.ErrorHandling/TaskResult.fs b/src/FsToolkit.ErrorHandling/TaskResult.fs index 484f64d0..a1dfab15 100644 --- a/src/FsToolkit.ErrorHandling/TaskResult.fs +++ b/src/FsToolkit.ErrorHandling/TaskResult.fs @@ -107,11 +107,21 @@ module TaskResult = option |> Task.map (Result.requireSome error) + // Converts an task-wrapped Option to a Result, using the given error factory if None. + let inline requireSomeWith ([] errorFactory: unit -> 'error) option = + option + |> Task.map (Result.requireSomeWith errorFactory) + // Converts an task-wrapped Option to a Result, using the given error if Some. let inline requireNone error option = option |> Task.map (Result.requireNone error) + // Converts an task-wrapped Option to a Result, using the given error factory if Some. + let inline requireNoneWith ([] errorFactory: unit -> 'error) option = + option + |> Task.map (Result.requireNoneWith errorFactory) + // Converts an task-wrapped ValueOption to a Result, using the given error if ValueNone. let inline requireValueSome error voption = voption diff --git a/tests/FsToolkit.ErrorHandling.JobResult.Tests/JobResult.fs b/tests/FsToolkit.ErrorHandling.JobResult.Tests/JobResult.fs index 38ded2d7..bb87261e 100644 --- a/tests/FsToolkit.ErrorHandling.JobResult.Tests/JobResult.fs +++ b/tests/FsToolkit.ErrorHandling.JobResult.Tests/JobResult.fs @@ -284,6 +284,22 @@ let requireSomeTests = |> Expect.hasJobErrorValueSync err ] +[] +let requireSomeWithTests = + testList "JobResult.requireSomeWith Tests" [ + testCase "requireSomeWith happy path" + <| fun _ -> + toJob (Some 42) + |> JobResult.requireSomeWith (fun () -> err) + |> Expect.hasJobOkValueSync 42 + + testCase "requireSomeWith error path" + <| fun _ -> + toJob None + |> JobResult.requireSomeWith (fun () -> err) + |> Expect.hasJobErrorValueSync err + ] + [] let requireNoneTests = testList "JobResult.requireNone Tests" [ @@ -300,6 +316,22 @@ let requireNoneTests = |> Expect.hasJobErrorValueSync err ] +[] +let requireNoneWithTests = + testList "JobResult.requireNoneWith Tests" [ + testCase "requireNoneWith happy path" + <| fun _ -> + toJob None + |> JobResult.requireNoneWith (fun () -> err) + |> Expect.hasJobOkValueSync () + + testCase "requireNoneWith error path" + <| fun _ -> + toJob (Some 42) + |> JobResult.requireNoneWith (fun () -> err) + |> Expect.hasJobErrorValueSync err + ] + [] let requireValueSomeTests = testList "JobResult.requireValueSome Tests" [ diff --git a/tests/FsToolkit.ErrorHandling.Tests/AsyncResult.fs b/tests/FsToolkit.ErrorHandling.Tests/AsyncResult.fs index 77cec8d4..79ae9aea 100644 --- a/tests/FsToolkit.ErrorHandling.Tests/AsyncResult.fs +++ b/tests/FsToolkit.ErrorHandling.Tests/AsyncResult.fs @@ -296,6 +296,20 @@ let requireSomeTests = ] +let requireSomeWithTests = + testList "AsyncResult.requireSomeWith Tests" [ + testCaseAsync "requireSomeWith happy path" + <| (toAsync (Some 42) + |> AsyncResult.requireSomeWith (fun () -> err) + |> Expect.hasAsyncOkValue 42) + + testCaseAsync "requireSomeWith error path" + <| (toAsync None + |> AsyncResult.requireSomeWith (fun () -> err) + |> Expect.hasAsyncErrorValue err) + ] + + let requireNoneTests = testList "AsyncResult.requireNone Tests" [ testCaseAsync "requireNone happy path" @@ -310,6 +324,20 @@ let requireNoneTests = ] +let requireNoneWithTests = + testList "AsyncResult.requireNoneWith Tests" [ + testCaseAsync "requireNoneWith happy path" + <| (toAsync None + |> AsyncResult.requireNoneWith (fun () -> err) + |> Expect.hasAsyncOkValue ()) + + testCaseAsync "requireNoneWith error path" + <| (toAsync (Some 42) + |> AsyncResult.requireNoneWith (fun () -> err) + |> Expect.hasAsyncErrorValue err) + ] + + let requireValueSomeTests = testList "AsyncResult.requireValueSome Tests" [ testCaseAsync "requireValueSome happy path" @@ -1034,7 +1062,9 @@ let allTests = requireTrueTests requireFalseTests requireSomeTests + requireSomeWithTests requireNoneTests + requireNoneWithTests requireValueSomeTests requireValueNoneTests requireEqualToTests diff --git a/tests/FsToolkit.ErrorHandling.Tests/Result.fs b/tests/FsToolkit.ErrorHandling.Tests/Result.fs index 4bd124d5..404208d6 100644 --- a/tests/FsToolkit.ErrorHandling.Tests/Result.fs +++ b/tests/FsToolkit.ErrorHandling.Tests/Result.fs @@ -329,6 +329,19 @@ let requireSomeTests = |> Expect.hasErrorValue err ] +let requireSomeWithTests = + testList "requireSomeWith Tests" [ + testCase "requireSomeWith happy path" + <| fun _ -> + Result.requireSomeWith (fun () -> err) (Some 42) + |> Expect.hasOkValue 42 + + testCase "requireSomeWith error path" + <| fun _ -> + Result.requireSomeWith (fun () -> err) None + |> Expect.hasErrorValue err + ] + let requireNotNullTests = testList "requireNotNull Tests" [ testCase "requireNotNull happy path" @@ -355,6 +368,19 @@ let requireNoneTests = |> Expect.hasErrorValue err ] +let requireNoneWithTests = + testList "requireNoneWith Tests" [ + testCase "requireNoneWith happy path" + <| fun _ -> + Result.requireNoneWith (fun () -> err) None + |> Expect.hasOkValue () + + testCase "requireNoneWith error path" + <| fun _ -> + Result.requireNoneWith (fun () -> err) (Some 42) + |> Expect.hasErrorValue err + ] + let requireValueSomeTests = testList "requireValueSome Tests" [ testCase "requireValueSome happy path" @@ -942,7 +968,9 @@ let allTests = requireTrueTests requireFalseTests requireSomeTests + requireSomeWithTests requireNoneTests + requireNoneWithTests requireValueSomeTests requireValueNoneTests requireNotNullTests diff --git a/tests/FsToolkit.ErrorHandling.Tests/TaskResult.fs b/tests/FsToolkit.ErrorHandling.Tests/TaskResult.fs index 25df635e..f8491755 100644 --- a/tests/FsToolkit.ErrorHandling.Tests/TaskResult.fs +++ b/tests/FsToolkit.ErrorHandling.Tests/TaskResult.fs @@ -278,6 +278,21 @@ let requireSomeTests = |> Expect.hasTaskErrorValueSync err ] +let requireSomeWithTests = + testList "TaskResult.requireSomeWith Tests" [ + testCase "requireSomeWith happy path" + <| fun _ -> + toTask (Some 42) + |> TaskResult.requireSomeWith (fun () -> err) + |> Expect.hasTaskOkValueSync 42 + + testCase "requireSomeWith error path" + <| fun _ -> + toTask None + |> TaskResult.requireSomeWith (fun () -> err) + |> Expect.hasTaskErrorValueSync err + ] + let requireNoneTests = testList "TaskResult.requireNone Tests" [ testCase "requireNone happy path" @@ -293,6 +308,21 @@ let requireNoneTests = |> Expect.hasTaskErrorValueSync err ] +let requireNoneWithTests = + testList "TaskResult.requireNoneWith Tests" [ + testCase "requireNoneWith happy path" + <| fun _ -> + toTask None + |> TaskResult.requireNoneWith (fun () -> err) + |> Expect.hasTaskOkValueSync () + + testCase "requireNoneWith error path" + <| fun _ -> + toTask (Some 42) + |> TaskResult.requireNoneWith (fun () -> err) + |> Expect.hasTaskErrorValueSync err + ] + let requireValueSomeTests = testList "TaskResult.requireValueSome Tests" [ testCase "requireValueSome happy path" @@ -1025,7 +1055,9 @@ let allTests = requireTrueTests requireFalseTests requireSomeTests + requireSomeWithTests requireNoneTests + requireNoneWithTests requireValueSomeTests requireValueNoneTests requireEqualToTests