-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
142 lines (119 loc) · 2.58 KB
/
option.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
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
package scale_codec
import (
"bytes"
"fmt"
"io"
)
var NoneEncoded = []byte{0x00}
type OptionG[T Marshaler] struct {
inner T
isNone bool
}
func UnmarshalOptionFromRawBytes[T Marshaler](
f func(io.Reader) (T, error)) func(reader io.Reader) (*OptionG[T], error) {
return func(reader io.Reader) (*OptionG[T], error) {
option := &OptionG[T]{}
err := option.UnmarshalSCALE(reader, f)
if err != nil {
return nil, err
}
return option, nil
}
}
func (o *OptionG[T]) MarshalSCALE() ([]byte, error) {
if o.isNone {
return NoneEncoded, nil
}
encodedOptionTag := []byte{0x01}
encodedInner, err := o.inner.MarshalSCALE()
if err != nil {
return nil, err
}
return bytes.Join([][]byte{encodedOptionTag, encodedInner}, nil), nil
}
func (o *OptionG[T]) UnmarshalSCALE(reader io.Reader, f func(io.Reader) (T, error)) error {
encodedOptionTag := make([]byte, 1)
n, err := reader.Read(encodedOptionTag)
if err != nil {
return err
}
if n != 1 {
return fmt.Errorf("%w: want: 1, got: %v", ErrUnexpectedReadBytes, n)
}
switch encodedOptionTag[0] {
case 0x00:
o.isNone = true
return nil
case 0x01:
innerValue, err := f(reader)
if err != nil {
return err
}
o.inner = innerValue
o.isNone = false
return nil
default:
return fmt.Errorf("%w: %v", ErrUnexpectedOptionTag, encodedOptionTag[0])
}
}
func SomeG[T Marshaler](inner T) *OptionG[T] {
return &OptionG[T]{inner, false}
}
func NoneG[T Marshaler]() *OptionG[T] {
return &OptionG[T]{
isNone: true,
}
}
type Option struct {
inner Encodable
isNone bool
}
func NewOption(encodable Encodable) *Option {
return &Option{inner: encodable}
}
func None() *Option {
return &Option{
isNone: true,
}
}
func Some(v Encodable) *Option {
return &Option{
inner: v,
isNone: false,
}
}
func (o Option) MarshalSCALE() ([]byte, error) {
if o.isNone {
return NoneEncoded, nil
}
encodedOptionTag := []byte{0x01}
encodedInner, err := o.inner.MarshalSCALE()
if err != nil {
return nil, err
}
return bytes.Join([][]byte{encodedOptionTag, encodedInner}, nil), nil
}
func (o *Option) UnmarshalSCALE(reader io.Reader) error {
encodedOptionTag := make([]byte, 1)
n, err := reader.Read(encodedOptionTag)
if err != nil {
return err
}
if n != 1 {
return fmt.Errorf("%w: want: 1, got: %v", ErrUnexpectedReadBytes, n)
}
switch encodedOptionTag[0] {
case 0x00:
o.inner = nil
o.isNone = true
return nil
case 0x01:
o.isNone = false
return o.inner.UnmarshalSCALE(reader)
default:
return fmt.Errorf("%w: %v", ErrUnexpectedOptionTag, encodedOptionTag[0])
}
}
func (o *Option) IsNone() bool {
return o.isNone
}