@glutinum/cli 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -0
- package/dist/Glutinum.Converter/FsharpAST.js +76 -21
- package/dist/Glutinum.Converter/FsharpAST.js.map +1 -1
- package/dist/Glutinum.Converter/Generate.js +5 -7
- package/dist/Glutinum.Converter/Generate.js.map +1 -1
- package/dist/Glutinum.Converter/GlueAST.js +90 -14
- package/dist/Glutinum.Converter/GlueAST.js.map +1 -1
- package/dist/Glutinum.Converter/Log.js +5 -5
- package/dist/Glutinum.Converter/Log.js.map +1 -1
- package/dist/Glutinum.Converter/Merge.js +7 -7
- package/dist/Glutinum.Converter/Merge.js.map +1 -1
- package/dist/Glutinum.Converter/Printer.js +203 -106
- package/dist/Glutinum.Converter/Printer.js.map +1 -1
- package/dist/Glutinum.Converter/Read.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/ClassDeclaration.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/Declaration.js +20 -19
- package/dist/Glutinum.Converter/Reader/Declaration.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/Documentation.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/EnumDeclaration.js +4 -3
- package/dist/Glutinum.Converter/Reader/EnumDeclaration.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/ExportAssignment.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/FunctionDeclaration.js +2 -2
- package/dist/Glutinum.Converter/Reader/FunctionDeclaration.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/IndexedAccessType.js +5 -6
- package/dist/Glutinum.Converter/Reader/IndexedAccessType.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/InterfaceDeclaration.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/MappedTypeNode.js +42 -0
- package/dist/Glutinum.Converter/Reader/MappedTypeNode.js.map +1 -0
- package/dist/Glutinum.Converter/Reader/ModuleDeclaration.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/NamedTupleMember.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/Node.js +8 -5
- package/dist/Glutinum.Converter/Reader/Node.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/Parameters.js +7 -4
- package/dist/Glutinum.Converter/Reader/Parameters.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/Report.js +35 -0
- package/dist/Glutinum.Converter/Reader/Report.js.map +1 -0
- package/dist/Glutinum.Converter/Reader/TypeAliasDeclaration.js +5 -0
- package/dist/Glutinum.Converter/Reader/TypeAliasDeclaration.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/TypeNode.js +250 -69
- package/dist/Glutinum.Converter/Reader/TypeNode.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/TypeOperatorNode.js +31 -26
- package/dist/Glutinum.Converter/Reader/TypeOperatorNode.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/TypeParameters.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/TypeQueryNode.js +127 -0
- package/dist/Glutinum.Converter/Reader/TypeQueryNode.js.map +1 -0
- package/dist/Glutinum.Converter/Reader/TypeScriptReader.js +7 -2
- package/dist/Glutinum.Converter/Reader/TypeScriptReader.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/UnionTypeNode.js +6 -9
- package/dist/Glutinum.Converter/Reader/UnionTypeNode.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/Utils.js +35 -13
- package/dist/Glutinum.Converter/Reader/Utils.js.map +1 -1
- package/dist/Glutinum.Converter/Reader/VariableStatement.js +2 -2
- package/dist/Glutinum.Converter/Reader/VariableStatement.js.map +1 -1
- package/dist/Glutinum.Converter/Transform.js +614 -211
- package/dist/Glutinum.Converter/Transform.js.map +1 -1
- package/dist/Glutinum.Converter/TypeScript.Extensions.js +745 -0
- package/dist/Glutinum.Converter/TypeScript.Extensions.js.map +1 -0
- package/dist/Glutinum.Converter/Utils/Naming.js +6 -2
- package/dist/Glutinum.Converter/Utils/Naming.js.map +1 -1
- package/dist/Program.js +2 -2
- package/dist/Program.js.map +1 -1
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AssemblyInfo.fs +19 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/Async.fs +207 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/Async.fs.js +127 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/Async.fs.js.map +1 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncOption.fs +81 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncOptionCE.fs +183 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncOptionCE.fs.js +37 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncOptionCE.fs.js.map +1 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncOptionOp.fs +24 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncResult.fs +439 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncResultCE.fs +217 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncResultCE.fs.js +18 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncResultCE.fs.js.map +1 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncResultOp.fs +24 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncResultOption.fs +75 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncResultOption.fs.js +26 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncResultOption.fs.js.map +1 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncResultOptionCE.fs +238 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncResultOptionCE.fs.js +18 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncResultOptionCE.fs.js.map +1 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncResultOptionOp.fs +24 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncValidation.fs +190 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncValidationCE.fs +194 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncValidationCE.fs.js +18 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncValidationCE.fs.js.map +1 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/AsyncValidationOp.fs +35 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/FsToolkit.ErrorHandling.fableproj +51 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/List.fs +218 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/List.fs.js +358 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/List.fs.js.map +1 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/Option.fs +331 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/OptionCE.fs +186 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/OptionCE.fs.js +18 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/OptionCE.fs.js.map +1 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/OptionOp.fs +21 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/Result.fs +644 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/Result.fs.js +45 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/Result.fs.js.map +1 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/ResultCE.fs +155 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/ResultCE.fs.js +23 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/ResultCE.fs.js.map +1 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/ResultOp.fs +45 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/ResultOption.fs +201 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/ResultOption.fs.js +65 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/ResultOption.fs.js.map +1 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/ResultOptionCE.fs +146 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/ResultOptionCE.fs.js +18 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/ResultOptionCE.fs.js.map +1 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/ResultOptionOp.fs +57 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/Seq.fs +19 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/Seq.fs.js +31 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/Seq.fs.js.map +1 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/Shims.fs +22 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/Shims.fs.js +18 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/Shims.fs.js.map +1 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/Validation.fs +172 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/ValidationCE.fs +148 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/ValidationCE.fs.js +18 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/ValidationCE.fs.js.map +1 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/ValidationOp.fs +35 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/ValueOption.fs +151 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/ValueOptionCE.fs +165 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/obj/Debug/netstandard2.1/.NETStandard,Version=v2.1.AssemblyAttributes.fs +3 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/obj/Debug/netstandard2.1/FsToolkit.ErrorHandling.AssemblyInfo.fs +20 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/obj/Release/netstandard2.0/.NETStandard,Version=v2.0.AssemblyAttributes.fs +3 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/obj/Release/netstandard2.0/FsToolkit.ErrorHandling.AssemblyInfo.fs +20 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/obj/Release/netstandard2.1/.NETStandard,Version=v2.1.AssemblyAttributes.fs +3 -0
- package/dist/fable_modules/FsToolkit.ErrorHandling.4.16.0/obj/Release/netstandard2.1/FsToolkit.ErrorHandling.AssemblyInfo.fs +20 -0
- package/dist/fable_modules/project_cracked.json +1 -1
- package/package.json +3 -2
- package/src/Glutinum.Converter/js/bootstrap.js +16 -0
|
@@ -0,0 +1,644 @@
|
|
|
1
|
+
namespace FsToolkit.ErrorHandling
|
|
2
|
+
|
|
3
|
+
/// <summary>
|
|
4
|
+
/// Helper functions for working with <c>Result</c> values.
|
|
5
|
+
/// </summary>
|
|
6
|
+
[<RequireQualifiedAccess>]
|
|
7
|
+
module Result =
|
|
8
|
+
|
|
9
|
+
/// <summary>
|
|
10
|
+
/// Applies a transformation to the value of a <c>Result</c> to a new value using the specified mapper function.
|
|
11
|
+
///
|
|
12
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/map</href>
|
|
13
|
+
/// </summary>
|
|
14
|
+
/// <param name="mapper">The function to apply to the value of the <c>Result</c> if it is <c>Ok</c>.</param>
|
|
15
|
+
/// <param name="input">The <c>Result</c> to map.</param>
|
|
16
|
+
/// <returns>A new <c>Result</c>with the mapped value if the input <c>Result</c> is <c>Ok</c>, otherwise the original <c>Error</c>.</returns>
|
|
17
|
+
let inline map
|
|
18
|
+
([<InlineIfLambda>] mapper: 'okInput -> 'okOutput)
|
|
19
|
+
(input: Result<'okInput, 'error>)
|
|
20
|
+
: Result<'okOutput, 'error> =
|
|
21
|
+
match input with
|
|
22
|
+
| Ok x -> Ok(mapper x)
|
|
23
|
+
| Error e -> Error e
|
|
24
|
+
|
|
25
|
+
/// <summary>
|
|
26
|
+
/// Maps the error value of a <c>Result</c>to a new error value using the specified error mapper function.
|
|
27
|
+
///
|
|
28
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/maperror</href>
|
|
29
|
+
/// </summary>
|
|
30
|
+
/// <param name="errorMapper">The function that maps the input error value to the output error value.</param>
|
|
31
|
+
/// <param name="input">The <c>Result</c>value to map the error value of.</param>
|
|
32
|
+
/// <returns>A new <c>Result</c>with the same Ok value and the mapped error value.</returns>
|
|
33
|
+
let inline mapError
|
|
34
|
+
([<InlineIfLambda>] errorMapper: 'errorInput -> 'errorOutput)
|
|
35
|
+
(input: Result<'ok, 'errorInput>)
|
|
36
|
+
: Result<'ok, 'errorOutput> =
|
|
37
|
+
match input with
|
|
38
|
+
| Ok x -> Ok x
|
|
39
|
+
| Error e -> Error(errorMapper e)
|
|
40
|
+
|
|
41
|
+
/// <summary>
|
|
42
|
+
/// Takes a transformation function and applies it to the <c>Result</c> if it is <c>Ok</c>.
|
|
43
|
+
///
|
|
44
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/bind</href>
|
|
45
|
+
/// </summary>
|
|
46
|
+
/// <param name="binder">The transformation function</param>
|
|
47
|
+
/// <param name="input">The input result</param>
|
|
48
|
+
/// <typeparam name="'okInput">The type of the successful result.</typeparam>
|
|
49
|
+
/// <typeparam name="'okOutput">The type of the result after binding.</typeparam>
|
|
50
|
+
/// <typeparam name="'error">The type of the error.</typeparam>
|
|
51
|
+
/// <returns>Returns a new <c>Result</c> if the input is <c>Ok</c>, otherwise returns the original <c>Result</c></returns>
|
|
52
|
+
let inline bind
|
|
53
|
+
([<InlineIfLambda>] binder: 'okInput -> Result<'okOutput, 'error>)
|
|
54
|
+
(input: Result<'okInput, 'error>)
|
|
55
|
+
: Result<'okOutput, 'error> =
|
|
56
|
+
match input with
|
|
57
|
+
| Ok x -> binder x
|
|
58
|
+
| Error e -> Error e
|
|
59
|
+
|
|
60
|
+
/// <summary>
|
|
61
|
+
/// Determines whether the specified <c>Result</c> is in a successful state.
|
|
62
|
+
///
|
|
63
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/others#isok</href>
|
|
64
|
+
/// </summary>
|
|
65
|
+
/// <param name="value">The result to check.</param>
|
|
66
|
+
/// <returns>True if the result is in a successful state; otherwise, false.</returns>
|
|
67
|
+
let inline isOk (value: Result<'ok, 'error>) : bool =
|
|
68
|
+
match value with
|
|
69
|
+
| Ok _ -> true
|
|
70
|
+
| Error _ -> false
|
|
71
|
+
|
|
72
|
+
/// <summary>
|
|
73
|
+
/// Determines whether the specified <c>Result</c> is an error.
|
|
74
|
+
///
|
|
75
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/others#iserror</href>
|
|
76
|
+
/// </summary>
|
|
77
|
+
/// <param name="value">The result to check.</param>
|
|
78
|
+
/// <returns>True if the result is an error; otherwise, false.</returns>
|
|
79
|
+
let inline isError (value: Result<'ok, 'error>) : bool =
|
|
80
|
+
match value with
|
|
81
|
+
| Ok _ -> false
|
|
82
|
+
| Error _ -> true
|
|
83
|
+
|
|
84
|
+
/// <summary>
|
|
85
|
+
/// Applies the appropriate function based on the result of the input.
|
|
86
|
+
///
|
|
87
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/eitherfunctions#result.either</href>
|
|
88
|
+
/// </summary>
|
|
89
|
+
/// <param name="onOk">The function to apply if the input is <c>Ok</c></param>
|
|
90
|
+
/// <param name="onError">The function to apply if the input is <c>Error</c></param>
|
|
91
|
+
/// <param name="input">The input result</param>
|
|
92
|
+
/// <returns>The result of applying the appropriate function.</returns>
|
|
93
|
+
let inline either
|
|
94
|
+
([<InlineIfLambda>] onOk: 'okInput -> 'output)
|
|
95
|
+
([<InlineIfLambda>] onError: 'errorInput -> 'output)
|
|
96
|
+
(input: Result<'okInput, 'errorInput>)
|
|
97
|
+
: 'output =
|
|
98
|
+
match input with
|
|
99
|
+
| Ok x -> onOk x
|
|
100
|
+
| Error err -> onError err
|
|
101
|
+
|
|
102
|
+
/// <summary>
|
|
103
|
+
/// Maps the values of a Result to a new Result using the provided functions.
|
|
104
|
+
///
|
|
105
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/eitherfunctions#result.eithermap</href>
|
|
106
|
+
/// </summary>
|
|
107
|
+
/// <param name="onOk">The function to apply to the 'ok' value of the input Result.</param>
|
|
108
|
+
/// <param name="onError">The function to apply to the 'error' value of the input Result.</param>
|
|
109
|
+
/// <param name="input">The input <c>Result</c>to map.</param>
|
|
110
|
+
/// <returns>A new Result with the mapped values.</returns>
|
|
111
|
+
let inline eitherMap
|
|
112
|
+
([<InlineIfLambda>] onOk: 'okInput -> 'okOutput)
|
|
113
|
+
([<InlineIfLambda>] onError: 'errorInput -> 'errorOutput)
|
|
114
|
+
(input: Result<'okInput, 'errorInput>)
|
|
115
|
+
: Result<'okOutput, 'errorOutput> =
|
|
116
|
+
match input with
|
|
117
|
+
| Ok x -> Ok(onOk x)
|
|
118
|
+
| Error err -> Error(onError err)
|
|
119
|
+
|
|
120
|
+
/// <summary>
|
|
121
|
+
/// Applies a function to the value within a <c>Result</c> and returns a new <c>Result</c> with the output of the function.
|
|
122
|
+
///
|
|
123
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/apply</href>
|
|
124
|
+
/// </summary>
|
|
125
|
+
/// <param name="applier">The <c>Result</c>containing the function to apply.</param>
|
|
126
|
+
/// <param name="input">The <c>Result</c>containing the input to apply the function to.</param>
|
|
127
|
+
/// <returns>A <c>Result</c>containing the output of applying the function to the input, or an <c>Error</c> if either <c>Result</c>is an <c>Error</c></returns>
|
|
128
|
+
let inline apply
|
|
129
|
+
(applier: Result<'okInput -> 'okOutput, 'error>)
|
|
130
|
+
(input: Result<'okInput, 'error>)
|
|
131
|
+
: Result<'okOutput, 'error> =
|
|
132
|
+
match (applier, input) with
|
|
133
|
+
| Ok f, Ok x -> Ok(f x)
|
|
134
|
+
| Error e, _
|
|
135
|
+
| _, Error e -> Error e
|
|
136
|
+
|
|
137
|
+
/// <summary>
|
|
138
|
+
/// Applies a mapper function to two input Results, producing a new <c>Result</c>.
|
|
139
|
+
///
|
|
140
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/map2</href>
|
|
141
|
+
/// </summary>
|
|
142
|
+
/// <param name="mapper">The function to apply to the inputs.</param>
|
|
143
|
+
/// <param name="input1">The first input Result.</param>
|
|
144
|
+
/// <param name="input2">The second input Result.</param>
|
|
145
|
+
/// <returns>A new <c>Result</c>containing the output of the mapper function if both input Results are Ok, otherwise an Error Result.</returns>
|
|
146
|
+
let inline map2
|
|
147
|
+
([<InlineIfLambda>] mapper: 'okInput1 -> 'okInput2 -> 'okOutput)
|
|
148
|
+
(input1: Result<'okInput1, 'error>)
|
|
149
|
+
(input2: Result<'okInput2, 'error>)
|
|
150
|
+
: Result<'okOutput, 'error> =
|
|
151
|
+
match (input1, input2) with
|
|
152
|
+
| Ok x, Ok y -> Ok(mapper x y)
|
|
153
|
+
| Error e, _
|
|
154
|
+
| _, Error e -> Error e
|
|
155
|
+
|
|
156
|
+
/// <summary>
|
|
157
|
+
/// Applies a mapper function to three input Results, producing a new <c>Result</c>.
|
|
158
|
+
///
|
|
159
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/map3</href>
|
|
160
|
+
/// </summary>
|
|
161
|
+
/// <param name="mapper">The function to apply to the input results.</param>
|
|
162
|
+
/// <param name="input1">The first input result.</param>
|
|
163
|
+
/// <param name="input2">The second input result.</param>
|
|
164
|
+
/// <param name="input3">The third input result.</param>
|
|
165
|
+
/// <returns>A new <c>Result</c> with the output of the mapper function applied to the input results, if all Results are <c>Ok</c>, otherwise returns the original <c>Error</c></returns>
|
|
166
|
+
let inline map3
|
|
167
|
+
([<InlineIfLambda>] mapper: 'okInput1 -> 'okInput2 -> 'okInput3 -> 'okOutput)
|
|
168
|
+
(input1: Result<'okInput1, 'error>)
|
|
169
|
+
(input2: Result<'okInput2, 'error>)
|
|
170
|
+
(input3: Result<'okInput3, 'error>)
|
|
171
|
+
: Result<'okOutput, 'error> =
|
|
172
|
+
match (input1, input2, input3) with
|
|
173
|
+
| Ok x, Ok y, Ok z -> Ok(mapper x y z)
|
|
174
|
+
| Error e, _, _
|
|
175
|
+
| _, Error e, _
|
|
176
|
+
| _, _, Error e -> Error e
|
|
177
|
+
|
|
178
|
+
/// <summary>
|
|
179
|
+
/// Converts a <c>Choice</c> to a <c>Result</c>
|
|
180
|
+
///
|
|
181
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/transforms/ofchoice</href>
|
|
182
|
+
/// </summary>
|
|
183
|
+
/// <param name="input">The <c>Choice</c> value to convert.</param>
|
|
184
|
+
/// <returns>A <c>Result</c> matching the types of the input <c>Choice</c></returns>
|
|
185
|
+
let inline ofChoice (input: Choice<'ok, 'error>) : Result<'ok, 'error> =
|
|
186
|
+
match input with
|
|
187
|
+
| Choice1Of2 x -> Ok x
|
|
188
|
+
| Choice2Of2 e -> Error e
|
|
189
|
+
|
|
190
|
+
/// <summary>
|
|
191
|
+
/// Calls a <c>TryCreate</c> member function on a value passed in, returning a <c>Result</c> containing the value or an error tuple
|
|
192
|
+
///
|
|
193
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/trycreate</href>
|
|
194
|
+
/// </summary>
|
|
195
|
+
/// <param name="fieldName">The name of the field</param>
|
|
196
|
+
/// <param name="x">The value to create a result from.</param>
|
|
197
|
+
/// <returns>A <c>Result</c> containing the value or an error tuple of the field name and the original error type</returns>
|
|
198
|
+
let inline tryCreate (fieldName: string) (x: 'a) : Result< ^b, (string * 'c) > =
|
|
199
|
+
let tryCreate' x =
|
|
200
|
+
(^b: (static member TryCreate: 'a -> Result< ^b, 'c >) x)
|
|
201
|
+
|
|
202
|
+
tryCreate' x
|
|
203
|
+
|> mapError (fun z -> (fieldName, z))
|
|
204
|
+
|
|
205
|
+
/// <summary>
|
|
206
|
+
/// Returns <paramref name="result"/> if it is <c>Ok</c>, otherwise returns <paramref name="ifError"/>
|
|
207
|
+
///
|
|
208
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/orelsefunctions#result.orelse</href>
|
|
209
|
+
/// </summary>
|
|
210
|
+
/// <param name="ifError">The value to use if <paramref name="result"/> is <c>Error</c></param>
|
|
211
|
+
/// <param name="result">The input result.</param>
|
|
212
|
+
/// <remarks>
|
|
213
|
+
/// </remarks>
|
|
214
|
+
/// <example>
|
|
215
|
+
/// <code>
|
|
216
|
+
/// Error ("First") |> Result.orElse (Error ("Second")) // evaluates to Error ("Second")
|
|
217
|
+
/// Error ("First") |> Result.orElseWith (Ok ("Second")) // evaluates to Ok ("Second")
|
|
218
|
+
/// Ok ("First") |> Result.orElseWith (Error ("Second")) // evaluates to Ok ("First")
|
|
219
|
+
/// Ok ("First") |> Result.orElseWith (Ok ("Second")) // evaluates to Ok ("First")
|
|
220
|
+
/// </code>
|
|
221
|
+
/// </example>
|
|
222
|
+
/// <returns>
|
|
223
|
+
/// The result if the result is Ok, else returns <paramref name="ifError"/>.
|
|
224
|
+
/// </returns>
|
|
225
|
+
let inline orElse
|
|
226
|
+
(ifError: Result<'ok, 'errorOutput>)
|
|
227
|
+
(result: Result<'ok, 'error>)
|
|
228
|
+
: Result<'ok, 'errorOutput> =
|
|
229
|
+
match result with
|
|
230
|
+
| Ok x -> Ok x
|
|
231
|
+
| Error _ -> ifError
|
|
232
|
+
|
|
233
|
+
/// <summary>
|
|
234
|
+
/// Returns <paramref name="result"/> if it is <c>Ok</c>, otherwise executes <paramref name="ifErrorFunc"/> and returns the result.
|
|
235
|
+
///
|
|
236
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/orelsefunctions#result.orelsewith</href>
|
|
237
|
+
/// </summary>
|
|
238
|
+
/// <param name="ifErrorFunc">A function that provides an alternate result when evaluated.</param>
|
|
239
|
+
/// <param name="result">The input result.</param>
|
|
240
|
+
/// <remarks>
|
|
241
|
+
/// <paramref name="ifErrorFunc"/> is not executed unless <paramref name="result"/> is an <c>Error</c>.
|
|
242
|
+
/// </remarks>
|
|
243
|
+
/// <example>
|
|
244
|
+
/// <code>
|
|
245
|
+
/// Error ("First") |> Result.orElseWith (fun _ -> Error ("Second")) // evaluates to Error ("Second")
|
|
246
|
+
/// Error ("First") |> Result.orElseWith (fun _ -> Ok ("Second")) // evaluates to Ok ("Second")
|
|
247
|
+
/// Ok ("First") |> Result.orElseWith (fun _ -> Error ("Second")) // evaluates to Ok ("First")
|
|
248
|
+
/// Ok ("First") |> Result.orElseWith (fun _ -> Ok ("Second")) // evaluates to Ok ("First")
|
|
249
|
+
/// </code>
|
|
250
|
+
/// </example>
|
|
251
|
+
/// <returns>
|
|
252
|
+
/// The result if the result is Ok, else the result of executing <paramref name="ifErrorFunc"/>.
|
|
253
|
+
/// </returns>
|
|
254
|
+
let inline orElseWith
|
|
255
|
+
([<InlineIfLambda>] ifErrorFunc: 'error -> Result<'ok, 'errorOutput>)
|
|
256
|
+
(result: Result<'ok, 'error>)
|
|
257
|
+
: Result<'ok, 'errorOutput> =
|
|
258
|
+
match result with
|
|
259
|
+
| Ok x -> Ok x
|
|
260
|
+
| Error e -> ifErrorFunc e
|
|
261
|
+
|
|
262
|
+
/// <summary>
|
|
263
|
+
/// Ignores the value of a <c>Result</c> and returns a new <c>Result</c> with unit as the success value.
|
|
264
|
+
///
|
|
265
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/others#ignore</href>
|
|
266
|
+
/// </summary>
|
|
267
|
+
/// <param name="result">The <c>Result</c> to ignore.</param>
|
|
268
|
+
/// <returns>A new <c>Result</c> with unit as the success value.</returns>
|
|
269
|
+
let inline ignore<'ok, 'error> (result: Result<'ok, 'error>) : Result<unit, 'error> =
|
|
270
|
+
match result with
|
|
271
|
+
| Ok _ -> Ok()
|
|
272
|
+
| Error e -> Error e
|
|
273
|
+
|
|
274
|
+
/// <summary>
|
|
275
|
+
/// Requires a boolean value to be true, otherwise returns an error result.
|
|
276
|
+
///
|
|
277
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/requirefunctions#requiretrue</href>
|
|
278
|
+
/// </summary>
|
|
279
|
+
/// <param name="error">The error value to return if the condition is false.</param>
|
|
280
|
+
/// <param name="value">The boolean value to check.</param>
|
|
281
|
+
/// <returns>An <c>Ok</c> result if the condition is true, otherwise an Error result with the specified error value.</returns>
|
|
282
|
+
let inline requireTrue (error: 'error) (value: bool) : Result<unit, 'error> =
|
|
283
|
+
if value then Ok() else Error error
|
|
284
|
+
|
|
285
|
+
/// <summary>
|
|
286
|
+
/// Requires a boolean value to be false, otherwise returns an error result.
|
|
287
|
+
///
|
|
288
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/requirefunctions#requirefalse</href>
|
|
289
|
+
/// </summary>
|
|
290
|
+
/// <param name="error">The error value to return if the condition is true.</param>
|
|
291
|
+
/// <param name="value">The boolean value to check.</param>
|
|
292
|
+
/// <returns>An <c>Ok</c> result if the condition is false, otherwise an Error result with the specified error value.</returns>
|
|
293
|
+
let inline requireFalse (error: 'error) (value: bool) : Result<unit, 'error> =
|
|
294
|
+
if not value then Ok() else Error error
|
|
295
|
+
|
|
296
|
+
/// <summary>
|
|
297
|
+
/// Requires a value to be <c>Some</c>, otherwise returns an error result.
|
|
298
|
+
///
|
|
299
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/requirefunctions#requiresome</href>
|
|
300
|
+
/// </summary>
|
|
301
|
+
/// <param name="error">The error value to return if the value is <c>None</c>.</param>
|
|
302
|
+
/// <param name="option">The <c>Option</c> value to check.</param>
|
|
303
|
+
/// <returns>An <c>Ok</c> result if the value is <c>Some</c>, otherwise an Error result with the specified error value.</returns>
|
|
304
|
+
let inline requireSome (error: 'error) (option: 'ok option) : Result<'ok, 'error> =
|
|
305
|
+
match option with
|
|
306
|
+
| Some x -> Ok x
|
|
307
|
+
| None -> Error error
|
|
308
|
+
|
|
309
|
+
/// <summary>
|
|
310
|
+
/// Requires a value to be <c>None</c>, otherwise returns an error result.
|
|
311
|
+
///
|
|
312
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/requirefunctions#requirenone</href>
|
|
313
|
+
/// </summary>
|
|
314
|
+
/// <param name="error">The error value to return if the value is <c>Some</c>.</param>
|
|
315
|
+
/// <param name="option">The <c>Option</c> value to check.</param>
|
|
316
|
+
/// <returns>An <c>Ok</c> result if the value is <c>None</c>, otherwise an Error result with the specified error value.</returns>
|
|
317
|
+
let inline requireNone (error: 'error) (option: 'value option) : Result<unit, 'error> =
|
|
318
|
+
match option with
|
|
319
|
+
| Some _ -> Error error
|
|
320
|
+
| None -> Ok()
|
|
321
|
+
|
|
322
|
+
/// <summary>
|
|
323
|
+
/// Requires a value to be <c>ValueSome</c>, otherwise returns an error result.
|
|
324
|
+
///
|
|
325
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/requirefunctions#requirevaluesome</href>
|
|
326
|
+
/// </summary>
|
|
327
|
+
/// <param name="error">The error value to return if the value is <c>ValueNone</c>.</param>
|
|
328
|
+
/// <param name="voption">The <c>ValueOption</c> value to check.</param>
|
|
329
|
+
/// <returns>An <c>Ok</c> result if the value is <c>ValueSome</c>, otherwise an <c>Error</c> result with the specified error value.</returns>
|
|
330
|
+
let inline requireValueSome (error: 'error) (voption: 'ok voption) : Result<'ok, 'error> =
|
|
331
|
+
match voption with
|
|
332
|
+
| ValueSome x -> Ok x
|
|
333
|
+
| ValueNone -> Error error
|
|
334
|
+
|
|
335
|
+
/// <summary>
|
|
336
|
+
/// Requires a value to be <c>ValueNone</c>, otherwise returns an error result.
|
|
337
|
+
///
|
|
338
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/requirefunctions#requirevaluenone</href>
|
|
339
|
+
/// </summary>
|
|
340
|
+
/// <param name="error">The error value to return if the value is <c>ValueSome</c>.</param>
|
|
341
|
+
/// <param name="voption">The <c>ValueOption</c> value to check.</param>
|
|
342
|
+
/// <returns>An <c>Ok</c> result if the value is <c>ValueNone</c>, otherwise an <c>Error</c> result with the specified error value.</returns>
|
|
343
|
+
let inline requireValueNone (error: 'error) (voption: 'value voption) : Result<unit, 'error> =
|
|
344
|
+
match voption with
|
|
345
|
+
| ValueSome _ -> Error error
|
|
346
|
+
| ValueNone -> Ok()
|
|
347
|
+
|
|
348
|
+
/// <summary>
|
|
349
|
+
/// Converts a nullable value into a <c>Result</c>, using the given error if null
|
|
350
|
+
///
|
|
351
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/requirefunctions#requirenotnull</href>
|
|
352
|
+
/// </summary>
|
|
353
|
+
/// <param name="error">The error value to return if the value is null.</param>
|
|
354
|
+
/// <param name="value">The nullable value to check.</param>
|
|
355
|
+
/// <returns>An <c>Ok</c> result if the value is not null, otherwise an <c>Error</c> result with the specified error value.</returns>
|
|
356
|
+
let inline requireNotNull (error: 'error) (value: 'ok) : Result<'ok, 'error> =
|
|
357
|
+
match value with
|
|
358
|
+
| null -> Error error
|
|
359
|
+
| nonnull -> Ok nonnull
|
|
360
|
+
|
|
361
|
+
/// <summary>
|
|
362
|
+
/// Returns <c>Ok</c> if the two values are equal, or the specified error if not.
|
|
363
|
+
/// Same as <c>requireEqual</c>, but with a signature that fits piping better than normal function application.
|
|
364
|
+
///
|
|
365
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/requirefunctions#requireequalto</href>
|
|
366
|
+
/// </summary>
|
|
367
|
+
/// <param name="other">The value to compare to.</param>
|
|
368
|
+
/// <param name="error">The error value to return if the values are not equal.</param>
|
|
369
|
+
/// <param name="this">The value to compare.</param>
|
|
370
|
+
/// <returns>An <c>Ok</c> result if the values are equal, otherwise an <c>Error</c> result with the specified error value.</returns>
|
|
371
|
+
let inline requireEqualTo
|
|
372
|
+
(other: 'value)
|
|
373
|
+
(error: 'error)
|
|
374
|
+
(this: 'value)
|
|
375
|
+
: Result<unit, 'error> =
|
|
376
|
+
if this = other then Ok() else Error error
|
|
377
|
+
|
|
378
|
+
/// <summary>
|
|
379
|
+
/// Returns <c>Ok</c> if the two values are equal, or the specified error if not.
|
|
380
|
+
/// Same as <c>requireEqualTo</c>, but with a signature that fits normal function application better than piping.
|
|
381
|
+
///
|
|
382
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/requirefunctions#requireequal</href>
|
|
383
|
+
/// </summary>
|
|
384
|
+
/// <param name="x1">The first value to compare.</param>
|
|
385
|
+
/// <param name="x2">The second value to compare.</param>
|
|
386
|
+
/// <param name="error">The error value to return if the values are not equal.</param>
|
|
387
|
+
/// <returns>An <c>Ok</c> result if the values are equal, otherwise an <c>Error</c> result with the specified error value.</returns>
|
|
388
|
+
let inline requireEqual (x1: 'value) (x2: 'value) (error: 'error) : Result<unit, 'error> =
|
|
389
|
+
if x1 = x2 then Ok() else Error error
|
|
390
|
+
|
|
391
|
+
/// <summary>
|
|
392
|
+
/// Returns <c>Ok</c> if the sequence is empty, or the specified error if not.
|
|
393
|
+
///
|
|
394
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/requirefunctions#requireempty</href>
|
|
395
|
+
/// </summary>
|
|
396
|
+
/// <param name="error">The error value to return if the sequence is not empty.</param>
|
|
397
|
+
/// <param name="xs">The sequence to check.</param>
|
|
398
|
+
/// <returns>An <c>Ok</c> result if the sequence is empty, otherwise an <c>Error</c> result with the specified error value.</returns>
|
|
399
|
+
let inline requireEmpty (error: 'error) (xs: #seq<'value>) : Result<unit, 'error> =
|
|
400
|
+
if Seq.isEmpty xs then Ok() else Error error
|
|
401
|
+
|
|
402
|
+
/// <summary>
|
|
403
|
+
/// Returns <c>Ok</c> if the sequence is not empty, or the specified error if it is.
|
|
404
|
+
///
|
|
405
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/requirefunctions#requirenotempty</href>
|
|
406
|
+
/// </summary>
|
|
407
|
+
/// <param name="error">The error value to return if the sequence is empty.</param>
|
|
408
|
+
/// <param name="xs">The sequence to check.</param>
|
|
409
|
+
/// <returns>An <c>Ok</c> result if the sequence is not empty, otherwise an <c>Error</c> result with the specified error value.</returns>
|
|
410
|
+
let inline requireNotEmpty (error: 'error) (xs: #seq<'value>) : Result<unit, 'error> =
|
|
411
|
+
if Seq.isEmpty xs then Error error else Ok()
|
|
412
|
+
|
|
413
|
+
/// <summary>
|
|
414
|
+
/// Returns the first item of the sequence if it exists, or the specified error if the sequence is empty.
|
|
415
|
+
///
|
|
416
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/requirefunctions#requirehead</href>
|
|
417
|
+
/// </summary>
|
|
418
|
+
/// <param name="error">The error value to return if the sequence is empty.</param>
|
|
419
|
+
/// <param name="xs">The sequence to check.</param>
|
|
420
|
+
/// <returns>An <c>Ok</c> result containing the first item of the sequence if it exists, otherwise an <c>Error</c> result with the specified error value.</returns>
|
|
421
|
+
let inline requireHead (error: 'error) (xs: #seq<'ok>) : Result<'ok, 'error> =
|
|
422
|
+
match Seq.tryHead xs with
|
|
423
|
+
| Some x -> Ok x
|
|
424
|
+
| None -> Error error
|
|
425
|
+
|
|
426
|
+
/// <summary>
|
|
427
|
+
/// Replaces an error value with a custom error value.
|
|
428
|
+
///
|
|
429
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/others#seterror</href>
|
|
430
|
+
/// </summary>
|
|
431
|
+
/// <param name="error">The error value to replace the original error value with.</param>
|
|
432
|
+
/// <param name="result">The input result.</param>
|
|
433
|
+
/// <returns>A new <c>Result</c> with the same Ok value and the new error value.</returns>
|
|
434
|
+
let inline setError (error: 'error) (result: Result<'ok, 'errorIgnored>) : Result<'ok, 'error> =
|
|
435
|
+
result
|
|
436
|
+
|> mapError (fun _ -> error)
|
|
437
|
+
|
|
438
|
+
/// <summary>
|
|
439
|
+
/// Replaces a unit error value with a custom error value. Safer than setError
|
|
440
|
+
/// since you're not losing any information.
|
|
441
|
+
///
|
|
442
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/others#witherror</href>
|
|
443
|
+
/// </summary>
|
|
444
|
+
/// <param name="error">The error value to replace the original error value with.</param>
|
|
445
|
+
/// <param name="result">The input result.</param>
|
|
446
|
+
/// <returns>A new <c>Result</c> with the same Ok value and the new error value.</returns>
|
|
447
|
+
let inline withError (error: 'error) (result: Result<'ok, unit>) : Result<'ok, 'error> =
|
|
448
|
+
result
|
|
449
|
+
|> mapError (fun () -> error)
|
|
450
|
+
|
|
451
|
+
/// <summary>
|
|
452
|
+
/// Returns the contained value if <c>Ok</c>, otherwise returns the provided value
|
|
453
|
+
///
|
|
454
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/others#defaultvalue</href>
|
|
455
|
+
/// </summary>
|
|
456
|
+
/// <param name="ifError">The value to use if the result is <c>Error</c>.</param>
|
|
457
|
+
/// <param name="result">The input result.</param>
|
|
458
|
+
/// <returns>The contained value if <c>Ok</c>, otherwise <paramref name="ifError"/>.</returns>
|
|
459
|
+
let inline defaultValue (ifError: 'ok) (result: Result<'ok, 'error>) : 'ok =
|
|
460
|
+
match result with
|
|
461
|
+
| Ok x -> x
|
|
462
|
+
| Error _ -> ifError
|
|
463
|
+
|
|
464
|
+
/// <summary>
|
|
465
|
+
/// Returns the contained value if <c>Error</c>, otherwise returns the provided value
|
|
466
|
+
///
|
|
467
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/others#defaulterror</href>
|
|
468
|
+
/// </summary>
|
|
469
|
+
/// <param name="ifOk">The value to use if the result is <c>Ok</c>.</param>
|
|
470
|
+
/// <param name="result">The input result.</param>
|
|
471
|
+
/// <returns>The contained value if <c>Error</c>, otherwise <paramref name="ifOk"/>.</returns>
|
|
472
|
+
let inline defaultError (ifOk: 'error) (result: Result<'ok, 'error>) : 'error =
|
|
473
|
+
match result with
|
|
474
|
+
| Error error -> error
|
|
475
|
+
| Ok _ -> ifOk
|
|
476
|
+
|
|
477
|
+
/// <summary>
|
|
478
|
+
/// Returns the contained value if <c>Ok</c>, otherwise evaluates <param name="ifErrorThunk"/> and returns the value.
|
|
479
|
+
///
|
|
480
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/others#defaultwith</href>
|
|
481
|
+
/// </summary>
|
|
482
|
+
/// <param name="ifErrorThunk">The function to evaluate if the result is <c>Error</c>.</param>
|
|
483
|
+
/// <param name="result">The input result.</param>
|
|
484
|
+
/// <returns>The contained value if <c>Ok</c>, otherwise the result of evaluating <paramref name="ifErrorThunk"/>.</returns>
|
|
485
|
+
let inline defaultWith
|
|
486
|
+
([<InlineIfLambda>] ifErrorThunk: 'error -> 'ok)
|
|
487
|
+
(result: Result<'ok, 'error>)
|
|
488
|
+
: 'ok =
|
|
489
|
+
match result with
|
|
490
|
+
| Ok x -> x
|
|
491
|
+
| Error e -> ifErrorThunk e
|
|
492
|
+
|
|
493
|
+
/// <summary>
|
|
494
|
+
/// Same as <c>defaultValue</c> for a result where the Ok value is unit. The name
|
|
495
|
+
/// describes better what is actually happening in this case.
|
|
496
|
+
///
|
|
497
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/others#ignoreerror</href>
|
|
498
|
+
/// </summary>
|
|
499
|
+
/// <param name="result">The input result.</param>
|
|
500
|
+
/// <returns>Unit if <c>Ok</c>, otherwise the provided value.</returns>
|
|
501
|
+
let inline ignoreError<'error> (result: Result<unit, 'error>) : unit = defaultValue () result
|
|
502
|
+
|
|
503
|
+
/// <summary>
|
|
504
|
+
/// If the result is <c>Ok</c> and the predicate returns true, executes the function on the <c>Ok</c> value. Passes through the input value.
|
|
505
|
+
///
|
|
506
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/teefunctions#teeif</href>
|
|
507
|
+
/// </summary>
|
|
508
|
+
/// <param name="predicate">The predicate to evaluate on the <c>Ok</c> value.</param>
|
|
509
|
+
/// <param name="sideEffect">The function to execute on the <c>Ok</c> value if the predicate returns true.</param>
|
|
510
|
+
/// <param name="result">The input result.</param>
|
|
511
|
+
/// <returns>The input result.</returns>
|
|
512
|
+
let inline teeIf
|
|
513
|
+
([<InlineIfLambda>] predicate: 'ok -> bool)
|
|
514
|
+
([<InlineIfLambda>] sideEffect: 'ok -> unit)
|
|
515
|
+
(result: Result<'ok, 'error>)
|
|
516
|
+
: Result<'ok, 'error> =
|
|
517
|
+
match result with
|
|
518
|
+
| Ok x ->
|
|
519
|
+
if predicate x then
|
|
520
|
+
sideEffect x
|
|
521
|
+
| Error _ -> ()
|
|
522
|
+
|
|
523
|
+
result
|
|
524
|
+
|
|
525
|
+
/// <summary>
|
|
526
|
+
/// If the result is <c>Error</c> and the predicate returns true, executes the
|
|
527
|
+
/// function on the <c>Error</c> value. Passes through the input value.
|
|
528
|
+
///
|
|
529
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/teefunctions#teeerrorif</href>
|
|
530
|
+
/// </summary>
|
|
531
|
+
/// <param name="predicate">The predicate to evaluate on the <c>Error</c> value.</param>
|
|
532
|
+
/// <param name="sideEffect">The function to execute on the <c>Error</c> value if the predicate returns true.</param>
|
|
533
|
+
/// <param name="result">The input result.</param>
|
|
534
|
+
/// <returns>The input result.</returns>
|
|
535
|
+
let inline teeErrorIf
|
|
536
|
+
([<InlineIfLambda>] predicate: 'error -> bool)
|
|
537
|
+
([<InlineIfLambda>] sideEffect: 'error -> unit)
|
|
538
|
+
(result: Result<'ok, 'error>)
|
|
539
|
+
: Result<'ok, 'error> =
|
|
540
|
+
match result with
|
|
541
|
+
| Ok _ -> ()
|
|
542
|
+
| Error x ->
|
|
543
|
+
if predicate x then
|
|
544
|
+
sideEffect x
|
|
545
|
+
|
|
546
|
+
result
|
|
547
|
+
|
|
548
|
+
/// <summary>
|
|
549
|
+
/// If the result is <c>Ok</c>, executes the function on the <c>Ok</c> value. Passes through the input value.
|
|
550
|
+
///
|
|
551
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/teefunctions#tee</href>
|
|
552
|
+
/// </summary>
|
|
553
|
+
/// <param name="sideEffect">The function to execute on the <c>Ok</c> value.</param>
|
|
554
|
+
/// <param name="result">The input result.</param>
|
|
555
|
+
/// <returns>The input result.</returns>
|
|
556
|
+
let inline tee
|
|
557
|
+
([<InlineIfLambda>] sideEffect: 'ok -> unit)
|
|
558
|
+
(result: Result<'ok, 'error>)
|
|
559
|
+
: Result<'ok, 'error> =
|
|
560
|
+
teeIf (fun _ -> true) sideEffect result
|
|
561
|
+
|
|
562
|
+
/// <summary>
|
|
563
|
+
/// If the result is <c>Error</c>, executes the function on the <c>Error</c> value. Passes through the input value.
|
|
564
|
+
///
|
|
565
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/teefunctions#teeerror</href>
|
|
566
|
+
/// </summary>
|
|
567
|
+
/// <param name="sideEffect">The function to execute on the <c>Error</c> value.</param>
|
|
568
|
+
/// <param name="result">The input result.</param>
|
|
569
|
+
/// <returns>The input result.</returns>
|
|
570
|
+
let inline teeError
|
|
571
|
+
([<InlineIfLambda>] sideEffect: 'error -> unit)
|
|
572
|
+
(result: Result<'ok, 'error>)
|
|
573
|
+
: Result<'ok, 'error> =
|
|
574
|
+
teeErrorIf (fun _ -> true) sideEffect result
|
|
575
|
+
|
|
576
|
+
/// Converts a Result<Async<_>,_> to an Async<Result<_,_>>
|
|
577
|
+
let inline sequenceAsync (resAsync: Result<Async<'ok>, 'error>) : Async<Result<'ok, 'error>> =
|
|
578
|
+
async {
|
|
579
|
+
match resAsync with
|
|
580
|
+
| Ok asnc ->
|
|
581
|
+
let! x = asnc
|
|
582
|
+
return Ok x
|
|
583
|
+
| Error err -> return Error err
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/// <summary>
|
|
587
|
+
/// Maps an Async function over a Result, returning an Async Result.
|
|
588
|
+
/// </summary>
|
|
589
|
+
/// <param name="f">The function to map over the Result.</param>
|
|
590
|
+
/// <param name="res">The Result to map over.</param>
|
|
591
|
+
/// <returns>An Async Result with the mapped value.</returns>
|
|
592
|
+
let inline traverseAsync
|
|
593
|
+
([<InlineIfLambda>] f: 'okInput -> Async<'okOutput>)
|
|
594
|
+
(res: Result<'okInput, 'error>)
|
|
595
|
+
: Async<Result<'okOutput, 'error>> =
|
|
596
|
+
sequenceAsync ((map f) res)
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
/// <summary>
|
|
600
|
+
/// Returns the <c>Ok</c> value or runs the specified function over the error value.
|
|
601
|
+
///
|
|
602
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/others#valueor</href>
|
|
603
|
+
/// </summary>
|
|
604
|
+
/// <param name="f">The function to run over the error value.</param>
|
|
605
|
+
/// <param name="res">The input result.</param>
|
|
606
|
+
/// <returns>The <c>Ok</c> value if the result is <c>Ok</c>, otherwise the result of running the function over the error value.</returns>
|
|
607
|
+
let inline valueOr ([<InlineIfLambda>] f: 'error -> 'ok) (res: Result<'ok, 'error>) : 'ok =
|
|
608
|
+
match res with
|
|
609
|
+
| Ok x -> x
|
|
610
|
+
| Error x -> f x
|
|
611
|
+
|
|
612
|
+
/// <summary>
|
|
613
|
+
/// Takes two results and returns a tuple of the pair
|
|
614
|
+
///
|
|
615
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/zip</href>
|
|
616
|
+
/// </summary>
|
|
617
|
+
/// <param name="left">The first input result.</param>
|
|
618
|
+
/// <param name="right">The second input result.</param>
|
|
619
|
+
/// <returns>A tuple of the pair of the input results.</returns>
|
|
620
|
+
let zip
|
|
621
|
+
(left: Result<'leftOk, 'error>)
|
|
622
|
+
(right: Result<'rightOk, 'error>)
|
|
623
|
+
: Result<'leftOk * 'rightOk, 'error> =
|
|
624
|
+
match left, right with
|
|
625
|
+
| Ok x1res, Ok x2res -> Ok(x1res, x2res)
|
|
626
|
+
| Error e, _ -> Error e
|
|
627
|
+
| _, Error e -> Error e
|
|
628
|
+
|
|
629
|
+
/// <summary>
|
|
630
|
+
/// Takes two results and returns a tuple of the error pair
|
|
631
|
+
///
|
|
632
|
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/ziperror</href>
|
|
633
|
+
/// </summary>
|
|
634
|
+
/// <param name="left">The first input result.</param>
|
|
635
|
+
/// <param name="right">The second input result.</param>
|
|
636
|
+
/// <returns>A tuple of the error pair of the input results.</returns>
|
|
637
|
+
let zipError
|
|
638
|
+
(left: Result<'ok, 'leftError>)
|
|
639
|
+
(right: Result<'ok, 'rightError>)
|
|
640
|
+
: Result<'ok, 'leftError * 'rightError> =
|
|
641
|
+
match left, right with
|
|
642
|
+
| Error x1res, Error x2res -> Error(x1res, x2res)
|
|
643
|
+
| Ok e, _ -> Ok e
|
|
644
|
+
| _, Ok e -> Ok e
|