|
1 | 1 | namespace FsToolkit.ErrorHandling |
2 | 2 |
|
| 3 | +open System |
| 4 | + |
3 | 5 | [<AutoOpen>] |
4 | 6 | module ResultOptionCE = |
5 | 7 |
|
6 | 8 | type ResultOptionBuilder() = |
7 | | - member _.Return value = ResultOption.retn value |
8 | | - member _.ReturnFrom value = value |
| 9 | + member inline _.Return value = ResultOption.retn value |
| 10 | + |
| 11 | + member inline _.ReturnFrom value : Result<'ok option, 'error> = value |
| 12 | + |
| 13 | + member inline _.Zero() = |
| 14 | + option.Zero() |
| 15 | + |> Ok |
| 16 | + |
| 17 | + member inline _.Bind |
| 18 | + ( |
| 19 | + resultOpt: Result<'okInput option, 'error>, |
| 20 | + [<InlineIfLambda>] binder: 'okInput -> Result<'okOutput option, 'error> |
| 21 | + ) : Result<'okOutput option, 'error> = |
| 22 | + ResultOption.bind binder resultOpt |
| 23 | + |
| 24 | + member inline _.Combine |
| 25 | + ( |
| 26 | + resultOpt: Result<unit option, 'error>, |
| 27 | + [<InlineIfLambda>] binder: unit -> Result<'ok option, 'error> |
| 28 | + ) : Result<'ok option, 'error> = |
| 29 | + ResultOption.bind binder resultOpt |
| 30 | + |
| 31 | + member inline _.Delay([<InlineIfLambda>] delayer: unit -> Result<'ok option, 'error>) = |
| 32 | + delayer |
| 33 | + |
| 34 | + member inline _.Run |
| 35 | + ([<InlineIfLambda>] generator: unit -> Result<'ok option, 'error>) |
| 36 | + : Result<'ok option, 'error> = |
| 37 | + generator () |
| 38 | + |
| 39 | + member inline this.TryWith |
| 40 | + ( |
| 41 | + [<InlineIfLambda>] generator: unit -> Result<'T option, 'TError>, |
| 42 | + [<InlineIfLambda>] handler: exn -> Result<'T option, 'TError> |
| 43 | + ) : Result<'T option, 'TError> = |
| 44 | + try |
| 45 | + this.Run generator |
| 46 | + with e -> |
| 47 | + handler e |
| 48 | + |
| 49 | + member inline this.TryFinally |
| 50 | + ( |
| 51 | + [<InlineIfLambda>] generator: unit -> Result<'ok option, 'error>, |
| 52 | + [<InlineIfLambda>] compensation: unit -> unit |
| 53 | + ) : Result<'ok option, 'error> = |
| 54 | + try |
| 55 | + this.Run generator |
| 56 | + finally |
| 57 | + compensation () |
| 58 | + |
| 59 | + member inline this.Using |
| 60 | + ( |
| 61 | + resource: 'disposable :> IDisposable, |
| 62 | + binder: 'disposable -> Result<'ok option, 'error> |
| 63 | + ) : Result<'ok option, 'error> = |
| 64 | + this.TryFinally( |
| 65 | + (fun () -> binder resource), |
| 66 | + (fun () -> |
| 67 | + if not (obj.ReferenceEquals(resource, null)) then |
| 68 | + resource.Dispose() |
| 69 | + ) |
| 70 | + ) |
| 71 | + |
| 72 | + member inline this.While |
| 73 | + ( |
| 74 | + [<InlineIfLambda>] guard: unit -> bool, |
| 75 | + [<InlineIfLambda>] generator: unit -> Result<unit option, 'error> |
| 76 | + ) : Result<unit option, 'error> = |
9 | 77 |
|
10 | | - member _.Bind(resultOpt, binder) = ResultOption.bind binder resultOpt |
| 78 | + let mutable doContinue = true |
| 79 | + let mutable result = Ok(Some()) |
11 | 80 |
|
12 | | - member _.Combine(r1, r2) = |
13 | | - r1 |
14 | | - |> ResultOption.bind (fun _ -> r2) |
| 81 | + while doContinue |
| 82 | + && guard () do |
| 83 | + match generator () with |
| 84 | + | Ok option -> |
| 85 | + match option with |
| 86 | + | Some _ -> () |
| 87 | + | None -> |
| 88 | + doContinue <- false |
| 89 | + result <- Ok None |
| 90 | + | Error e -> |
| 91 | + doContinue <- false |
| 92 | + result <- Error e |
15 | 93 |
|
16 | | - member _.Delay f = f () |
| 94 | + result |
17 | 95 |
|
| 96 | + member inline this.For |
| 97 | + ( |
| 98 | + sequence: #seq<'T>, |
| 99 | + [<InlineIfLambda>] binder: 'T -> Result<unit option, 'TError> |
| 100 | + ) : Result<unit option, 'TError> = |
| 101 | + this.Using( |
| 102 | + sequence.GetEnumerator(), |
| 103 | + fun enum -> |
| 104 | + this.While( |
| 105 | + (fun () -> enum.MoveNext()), |
| 106 | + this.Delay(fun () -> binder enum.Current) |
| 107 | + ) |
| 108 | + ) |
| 109 | + |
| 110 | + member inline _.BindReturn |
| 111 | + ( |
| 112 | + resultOpt: Result<'T option, 'TError>, |
| 113 | + [<InlineIfLambda>] binder: 'T -> 'U |
| 114 | + ) : Result<'U option, 'TError> = |
| 115 | + ResultOption.map binder resultOpt |
| 116 | + |
| 117 | + member inline _.MergeSources |
| 118 | + ( |
| 119 | + left: Result<'left option, 'error>, |
| 120 | + right: Result<'right option, 'error> |
| 121 | + ) : Result<('left * 'right) option, 'error> = |
| 122 | + ResultOption.zip left right |
| 123 | + |
| 124 | + member inline _.Source(result: Result<'ok option, 'error>) : Result<'ok option, 'error> = |
| 125 | + result |
18 | 126 |
|
19 | 127 | let resultOption = ResultOptionBuilder() |
| 128 | + |
| 129 | +[<AutoOpen>] |
| 130 | +module ResultOptionCEExtensions = |
| 131 | + |
| 132 | + type ResultOptionBuilder with |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Needed to allow `for..in` and `for..do` functionality |
| 136 | + /// </summary> |
| 137 | + member inline _.Source(s: #seq<'value>) : #seq<'value> = s |
| 138 | + |
| 139 | + member inline _.Source(result: Result<'ok, 'error>) : Result<'ok option, 'error> = |
| 140 | + ResultOption.ofResult result |
| 141 | + |
| 142 | + member inline _.Source(option: 'T option) : Result<'T option, 'error> = |
| 143 | + ResultOption.ofOption option |
| 144 | + |
| 145 | + member inline _.Source(choice: Choice<'T, 'Error>) : Result<'T option, 'Error> = |
| 146 | + ResultOption.ofChoice choice |
0 commit comments