Total Lines | 82 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 0 |
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 | ) |
||
16 | |||
17 | type FailedToReadTypeError struct { |
||
18 | Previous error |
||
19 | } |
||
20 | |||
21 | func (err *FailedToReadTypeError) Error() string { |
||
22 | text := strings.Builder{} |
||
23 | text.WriteString("failed to read type") |
||
24 | |||
25 | if err.Previous == nil { |
||
26 | return text.String() |
||
27 | } |
||
28 | |||
29 | text.WriteString(": ") |
||
30 | text.WriteString(err.Previous.Error()) |
||
31 | |||
32 | return text.String() |
||
33 | } |
||
34 | |||
35 | func (err *FailedToReadTypeError) Unwrap() error { |
||
36 | return err.Previous |
||
37 | } |
||
38 | |||
39 | type FailedToReadSizeError struct { |
||
40 | Previous error |
||
41 | } |
||
42 | |||
43 | func (err *FailedToReadSizeError) Error() string { |
||
44 | text := strings.Builder{} |
||
45 | text.WriteString("failed to read size") |
||
46 | |||
47 | if err.Previous == nil { |
||
48 | return text.String() |
||
49 | } |
||
50 | |||
51 | text.WriteString(": ") |
||
52 | text.WriteString(err.Previous.Error()) |
||
53 | |||
54 | return text.String() |
||
55 | } |
||
56 | |||
57 | func (err *FailedToReadSizeError) Unwrap() error { |
||
58 | return err.Previous |
||
59 | } |
||
60 | |||
61 | type InvalidUnmarshalError struct { |
||
62 | Type reflect.Type |
||
63 | } |
||
64 | |||
65 | func (e *InvalidUnmarshalError) Error() string { |
||
66 | if e.Type == nil { |
||
67 | return "binn: Unmarshal(nil)" |
||
68 | } |
||
69 | |||
70 | if e.Type.Kind() != reflect.Ptr { |
||
71 | return "binn: Unmarshal(non-pointer " + e.Type.String() + ")" |
||
72 | } |
||
73 | return "binn: Unmarshal(nil " + e.Type.String() + ")" |
||
74 | } |
||
75 | |||
76 | type UnknownValueError struct { |
||
77 | Expected reflect.Kind |
||
78 | Got reflect.Kind |
||
79 | } |
||
80 | |||
81 | func (e *UnknownValueError) Error() string { |
||
82 | return "binn: Unknown value. Expected " + e.Expected.String() + ", got " + e.Got.String() |
||
83 | } |
||
84 |