1
|
|
|
package decode |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"errors" |
5
|
|
|
"reflect" |
6
|
|
|
"strings" |
7
|
|
|
) |
8
|
|
|
|
9
|
|
|
var ( |
10
|
|
|
ErrUnknownType = errors.New("unknown storage type") |
11
|
|
|
ErrCantSetValue = errors.New("can't set value") |
12
|
|
|
ErrItemNotFound = errors.New("item not found") |
13
|
|
|
ErrInvalidItem = errors.New("invalid item") |
14
|
|
|
ErrInvalidStructValue = errors.New("invalid struct value") |
15
|
|
|
ErrIncompleteRead = errors.New("incomplete read") |
16
|
|
|
) |
17
|
|
|
|
18
|
|
|
type FailedToReadTypeError struct { |
19
|
|
|
Previous error |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
func (err *FailedToReadTypeError) Error() string { |
23
|
|
|
text := strings.Builder{} |
24
|
|
|
text.WriteString("failed to read type") |
25
|
|
|
|
26
|
|
|
if err.Previous == nil { |
27
|
|
|
return text.String() |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
text.WriteString(": ") |
31
|
|
|
text.WriteString(err.Previous.Error()) |
32
|
|
|
|
33
|
|
|
return text.String() |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
func (err *FailedToReadTypeError) Unwrap() error { |
37
|
|
|
return err.Previous |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
type FailedToReadSizeError struct { |
41
|
|
|
Previous error |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
func (err *FailedToReadSizeError) Error() string { |
45
|
|
|
text := strings.Builder{} |
46
|
|
|
text.WriteString("failed to read size") |
47
|
|
|
|
48
|
|
|
if err.Previous == nil { |
49
|
|
|
return text.String() |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
text.WriteString(": ") |
53
|
|
|
text.WriteString(err.Previous.Error()) |
54
|
|
|
|
55
|
|
|
return text.String() |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
func (err *FailedToReadSizeError) Unwrap() error { |
59
|
|
|
return err.Previous |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
type InvalidUnmarshalError struct { |
63
|
|
|
Type reflect.Type |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
func (e *InvalidUnmarshalError) Error() string { |
67
|
|
|
if e.Type == nil { |
68
|
|
|
return "binn: Unmarshal(nil)" |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if e.Type.Kind() != reflect.Ptr { |
72
|
|
|
return "binn: Unmarshal(non-pointer " + e.Type.String() + ")" |
73
|
|
|
} |
74
|
|
|
return "binn: Unmarshal(nil " + e.Type.String() + ")" |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
type UnknownValueError struct { |
78
|
|
|
Expected reflect.Kind |
79
|
|
|
Got reflect.Kind |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
func (e *UnknownValueError) Error() string { |
83
|
|
|
return "binn: Unknown value. Expected " + e.Expected.String() + ", got " + e.Got.String() |
84
|
|
|
} |
85
|
|
|
|