core/internal/domain/coords.go

54 lines
886 B
Go

package domain
import (
"strconv"
"strings"
)
type Coords struct {
x int
y int
}
var ErrInvalidCoordsString error = simpleError{
msg: "invalid coords string, should be x|y",
typ: ErrorTypeIncorrectInput,
code: "invalid-coords-string",
}
const coordsSubstrings = 2
func newCoords(s string) (Coords, error) {
coords := strings.SplitN(s, "|", coordsSubstrings)
if len(coords) != coordsSubstrings {
return Coords{}, ErrInvalidCoordsString
}
x, err := strconv.Atoi(coords[0])
if err != nil || x <= 0 {
return Coords{}, ErrInvalidCoordsString
}
y, err := strconv.Atoi(coords[1])
if err != nil || y <= 0 {
return Coords{}, ErrInvalidCoordsString
}
return Coords{
x: x,
y: y,
}, nil
}
func (c Coords) X() int {
return c.x
}
func (c Coords) Y() int {
return c.y
}
func (c Coords) String() string {
return strconv.Itoa(c.x) + "|" + strconv.Itoa(c.y)
}