-
Notifications
You must be signed in to change notification settings - Fork 328
/
DecodeError.ts
165 lines (149 loc) · 3.33 KB
/
DecodeError.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* @since 2.2.7
*/
import { Semigroup } from 'fp-ts/lib/Semigroup'
import * as FS from './FreeSemigroup'
/**
* @category model
* @since 2.2.7
*/
export interface Leaf<E> {
readonly _tag: 'Leaf'
readonly actual: unknown
readonly error: E
}
/**
* @category model
* @since 2.2.7
*/
export const required: 'required' = 'required'
/**
* @category model
* @since 2.2.7
*/
export const optional: 'optional' = 'optional'
/**
* @category model
* @since 2.2.7
*/
export type Kind = 'required' | 'optional'
/**
* @category model
* @since 2.2.7
*/
export interface Key<E> {
readonly _tag: 'Key'
readonly key: string
readonly kind: Kind
readonly errors: FS.FreeSemigroup<DecodeError<E>>
}
/**
* @category model
* @since 2.2.7
*/
export interface Index<E> {
readonly _tag: 'Index'
readonly index: number
readonly kind: Kind
readonly errors: FS.FreeSemigroup<DecodeError<E>>
}
/**
* @category model
* @since 2.2.7
*/
export interface Member<E> {
readonly _tag: 'Member'
readonly index: number
readonly errors: FS.FreeSemigroup<DecodeError<E>>
}
/**
* @category model
* @since 2.2.7
*/
export interface Lazy<E> {
readonly _tag: 'Lazy'
readonly id: string
readonly errors: FS.FreeSemigroup<DecodeError<E>>
}
/**
* @category model
* @since 2.2.7
*/
export type DecodeError<E> = Leaf<E> | Key<E> | Index<E> | Member<E> | Lazy<E>
/**
* @category constructors
* @since 2.2.7
*/
export const leaf = <E>(actual: unknown, error: E): DecodeError<E> => ({ _tag: 'Leaf', actual, error })
/**
* @category constructors
* @since 2.2.7
*/
export const key = <E>(key: string, kind: Kind, errors: FS.FreeSemigroup<DecodeError<E>>): DecodeError<E> => ({
_tag: 'Key',
key,
kind,
errors
})
/**
* @category constructors
* @since 2.2.7
*/
export const index = <E>(index: number, kind: Kind, errors: FS.FreeSemigroup<DecodeError<E>>): DecodeError<E> => ({
_tag: 'Index',
index,
kind,
errors
})
/**
* @category constructors
* @since 2.2.7
*/
export const member = <E>(index: number, errors: FS.FreeSemigroup<DecodeError<E>>): DecodeError<E> => ({
_tag: 'Member',
index,
errors
})
/**
* @category constructors
* @since 2.2.7
*/
export const lazy = <E>(id: string, errors: FS.FreeSemigroup<DecodeError<E>>): DecodeError<E> => ({
_tag: 'Lazy',
id,
errors
})
/**
* @category destructors
* @since 2.2.7
*/
export const fold = <E, R>(patterns: {
Leaf: (input: unknown, error: E) => R
Key: (key: string, kind: Kind, errors: FS.FreeSemigroup<DecodeError<E>>) => R
Index: (index: number, kind: Kind, errors: FS.FreeSemigroup<DecodeError<E>>) => R
Member: (index: number, errors: FS.FreeSemigroup<DecodeError<E>>) => R
Lazy: (id: string, errors: FS.FreeSemigroup<DecodeError<E>>) => R
}): ((e: DecodeError<E>) => R) => {
const f = (e: DecodeError<E>): R => {
switch (e._tag) {
case 'Leaf':
return patterns.Leaf(e.actual, e.error)
case 'Key':
return patterns.Key(e.key, e.kind, e.errors)
case 'Index':
return patterns.Index(e.index, e.kind, e.errors)
case 'Member':
return patterns.Member(e.index, e.errors)
case 'Lazy':
return patterns.Lazy(e.id, e.errors)
}
}
return f
}
/**
* @category instances
* @since 2.2.7
*/
export function getSemigroup<E = never>(): Semigroup<FS.FreeSemigroup<DecodeError<E>>> {
return FS.getSemigroup()
}