10 "github.com/anacrolix/missinggo/expect"
13 //----------------------------------------------------------------------------
15 //----------------------------------------------------------------------------
17 // In case if marshaler cannot encode a type, it will return this error. Typical
18 // example of such type is float32/float64 which has no bencode representation.
19 type MarshalTypeError struct {
23 func (e *MarshalTypeError) Error() string {
24 return "bencode: unsupported type: " + e.Type.String()
27 // Unmarshal argument must be a non-nil value of some pointer type.
28 type UnmarshalInvalidArgError struct {
32 func (e *UnmarshalInvalidArgError) Error() string {
34 return "bencode: Unmarshal(nil)"
37 if e.Type.Kind() != reflect.Ptr {
38 return "bencode: Unmarshal(non-pointer " + e.Type.String() + ")"
40 return "bencode: Unmarshal(nil " + e.Type.String() + ")"
43 // Unmarshaler spotted a value that was not appropriate for a given Go value.
44 type UnmarshalTypeError struct {
49 func (e *UnmarshalTypeError) Error() string {
50 return "bencode: value (" + e.Value + ") is not appropriate for type: " +
54 // Unmarshaler tried to write to an unexported (therefore unwritable) field.
55 type UnmarshalFieldError struct {
58 Field reflect.StructField
61 func (e *UnmarshalFieldError) Error() string {
62 return "bencode: key \"" + e.Key + "\" led to an unexported field \"" +
63 e.Field.Name + "\" in type: " + e.Type.String()
66 // Malformed bencode input, unmarshaler failed to parse it.
67 type SyntaxError struct {
68 Offset int64 // location of the error
69 What error // error description
72 func (e *SyntaxError) Error() string {
73 return fmt.Sprintf("bencode: syntax error (offset: %d): %s", e.Offset, e.What)
76 // A non-nil error was returned after calling MarshalBencode on a type which
77 // implements the Marshaler interface.
78 type MarshalerError struct {
83 func (e *MarshalerError) Error() string {
84 return "bencode: error calling MarshalBencode for type " + e.Type.String() + ": " + e.Err.Error()
87 // A non-nil error was returned after calling UnmarshalBencode on a type which
88 // implements the Unmarshaler interface.
89 type UnmarshalerError struct {
94 func (e *UnmarshalerError) Error() string {
95 return "bencode: error calling UnmarshalBencode for type " + e.Type.String() + ": " + e.Err.Error()
98 //----------------------------------------------------------------------------
100 //----------------------------------------------------------------------------
102 // Any type which implements this interface, will be marshaled using the
104 type Marshaler interface {
105 MarshalBencode() ([]byte, error)
108 // Any type which implements this interface, will be unmarshaled using the
110 type Unmarshaler interface {
111 UnmarshalBencode([]byte) error
114 // Marshal the value 'v' to the bencode form, return the result as []byte and an
116 func Marshal(v interface{}) ([]byte, error) {
118 e := Encoder{w: bufio.NewWriter(&buf)}
123 return buf.Bytes(), nil
126 func MustMarshal(v interface{}) []byte {
132 // Unmarshal the bencode value in the 'data' to a value pointed by the 'v'
133 // pointer, return a non-nil error if any.
134 func Unmarshal(data []byte, v interface{}) (err error) {
135 buf := bytes.NewBuffer(data)
138 if err == nil && buf.Len() != 0 {
139 err = ErrUnusedTrailingBytes{buf.Len()}
144 type ErrUnusedTrailingBytes struct {
148 func (me ErrUnusedTrailingBytes) Error() string {
149 return fmt.Sprintf("%d unused trailing bytes", me.NumUnusedBytes)
152 func NewDecoder(r io.Reader) *Decoder {
153 return &Decoder{r: &scanner{r: r}}
156 func NewEncoder(w io.Writer) *Encoder {
157 return &Encoder{w: bufio.NewWriter(w)}