File: encoder.go

package info (click to toggle)
golang-github-multiformats-go-multibase 0.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 216 kB
  • sloc: makefile: 7
file content (65 lines) | stat: -rw-r--r-- 1,570 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package multibase

import (
	"fmt"
	"unicode/utf8"
)

// Encoder is a multibase encoding that is verified to be supported and
// supports an Encode method that does not return an error
type Encoder struct {
	enc Encoding
}

// NewEncoder create a new Encoder from an Encoding
func NewEncoder(base Encoding) (Encoder, error) {
	_, ok := EncodingToStr[base]
	if !ok {
		return Encoder{-1}, fmt.Errorf("unsupported multibase encoding: %d", base)
	}
	return Encoder{base}, nil
}

// MustNewEncoder is like NewEncoder but will panic if the encoding is
// invalid.
func MustNewEncoder(base Encoding) Encoder {
	_, ok := EncodingToStr[base]
	if !ok {
		panic("Unsupported multibase encoding")
	}
	return Encoder{base}
}

// EncoderByName creates an encoder from a string, the string can
// either be the multibase name or single character multibase prefix
func EncoderByName(str string) (Encoder, error) {
	var base Encoding
	var ok bool
	if len(str) == 0 {
		return Encoder{-1}, fmt.Errorf("empty multibase encoding")
	} else if utf8.RuneCountInString(str) == 1 {
		r, _ := utf8.DecodeRuneInString(str)
		base = Encoding(r)
		_, ok = EncodingToStr[base]
	} else {
		base, ok = Encodings[str]
	}
	if !ok {
		return Encoder{-1}, fmt.Errorf("unsupported multibase encoding: %s", str)
	}
	return Encoder{base}, nil
}

func (p Encoder) Encoding() Encoding {
	return p.enc
}

// Encode encodes the multibase using the given Encoder.
func (p Encoder) Encode(data []byte) string {
	str, err := Encode(p.enc, data)
	if err != nil {
		// should not happen
		panic(err)
	}
	return str
}