2021-02-08 16:44:35 +00:00
|
|
|
package locodecolumn
|
|
|
|
|
|
|
|
import (
|
2022-05-20 07:43:13 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/locode"
|
2021-02-08 16:44:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const locationCodeLen = 3
|
|
|
|
|
|
|
|
// LocationCode represents 3-character code for the location.
|
|
|
|
type LocationCode [locationCodeLen]uint8
|
|
|
|
|
|
|
|
// Symbols returns characters of the location code.
|
|
|
|
func (lc *LocationCode) Symbols() [locationCodeLen]uint8 {
|
|
|
|
return *lc
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// LocationCodeFromString parses a string and returns the location code.
|
2021-02-08 16:44:35 +00:00
|
|
|
func LocationCodeFromString(s string) (*LocationCode, error) {
|
2022-05-20 07:43:13 +00:00
|
|
|
if l := len(s); l != locationCodeLen {
|
|
|
|
return nil, fmt.Errorf("incorrect location code length: expect: %d, got: %d",
|
|
|
|
locationCodeLen,
|
|
|
|
l,
|
|
|
|
)
|
2021-02-08 16:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range s {
|
|
|
|
if !isUpperAlpha(s[i]) && !isDigit(s[i]) {
|
|
|
|
return nil, locode.ErrInvalidString
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lc := LocationCode{}
|
|
|
|
copy(lc[:], s)
|
|
|
|
|
|
|
|
return &lc, nil
|
|
|
|
}
|