]> Sergey Matveev's repositories - btrtrc.git/blob - bencode/api.go
bencode: Improve support for embedded structs
[btrtrc.git] / bencode / api.go
1 package bencode
2
3 import (
4         "bytes"
5         "fmt"
6         "io"
7         "reflect"
8
9         "github.com/anacrolix/missinggo/expect"
10 )
11
12 //----------------------------------------------------------------------------
13 // Errors
14 //----------------------------------------------------------------------------
15
16 // In case if marshaler cannot encode a type, it will return this error. Typical
17 // example of such type is float32/float64 which has no bencode representation.
18 type MarshalTypeError struct {
19         Type reflect.Type
20 }
21
22 func (e *MarshalTypeError) Error() string {
23         return "bencode: unsupported type: " + e.Type.String()
24 }
25
26 // Unmarshal argument must be a non-nil value of some pointer type.
27 type UnmarshalInvalidArgError struct {
28         Type reflect.Type
29 }
30
31 func (e *UnmarshalInvalidArgError) Error() string {
32         if e.Type == nil {
33                 return "bencode: Unmarshal(nil)"
34         }
35
36         if e.Type.Kind() != reflect.Ptr {
37                 return "bencode: Unmarshal(non-pointer " + e.Type.String() + ")"
38         }
39         return "bencode: Unmarshal(nil " + e.Type.String() + ")"
40 }
41
42 // Unmarshaler spotted a value that was not appropriate for a given Go value.
43 type UnmarshalTypeError struct {
44         Value string
45         Type  reflect.Type
46 }
47
48 func (e *UnmarshalTypeError) Error() string {
49         return fmt.Sprintf("cannot unmarshal a bencode %s into a %s", e.Value, e.Type)
50 }
51
52 // Unmarshaler tried to write to an unexported (therefore unwritable) field.
53 type UnmarshalFieldError struct {
54         Key   string
55         Type  reflect.Type
56         Field reflect.StructField
57 }
58
59 func (e *UnmarshalFieldError) Error() string {
60         return "bencode: key \"" + e.Key + "\" led to an unexported field \"" +
61                 e.Field.Name + "\" in type: " + e.Type.String()
62 }
63
64 // Malformed bencode input, unmarshaler failed to parse it.
65 type SyntaxError struct {
66         Offset int64 // location of the error
67         What   error // error description
68 }
69
70 func (e *SyntaxError) Error() string {
71         return fmt.Sprintf("bencode: syntax error (offset: %d): %s", e.Offset, e.What)
72 }
73
74 // A non-nil error was returned after calling MarshalBencode on a type which
75 // implements the Marshaler interface.
76 type MarshalerError struct {
77         Type reflect.Type
78         Err  error
79 }
80
81 func (e *MarshalerError) Error() string {
82         return "bencode: error calling MarshalBencode for type " + e.Type.String() + ": " + e.Err.Error()
83 }
84
85 // A non-nil error was returned after calling UnmarshalBencode on a type which
86 // implements the Unmarshaler interface.
87 type UnmarshalerError struct {
88         Type reflect.Type
89         Err  error
90 }
91
92 func (e *UnmarshalerError) Error() string {
93         return "bencode: error calling UnmarshalBencode for type " + e.Type.String() + ": " + e.Err.Error()
94 }
95
96 //----------------------------------------------------------------------------
97 // Interfaces
98 //----------------------------------------------------------------------------
99
100 // Any type which implements this interface, will be marshaled using the
101 // specified method.
102 type Marshaler interface {
103         MarshalBencode() ([]byte, error)
104 }
105
106 // Any type which implements this interface, will be unmarshaled using the
107 // specified method.
108 type Unmarshaler interface {
109         UnmarshalBencode([]byte) error
110 }
111
112 // Marshal the value 'v' to the bencode form, return the result as []byte and
113 // an error if any.
114 func Marshal(v interface{}) ([]byte, error) {
115         var buf bytes.Buffer
116         e := Encoder{w: &buf}
117         err := e.Encode(v)
118         if err != nil {
119                 return nil, err
120         }
121         return buf.Bytes(), nil
122 }
123
124 func MustMarshal(v interface{}) []byte {
125         b, err := Marshal(v)
126         expect.Nil(err)
127         return b
128 }
129
130 // Unmarshal the bencode value in the 'data' to a value pointed by the 'v'
131 // pointer, return a non-nil error if any.
132 func Unmarshal(data []byte, v interface{}) (err error) {
133         buf := bytes.NewReader(data)
134         e := Decoder{r: buf}
135         err = e.Decode(v)
136         if err == nil && buf.Len() != 0 {
137                 err = ErrUnusedTrailingBytes{buf.Len()}
138         }
139         return
140 }
141
142 type ErrUnusedTrailingBytes struct {
143         NumUnusedBytes int
144 }
145
146 func (me ErrUnusedTrailingBytes) Error() string {
147         return fmt.Sprintf("%d unused trailing bytes", me.NumUnusedBytes)
148 }
149
150 func NewDecoder(r io.Reader) *Decoder {
151         return &Decoder{r: &scanner{r: r}}
152 }
153
154 func NewEncoder(w io.Writer) *Encoder {
155         return &Encoder{w: w}
156 }