api.go 4.4 KB

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