From 9d67accb9a5358da44cc43c29c4921a64af33f20 Mon Sep 17 00:00:00 2001 From: surdeus Date: Fri, 31 May 2024 18:58:28 +0500 Subject: [PATCH] fix: rebranding. --- inns/errors.go | 6 +++--- inns/main.go | 54 ++++++++++++++++++++++++++++---------------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/inns/errors.go b/inns/errors.go index 05d5871..08584a9 100644 --- a/inns/errors.go +++ b/inns/errors.go @@ -5,7 +5,7 @@ import ( ) var ( - IncorrectFormatErr = errors.New("incorrect INN format") - IncorrectLenErr = errors.New("incorrect INN length") - SumsNotMatchErr = errors.New("control sums do not match") + ErrIncorrectFormat = errors.New("incorrect INN format") + ErrIncorrectLen = errors.New("incorrect INN length") + ErrSumsNotMatch = errors.New("control sums do not match") ) diff --git a/inns/main.go b/inns/main.go index 850638c..2efbede 100644 --- a/inns/main.go +++ b/inns/main.go @@ -14,13 +14,14 @@ import ( "slices" ) -type InnType uint8 +type Type uint8 const ( - IncorrectInnType InnType = iota - LegalInnType - IndividualInnType + TypeIncorrect Type = iota + TypeLegal + TypeIndividual ) + func removeSpaces(s string) string { return strings.Map(func(r rune) rune{ if unicode.IsSpace(r) { @@ -30,45 +31,50 @@ func removeSpaces(s string) string { }, s) } -type Inn int64 +type INN int64 -func NewFromInt64(innRaw int64) (Inn, error) { - inn := Inn(innRaw) +func NewFromInt64(innRaw int64) (INN, error) { + inn := INN(innRaw) if !inn.isCorrectLen() { - return -1, IncorrectLenErr + return -1, ErrIncorrectLen } if !inn.isSumCorrect() { - return -1, SumsNotMatchErr + return -1, ErrSumsNotMatch } return inn, nil } + +func (inn INN) IsCorrect() bool { + _, err := NewFromInt64(int64(inn)) + return err == nil +} // Convert string with spaces into the INN // and return error if anything is wrong with it. -func NewFromStr(str string) (Inn, error) { +func NewFromStr(str string) (INN, error) { str = removeSpaces(str) innRaw, err := strconv.ParseInt(str, 10, 64) if err != nil { - return -1, IncorrectFormatErr + return -1, ErrIncorrectFormat } return NewFromInt64(innRaw) } -func (inn Inn) Type() InnType { +func (inn INN) Type() Type { ln := inn.length() switch ln { case 9, 10: - return LegalInnType + return TypeLegal case 11, 12: - return IndividualInnType + return TypeIndividual } - return IncorrectInnType + return TypeIncorrect } -func (inn Inn) length() int { +func (inn INN) length() int { ln := 0 for { if inn == 0 { @@ -83,7 +89,7 @@ func (inn Inn) length() int { return ln } -func (inn Inn) isCorrectLen() bool { +func (inn INN) isCorrectLen() bool { ln := inn.length() if ln == 9 || ln == 11 { ln++ @@ -105,7 +111,7 @@ var ( ) // Get the integer representation of digits in string INN. -func (inn Inn) digits() []int64 { +func (inn INN) digits() []int64 { ln := inn.length() v := int64(inn) 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() v := int64(inn) switch ln { @@ -132,7 +138,7 @@ func (inn Inn) specifiedSum() int64 { } // Returns the supposed control sum rune. (the last number) -func (inn Inn) sum() int64 { +func (inn INN) sum() int64 { d := inn.digits() ln := inn.length() switch ln { @@ -162,16 +168,16 @@ func (inn Inn) sum() int64 { return -1 } -func (inn Inn) isSumCorrect() bool { +func (inn INN) isSumCorrect() bool { return inn.specifiedSum() == inn.sum() } -func (inn Inn) String() string { +func (inn INN) String() string { return fmt.Sprintf("%d", inn) } // Fancy way to print INN with space delimiters. -func (inn Inn) Fancy() string { +func (inn INN) Fancy() string { ln := inn.length() str := fmt.Sprintf("%d", inn) @@ -188,6 +194,6 @@ func (inn Inn) Fancy() string { } // Reaching the point if the length is incorrect. - return "<"+IncorrectLenErr.Error()+">" + return "<"+ErrIncorrectLen.Error()+">" }