mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 22:54:01 +03:00
FILE / ScuroNeko/mtg
config/type_bytes.go
Исходный файл и его история в репозитории.
48 lines
787 B
Go
48 lines
787 B
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/alecthomas/units"
|
|
)
|
|
|
|
type TypeBytes struct {
|
|
value uint
|
|
}
|
|
|
|
func (c *TypeBytes) UnmarshalText(data []byte) error {
|
|
if len(data) == 0 {
|
|
return nil
|
|
}
|
|
|
|
value, err := units.ParseStrictBytes(strings.ToUpper(string(data)))
|
|
if err != nil {
|
|
return fmt.Errorf("incorrect bytes value: %w", err)
|
|
}
|
|
|
|
if value < 0 {
|
|
return fmt.Errorf("%d should be positive number", value)
|
|
}
|
|
|
|
c.value = uint(value)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c TypeBytes) MarshalText() ([]byte, error) { // nolint: unparam
|
|
return []byte(c.String()), nil
|
|
}
|
|
|
|
func (c TypeBytes) String() string {
|
|
return units.ToString(int64(c.value), 1024, "ib", "b")
|
|
}
|
|
|
|
func (c TypeBytes) Value(defaultValue uint) uint {
|
|
if c.value == 0 {
|
|
return defaultValue
|
|
}
|
|
|
|
return c.value
|
|
}
|