beancode

package module
v0.0.0-...-67a7a8d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2024 License: MIT Imports: 6 Imported by: 0

README

Beancode

Working with BitTorrent? You need Beancode.

Features

  • Easy API based on stdlib
  • Almost full unit test coverage: 86%
  • Support int, string, slice, map, and struct

Usage

Let's define a common struct for encoding and decoding processes:

type Boo struct {
	Foo int `beancode:"foo"`
	Bar string `Beancode:"bar"`
}

To marshal into Bencode:

from := Boo{Foo: 42, Bar: "qux"}
val, err := beancode.Marshal(from)

To unmarshal from Bencode:

var to Boo
from := "d3:fooi42e3:bar3:quxe"

err := beancode.Unmarshal(from, &to)

Voila, as simple as that!

Install

go get github.com/dxtym/beancode

Benchmarks

Covered on AMD Ryzen 3 4300U with Radeon Graphics (8GB RAM).

  • Marshal: 3652 ns/op, 361 B/op, 17 allocs/op
  • Unmarshal: 3672 ns/op, 1515 B/op, 22 allocs/op

Plans

  • Implement decode to struct
  • Enhance benchmark performance
  • Have some documentation

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(data any) (string, error)

Marshal converts the data to corresponding value of Bencode encoding and returns it.

func Unmarshal

func Unmarshal(data string, v any) error

Unmarshal parses the Bencoded data and stores the result in the value pointed by v.

Types

type DecodeError

type DecodeError struct {
	Type string
	Err  error
}

func (*DecodeError) Error

func (d *DecodeError) Error() string

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

func (*Decoder) Decode

func (d *Decoder) Decode(v any) error

type EncodeError

type EncodeError struct {
	Type string
	Err  error
}

better error handling

func (*EncodeError) Error

func (e *EncodeError) Error() string

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

func (*Encoder) Encode

func (e *Encoder) Encode(v any) error

type Marshaler

type Marshaler interface {
	Marshaler(data any) (string, error)
}

Marshaler is the interface implemented by types that can marshal themselves to valid Bencode.

type Unmarshaler

type Unmarshaler interface {
	Unmarshal(data string, v any) error
}

Unmarshaler is the interface implemented by types that can unmarshal a Bencode of themselves.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL