errors.go
author Mikael Berthe <mikael@lilotux.net>
Sun, 16 Oct 2016 11:52:01 +0200
changeset 9 4b3436c03726
child 10 8dc05ff5dbe2
permissions -rw-r--r--
Switch completely to the new validation "ErrorType" Stay backward-compatible with previous error messages.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
package takuzu
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
import "fmt"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
// This file contains the takuzu validation error type.
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
const (
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
	ErrorNil = iota
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
	ErrorDuplicate
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
	ErrorTooManyValues
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
	ErrorTooManyAdjacentValues
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
)
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
type validationError struct {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	ErrorType    int
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
	LineNumber   *int
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
	ColumnNumber *int
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
	CellValue    *int
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
}
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
func (e validationError) Error() string {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
	var axis string
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
	var n int
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
	// Currently we don't have validation errors with both
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
	// line and column so we can get the axis:
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
	if e.LineNumber != nil {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
		axis = "line"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
		n = *e.LineNumber
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
	} else if e.ColumnNumber != nil {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
		axis = "column"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
		n = *e.ColumnNumber
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
	}
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
	switch e.ErrorType {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
	case ErrorNil:
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
		return ""
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
	case ErrorDuplicate:
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
		if axis == "" {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
			return "internal validation error"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
		}
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
		return fmt.Sprintf("duplicate %ss (%d)", axis, n)
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
	case ErrorTooManyValues:
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
		if axis == "" || e.CellValue == nil {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
			return "internal validation error"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
		}
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
		var numberStr string
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
		if *e.CellValue == 0 {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
			numberStr = "zeroes"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
		} else if *e.CellValue == 1 {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
			numberStr = "ones"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
		} else {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
			return "internal validation error"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
		}
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
		return fmt.Sprintf("%s %d: too many %s", axis, n, numberStr)
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
	case ErrorTooManyAdjacentValues:
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
		if axis == "" || e.CellValue == nil {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
			return "internal validation error"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
		}
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
		return fmt.Sprintf("%s %d: 3+ same values %d", axis, n, *e.CellValue)
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
	}
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
	return "internal validation error"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
}