csv2db/encodings.go
2022-09-18 12:43:16 +05:00

50 lines
843 B
Go

package main
import (
"fmt"
"io"
"github.com/dimchansky/utfbom"
"golang.org/x/text/encoding/charmap"
)
type Encoding int
const (
Utf8 Encoding = iota
Win1251
)
func (e Encoding) Translate(r io.Reader) (io.Reader, error) {
switch e {
case Utf8:
return utfbom.SkipOnly(r), nil
case Win1251:
return charmap.Windows1251.NewDecoder().Reader(r), nil
}
return nil, fmt.Errorf("unknown encoding id = %d", e)
}
func (e Encoding) MarshalText() (text []byte, err error) {
switch e {
case Utf8:
return []byte("utf8"), nil
case Win1251:
return []byte("win1251"), nil
}
return nil, fmt.Errorf("unknown encoding id = %d", e)
}
func (e *Encoding) UnmarshalText(text []byte) error {
switch string(text) {
case "utf8":
*e = Utf8
case "win1251":
*e = Win1251
}
return fmt.Errorf("unknown encoding: %s", string(text))
}