9 //----------------------------------------------------------------------------
11 //----------------------------------------------------------------------------
13 // In case if marshaler cannot encode a type in bencode, it will return this
14 // error. Typical example of such type is float32/float64 which has no bencode
16 type MarshalTypeError struct {
20 func (this *MarshalTypeError) Error() string {
21 return "bencode: unsupported type: " + this.Type.String()
24 // Unmarshal argument must be a non-nil value of some pointer type.
25 type UnmarshalInvalidArgError struct {
29 func (e *UnmarshalInvalidArgError) Error() string {
31 return "bencode: Unmarshal(nil)"
34 if e.Type.Kind() != reflect.Ptr {
35 return "bencode: Unmarshal(non-pointer " + e.Type.String() + ")"
37 return "bencode: Unmarshal(nil " + e.Type.String() + ")"
40 // Unmarshaler spotted a value that was not appropriate for a given specific Go
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 type SyntaxError struct {
65 Offset int64 // location of the error
66 what string // error description
69 func (e *SyntaxError) Error() string {
70 return "bencode: syntax error (offset: " +
71 strconv.FormatInt(e.Offset, 10) +
75 type MarshalerError struct {
80 func (e *MarshalerError) Error() string {
81 return "bencode: error calling MarshalBencode for type " + e.Type.String() + ": " + e.Err.Error()
84 //----------------------------------------------------------------------------
86 //----------------------------------------------------------------------------
88 // unused for now (TODO)
89 type Marshaler interface {
90 MarshalBencode() ([]byte, error)
93 // unused for now (TODO)
94 type Unmarshaler interface {
95 UnmarshalBencode([]byte) error
98 //----------------------------------------------------------------------------
99 // Stateless interface
100 //----------------------------------------------------------------------------
102 func Marshal(v interface{}) ([]byte, error) {
104 e := encoder{Writer: bufio.NewWriter(&buf)}
109 return buf.Bytes(), nil
112 func Unmarshal(data []byte, v interface{}) error {
113 e := decoder{Reader: bufio.NewReader(bytes.NewBuffer(data))}
117 //----------------------------------------------------------------------------
118 // Stateful interface
119 //----------------------------------------------------------------------------
121 type Decoder struct {
125 func NewDecoder(r io.Reader) *Decoder {
126 return &Decoder{decoder{Reader: bufio.NewReader(r)}}
129 func (d *Decoder) Decode(v interface{}) error {
133 type Encoder struct {
137 func NewEncoder(w io.Writer) *Encoder {
138 return &Encoder{encoder{Writer: bufio.NewWriter(w)}}
141 func (e *Encoder) Encode(v interface{}) error {