2021-02-08 16:44:35 +00:00
|
|
|
package locodecolumn
|
|
|
|
|
|
|
|
import (
|
2022-05-20 07:43:13 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-02-08 16:44:35 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/locode"
|
|
|
|
)
|
|
|
|
|
|
|
|
const countryCodeLen = 2
|
|
|
|
|
|
|
|
// CountryCode represents ISO 3166 alpha-2 Country Code.
|
|
|
|
type CountryCode [countryCodeLen]uint8
|
|
|
|
|
|
|
|
// Symbols returns digits of the country code.
|
|
|
|
func (cc *CountryCode) Symbols() [countryCodeLen]uint8 {
|
|
|
|
return *cc
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// CountryCodeFromString parses a string and returns the country code.
|
2021-02-08 16:44:35 +00:00
|
|
|
func CountryCodeFromString(s string) (*CountryCode, error) {
|
2022-05-20 07:43:13 +00:00
|
|
|
if l := len(s); l != countryCodeLen {
|
|
|
|
return nil, fmt.Errorf("incorrect country code length: expect: %d, got: %d",
|
|
|
|
countryCodeLen,
|
|
|
|
l,
|
|
|
|
)
|
2021-02-08 16:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range s {
|
|
|
|
if !isUpperAlpha(s[i]) {
|
|
|
|
return nil, locode.ErrInvalidString
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cc := CountryCode{}
|
|
|
|
copy(cc[:], s)
|
|
|
|
|
|
|
|
return &cc, nil
|
|
|
|
}
|