Skip to content

Commit

Permalink
move some error strings to lib/strings.go as consts
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Feb 25, 2024
1 parent 2af2306 commit b1057f4
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/alter.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ func (g *alterGenerator) Entropy(s *State) (float64, error) {
if g.entropy != nil {
return *g.entropy, nil
}
return 0, s.errorUnknown("entropy is not calculated")
return 0, s.errorUnknown(s_entropy_not_calc)
}
2 changes: 1 addition & 1 deletion lib/bip39.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func bip39encode(s *State, in []rune) ([]rune, error) {
data, err := hex.DecodeString(string(in))
if err != nil {
s.errorMarkLen = len(in)
return nil, s.errorValue("invalid hex number %#v", string(in))
return nil, s.errorValue(s_invalid_hex_num, string(in))
}
return []rune(bip39.Encode(data)), nil
}
Expand Down
16 changes: 8 additions & 8 deletions lib/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var encoderFunctions = map[string]func(s *State, in []rune) ([]rune, error){
"base64": func(s *State, in []rune) ([]rune, error) {
data, err := hex.DecodeString(string(in))
if err != nil {
return nil, s.errorValue("invalid hex number %#v", string(in))
return nil, s.errorValue(s_invalid_hex_num, string(in))
}
return []rune(
base64.StdEncoding.EncodeToString(data),
Expand All @@ -37,7 +37,7 @@ var encoderFunctions = map[string]func(s *State, in []rune) ([]rune, error){
"base64url": func(s *State, in []rune) ([]rune, error) {
data, err := hex.DecodeString(string(in))
if err != nil {
return nil, s.errorValue("invalid hex number %#v", string(in))
return nil, s.errorValue(s_invalid_hex_num, string(in))
}
return []rune(
base64.URLEncoding.EncodeToString(data),
Expand All @@ -48,7 +48,7 @@ var encoderFunctions = map[string]func(s *State, in []rune) ([]rune, error){
"base32": func(s *State, in []rune) ([]rune, error) {
data, err := hex.DecodeString(string(in))
if err != nil {
return nil, s.errorValue("invalid hex number %#v", string(in))
return nil, s.errorValue(s_invalid_hex_num, string(in))
}
return []rune(
strings.ToLower(crock32.Encode(data)),
Expand All @@ -57,7 +57,7 @@ var encoderFunctions = map[string]func(s *State, in []rune) ([]rune, error){
"BASE32": func(s *State, in []rune) ([]rune, error) {
data, err := hex.DecodeString(string(in))
if err != nil {
return nil, s.errorValue("invalid hex number %#v", string(in))
return nil, s.errorValue(s_invalid_hex_num, string(in))
}
return []rune(
crock32.Encode(data),
Expand All @@ -68,7 +68,7 @@ var encoderFunctions = map[string]func(s *State, in []rune) ([]rune, error){
"base32std": func(s *State, in []rune) ([]rune, error) {
data, err := hex.DecodeString(string(in))
if err != nil {
return nil, s.errorValue("invalid hex number %#v", string(in))
return nil, s.errorValue(s_invalid_hex_num, string(in))
}
return []rune(
base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(
Expand All @@ -95,7 +95,7 @@ var encoderFunctions = map[string]func(s *State, in []rune) ([]rune, error){
//}
i64, err := strconv.ParseInt(string(in), 16, 64)
if err != nil {
return nil, s.errorValue("invalid hex number %#v", string(in))
return nil, s.errorValue(s_invalid_hex_num, string(in))
}
return []rune(strconv.FormatInt(i64, 10)), nil
},
Expand All @@ -104,7 +104,7 @@ var encoderFunctions = map[string]func(s *State, in []rune) ([]rune, error){
"pyhex": func(s *State, in []rune) ([]rune, error) {
data, err := hex.DecodeString(string(in))
if err != nil {
return nil, s.errorValue("invalid hex number %#v", string(in))
return nil, s.errorValue(s_invalid_hex_num, string(in))
}
out := ""
for _, cbyte := range data {
Expand Down Expand Up @@ -187,7 +187,7 @@ func (g *encoderFunctionCallGenerator) Entropy(s *State) (float64, error) {
if g.entropy != nil {
return *g.entropy, nil
}
return 0, s.errorUnknown("entropy is not calculated")
return 0, s.errorUnknown(s_entropy_not_calc)
}

func getFuncGenerator(s *State, funcName string, arg []rune) (generatorIface, error) {
Expand Down
2 changes: 1 addition & 1 deletion lib/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ func (g *groupGenerator) Entropy(s *State) (float64, error) {
if g.entropy != nil {
return *g.entropy, nil
}
return 0, s.errorUnknown("entropy is not calculated")
return 0, s.errorUnknown(s_entropy_not_calc)
}
2 changes: 1 addition & 1 deletion lib/justify.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (g *justifyGenerator) Entropy(s *State) (float64, error) {
if g.entropy != nil {
return *g.entropy, nil
}
return 0, s.errorUnknown("entropy is not calculated")
return 0, s.errorUnknown(s_entropy_not_calc)
}

func parseJustifyArgs(s *State, argsStr []rune, funcName string) (*JustifyArgs, error) {
Expand Down
4 changes: 2 additions & 2 deletions lib/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ func lexBackslash(s *State) (LexType, error) {
func lexIdent(s *State) (LexType, error) {
if s.end() {
s.errorOffset++
return nil, s.errorSyntax("expected a function call")
return nil, s.errorSyntax(s_func_call_expected)
}
c := s.input[s.inputPos]
s.move(1)
switch c {
case '\\', '[', '{', '$':
return nil, s.errorSyntax("expected a function call")
return nil, s.errorSyntax(s_func_call_expected)
case '(':
s.openParenth++
return lexIdentFuncCall, nil
Expand Down
4 changes: 2 additions & 2 deletions lib/lex_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func _lexIdentFuncCallEndError(s *State) error {
if s.openParenth > 0 {
return s.errorSyntax("'(' not closed")
}
return s.errorSyntax("expected a function call")
return s.errorSyntax(s_func_call_expected)
}

func _lexIdentFuncCallBackslash(s *State, buff []rune) []rune {
Expand Down Expand Up @@ -86,5 +86,5 @@ func lexIdentFuncCall(s *State) (LexType, error) {
if s.openParenth > 0 {
return nil, s.errorSyntax("'(' not closed")
}
return nil, s.errorSyntax("expected a function call")
return nil, s.errorSyntax(s_func_call_expected)
}
10 changes: 5 additions & 5 deletions lib/lex_repeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ func parseRepeatCount(s *State, countRunes []rune) (int64, error) {
if err != nil {
s.errorOffset--
s.errorMarkLen = len(countStr)
return 0, s.errorSyntax("invalid natural number '%v'", countStr)
return 0, s.errorSyntax(s_invalid_natural_num, countStr)
}
if countI64 < 1 {
s.errorOffset--
s.errorMarkLen = len(countStr)
return 0, s.errorSyntax("invalid natural number '%v'", countStr)
return 0, s.errorSyntax(s_invalid_natural_num, countStr)
}
if countI64 > maxRepeatCount {
return 0, s.errorSyntax("count value is too large")
Expand All @@ -116,19 +116,19 @@ func parseRepeatCount(s *State, countRunes []rune) (int64, error) {
// I don't know how to produce this by high-level Generate test
s.errorOffset -= int64(len(maxStr)) + 2
s.errorMarkLen = len(minStr)
return 0, s.errorSyntax("invalid natural number '%v'", minStr)
return 0, s.errorSyntax(s_invalid_natural_num, minStr)
}
if minCount < 1 {
s.errorOffset -= int64(len(maxStr)) + 2
s.errorMarkLen = len(minStr)
return 0, s.errorSyntax("invalid natural number '%v'", minStr)
return 0, s.errorSyntax(s_invalid_natural_num, minStr)
}
maxCount, err := strconv.ParseInt(maxStr, 10, 64)
if err != nil {
// I don't know how to produce this by high-level Generate test
s.errorOffset -= 2
s.errorMarkLen = len(maxStr)
return 0, s.errorSyntax("invalid natural number '%v'", maxStr)
return 0, s.errorSyntax(s_invalid_natural_num, maxStr)
}
if maxCount < minCount {
s.errorOffset--
Expand Down
2 changes: 1 addition & 1 deletion lib/rootgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ func (g *RootGenerator) Entropy(s *State) (float64, error) {
if g.entropy != nil {
return *g.entropy, nil
}
return 0, s.errorUnknown("entropy is not calculated")
return 0, s.errorUnknown(s_entropy_not_calc)
}
2 changes: 1 addition & 1 deletion lib/shuffle.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (g *shuffleGenerator) Entropy(s *State) (float64, error) {
if g.argPatternEntropy != nil {
return *g.argPatternEntropy, nil
}
return 0, s.errorUnknown("entropy is not calculated")
return 0, s.errorUnknown(s_entropy_not_calc)
}

func newShuffleGenerator(arg []rune) (*shuffleGenerator, error) {
Expand Down
8 changes: 8 additions & 0 deletions lib/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package passgen

const (
s_entropy_not_calc = "entropy is not calculated"
s_invalid_hex_num = "invalid hex number %#v"
s_invalid_natural_num = "invalid natural number '%v'"
s_func_call_expected = "expected a function call"
)

0 comments on commit b1057f4

Please sign in to comment.