fix: rebranding.

This commit is contained in:
Andrey Parhomenko 2024-05-31 18:58:28 +05:00
parent 7f647616e2
commit 9d67accb9a
2 changed files with 33 additions and 27 deletions

View file

@ -5,7 +5,7 @@ import (
) )
var ( var (
IncorrectFormatErr = errors.New("incorrect INN format") ErrIncorrectFormat = errors.New("incorrect INN format")
IncorrectLenErr = errors.New("incorrect INN length") ErrIncorrectLen = errors.New("incorrect INN length")
SumsNotMatchErr = errors.New("control sums do not match") ErrSumsNotMatch = errors.New("control sums do not match")
) )

View file

@ -14,13 +14,14 @@ import (
"slices" "slices"
) )
type InnType uint8 type Type uint8
const ( const (
IncorrectInnType InnType = iota TypeIncorrect Type = iota
LegalInnType TypeLegal
IndividualInnType TypeIndividual
) )
func removeSpaces(s string) string { func removeSpaces(s string) string {
return strings.Map(func(r rune) rune{ return strings.Map(func(r rune) rune{
if unicode.IsSpace(r) { if unicode.IsSpace(r) {
@ -30,45 +31,50 @@ func removeSpaces(s string) string {
}, s) }, s)
} }
type Inn int64 type INN int64
func NewFromInt64(innRaw int64) (Inn, error) { func NewFromInt64(innRaw int64) (INN, error) {
inn := Inn(innRaw) inn := INN(innRaw)
if !inn.isCorrectLen() { if !inn.isCorrectLen() {
return -1, IncorrectLenErr return -1, ErrIncorrectLen
} }
if !inn.isSumCorrect() { if !inn.isSumCorrect() {
return -1, SumsNotMatchErr return -1, ErrSumsNotMatch
} }
return inn, nil return inn, nil
} }
func (inn INN) IsCorrect() bool {
_, err := NewFromInt64(int64(inn))
return err == nil
}
// Convert string with spaces into the INN // Convert string with spaces into the INN
// and return error if anything is wrong with it. // and return error if anything is wrong with it.
func NewFromStr(str string) (Inn, error) { func NewFromStr(str string) (INN, error) {
str = removeSpaces(str) str = removeSpaces(str)
innRaw, err := strconv.ParseInt(str, 10, 64) innRaw, err := strconv.ParseInt(str, 10, 64)
if err != nil { if err != nil {
return -1, IncorrectFormatErr return -1, ErrIncorrectFormat
} }
return NewFromInt64(innRaw) return NewFromInt64(innRaw)
} }
func (inn Inn) Type() InnType { func (inn INN) Type() Type {
ln := inn.length() ln := inn.length()
switch ln { switch ln {
case 9, 10: case 9, 10:
return LegalInnType return TypeLegal
case 11, 12: case 11, 12:
return IndividualInnType return TypeIndividual
} }
return IncorrectInnType return TypeIncorrect
} }
func (inn Inn) length() int { func (inn INN) length() int {
ln := 0 ln := 0
for { for {
if inn == 0 { if inn == 0 {
@ -83,7 +89,7 @@ func (inn Inn) length() int {
return ln return ln
} }
func (inn Inn) isCorrectLen() bool { func (inn INN) isCorrectLen() bool {
ln := inn.length() ln := inn.length()
if ln == 9 || ln == 11 { if ln == 9 || ln == 11 {
ln++ ln++
@ -105,7 +111,7 @@ var (
) )
// Get the integer representation of digits in string INN. // Get the integer representation of digits in string INN.
func (inn Inn) digits() []int64 { func (inn INN) digits() []int64 {
ln := inn.length() ln := inn.length()
v := int64(inn) v := int64(inn)
ret := make([]int64, ln) ret := make([]int64, ln)
@ -118,7 +124,7 @@ func (inn Inn) digits() []int64 {
} }
// //
func (inn Inn) specifiedSum() int64 { func (inn INN) specifiedSum() int64 {
ln := inn.length() ln := inn.length()
v := int64(inn) v := int64(inn)
switch ln { switch ln {
@ -132,7 +138,7 @@ func (inn Inn) specifiedSum() int64 {
} }
// Returns the supposed control sum rune. (the last number) // Returns the supposed control sum rune. (the last number)
func (inn Inn) sum() int64 { func (inn INN) sum() int64 {
d := inn.digits() d := inn.digits()
ln := inn.length() ln := inn.length()
switch ln { switch ln {
@ -162,16 +168,16 @@ func (inn Inn) sum() int64 {
return -1 return -1
} }
func (inn Inn) isSumCorrect() bool { func (inn INN) isSumCorrect() bool {
return inn.specifiedSum() == inn.sum() return inn.specifiedSum() == inn.sum()
} }
func (inn Inn) String() string { func (inn INN) String() string {
return fmt.Sprintf("%d", inn) return fmt.Sprintf("%d", inn)
} }
// Fancy way to print INN with space delimiters. // Fancy way to print INN with space delimiters.
func (inn Inn) Fancy() string { func (inn INN) Fancy() string {
ln := inn.length() ln := inn.length()
str := fmt.Sprintf("%d", inn) str := fmt.Sprintf("%d", inn)
@ -188,6 +194,6 @@ func (inn Inn) Fancy() string {
} }
// Reaching the point if the length is incorrect. // Reaching the point if the length is incorrect.
return "<"+IncorrectLenErr.Error()+">" return "<"+ErrIncorrectLen.Error()+">"
} }