generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 120
/
encode.go
41 lines (36 loc) · 1.18 KB
/
encode.go
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
package prolog
import (
"errors"
"github.com/ichiban/prolog/engine"
"github.com/axone-protocol/axoned/v8/x/logic/util"
)
// Encode encodes the given string with the given encoding.
// value is used for error reporting (the culprit).
func Encode(value engine.Term, str string, encoding engine.Atom, env *engine.Env) ([]byte, error) {
bs, err := util.Encode(str, encoding.String())
if err != nil {
switch {
case errors.Is(err, util.ErrInvalidCharset):
return nil, engine.TypeError(AtomTypeCharset, encoding, env)
default:
return nil, WithError(
engine.DomainError(ValidEncoding(encoding.String()), value, env), err, env)
}
}
return bs, nil
}
// Decode decodes the given byte slice with the given encoding.
// value is used for error reporting (the culprit).
func Decode(value engine.Term, bs []byte, encoding engine.Atom, env *engine.Env) (string, error) {
str, err := util.Decode(bs, encoding.String())
if err != nil {
switch {
case errors.Is(err, util.ErrInvalidCharset):
return "", engine.TypeError(AtomTypeCharset, encoding, env)
default:
return "", WithError(
engine.DomainError(ValidEncoding(encoding.String()), value, env), err, env)
}
}
return str, nil
}