-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
67 lines (59 loc) · 1.51 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (C) 2016 Mikael Berthe <mikael@lilotux.net>. All rights reserved.
// Use of this source code is governed by the MIT license,
// which can be found in the LICENSE file.
package takuzu
import "fmt"
// This file contains the takuzu validation error type.
const (
ErrorNil = iota
ErrorDuplicate
ErrorTooManyValues
ErrorTooManyAdjacentValues
)
type validationError struct {
ErrorType int
LineNumber *int
ColumnNumber *int
CellValue *int
}
func (e validationError) Error() string {
var axis string
var n int
// Currently we don't have validation errors with both
// line and column so we can get the axis:
if e.LineNumber != nil {
axis = "line"
n = *e.LineNumber
} else if e.ColumnNumber != nil {
axis = "column"
n = *e.ColumnNumber
}
switch e.ErrorType {
case ErrorNil:
return ""
case ErrorDuplicate:
if axis == "" {
return "internal validation error"
}
return fmt.Sprintf("duplicate %ss (%d)", axis, n)
case ErrorTooManyValues:
if axis == "" || e.CellValue == nil {
return "internal validation error"
}
var numberStr string
if *e.CellValue == 0 {
numberStr = "zeroes"
} else if *e.CellValue == 1 {
numberStr = "ones"
} else {
return "internal validation error"
}
return fmt.Sprintf("%s %d: too many %s", axis, n, numberStr)
case ErrorTooManyAdjacentValues:
if axis == "" || e.CellValue == nil {
return "internal validation error"
}
return fmt.Sprintf("%s %d: 3+ same values %d", axis, n, *e.CellValue)
}
return "internal validation error"
}