2016-08-26 14:51:09 +10:00
|
|
|
package bencode
|
|
|
|
|
2021-11-02 17:28:05 +11:00
|
|
|
import (
|
|
|
|
"errors"
|
2022-03-09 20:57:23 +11:00
|
|
|
"fmt"
|
2021-11-02 17:28:05 +11:00
|
|
|
)
|
|
|
|
|
2016-08-26 14:51:09 +10:00
|
|
|
type Bytes []byte
|
|
|
|
|
|
|
|
var (
|
2021-09-17 21:36:25 -05:00
|
|
|
_ Unmarshaler = (*Bytes)(nil)
|
|
|
|
_ Marshaler = (*Bytes)(nil)
|
2016-08-26 20:29:29 +10:00
|
|
|
_ Marshaler = Bytes{}
|
2016-08-26 14:51:09 +10:00
|
|
|
)
|
|
|
|
|
|
|
|
func (me *Bytes) UnmarshalBencode(b []byte) error {
|
|
|
|
*me = append([]byte(nil), b...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-26 20:29:29 +10:00
|
|
|
func (me Bytes) MarshalBencode() ([]byte, error) {
|
2021-11-02 17:28:05 +11:00
|
|
|
if len(me) == 0 {
|
|
|
|
return nil, errors.New("marshalled Bytes should not be zero-length")
|
|
|
|
}
|
2016-08-26 20:29:29 +10:00
|
|
|
return me, nil
|
2016-08-26 14:51:09 +10:00
|
|
|
}
|
2022-03-09 20:57:23 +11:00
|
|
|
|
|
|
|
func (me Bytes) GoString() string {
|
|
|
|
return fmt.Sprintf("bencode.Bytes(%q)", []byte(me))
|
|
|
|
}
|