-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult_err.go
71 lines (53 loc) · 1008 Bytes
/
result_err.go
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
package shine
import (
"errors"
"io"
)
type Err[T any] [1]error
func (e Err[T]) UnwrapOr(def T) T {
return def
}
func (e Err[T]) UnwrapOrDefault() T {
var def T
return def
}
func (e Err[T]) UnwrapOrElse(fn func(e error) T) T {
return fn(e[0])
}
func (e Err[T]) AndThen(fn func(v T) Result[T]) Result[T] {
return e
}
func (e Err[T]) Ok() Option[T] {
return NewNone[T]()
}
func (e Err[T]) Err() Option[error] {
return NewSome[error](e[0])
}
func (e Err[T]) Close() error {
if cl, ok := e[0].(io.Closer); ok {
return cl.Close()
}
return nil
}
func (Err[T]) result() {
}
func (e Err[T]) Unwrap() error {
return e[0]
}
func (e Err[T]) Error() string {
return e[0].Error()
}
func (e Err[T]) Is(err error) bool {
return errors.Is(e[0], err)
}
func (e Err[T]) As(v any) bool {
// passed value as expected to be a pointer
return errors.As(e[0], v)
}
func NewErr[T any](err error) Err[T] {
return Err[T]{err}
}
var _ interface {
Result[struct{}]
error
} = (*Err[struct{}])(nil)