11 //----------------------------------------------------------------------------
13 //----------------------------------------------------------------------------
15 // In case if marshaler cannot encode a type, it will return this error. Typical
16 // example of such type is float32/float64 which has no bencode representation.
17 type MarshalTypeError struct {
21 func (e *MarshalTypeError) Error() string {
22 return "bencode: unsupported type: " + e.Type.String()
25 // Unmarshal argument must be a non-nil value of some pointer type.
26 type UnmarshalInvalidArgError struct {
30 func (e *UnmarshalInvalidArgError) Error() string {
32 return "bencode: Unmarshal(nil)"
35 if e.Type.Kind() != reflect.Ptr {
36 return "bencode: Unmarshal(non-pointer " + e.Type.String() + ")"
38 return "bencode: Unmarshal(nil " + e.Type.String() + ")"
41 // Unmarshaler spotted a value that was not appropriate for a given Go value.
42 type UnmarshalTypeError struct {
47 func (e *UnmarshalTypeError) Error() string {
48 return "bencode: value (" + e.Value + ") is not appropriate for type: " +
52 // Unmarshaler tried to write to an unexported (therefore unwritable) field.
53 type UnmarshalFieldError struct {
56 Field reflect.StructField
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()
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
70 func (e *SyntaxError) Error() string {
71 return fmt.Sprintf("bencode: syntax error (offset: %d): %s", e.Offset, e.What)
74 // A non-nil error was returned after calling MarshalBencode on a type which
75 // implements the Marshaler interface.
76 type MarshalerError struct {
81 func (e *MarshalerError) Error() string {
82 return "bencode: error calling MarshalBencode for type " + e.Type.String() + ": " + e.Err.Error()
85 // A non-nil error was returned after calling UnmarshalBencode on a type which
86 // implements the Unmarshaler interface.
87 type UnmarshalerError struct {
92 func (e *UnmarshalerError) Error() string {
93 return "bencode: error calling UnmarshalBencode for type " + e.Type.String() + ": " + e.Err.Error()
96 //----------------------------------------------------------------------------
98 //----------------------------------------------------------------------------
100 // Any type which implements this interface, will be marshaled using the
102 type Marshaler interface {
103 MarshalBencode() ([]byte, error)
106 // Any type which implements this interface, will be unmarshaled using the
108 type Unmarshaler interface {
109 UnmarshalBencode([]byte) error
112 // Marshal the value 'v' to the bencode form, return the result as []byte and an
114 func Marshal(v interface{}) ([]byte, error) {
116 e := Encoder{Writer: bufio.NewWriter(&buf)}
121 return buf.Bytes(), nil
124 // Unmarshal the bencode value in the 'data' to a value pointed by the 'v'
125 // pointer, return a non-nil error if any.
126 func Unmarshal(data []byte, v interface{}) error {
127 e := Decoder{r: bytes.NewBuffer(data)}
131 func NewDecoder(r io.Reader) *Decoder {
132 return &Decoder{r: bufio.NewReader(r)}
135 func NewEncoder(w io.Writer) *Encoder {
136 return &Encoder{Writer: bufio.NewWriter(w)}