errors.go
author Mikael Berthe <mikael@lilotux.net>
Sat, 07 Apr 2018 23:17:21 +0200
changeset 20 1110dfef8964
parent 10 8dc05ff5dbe2
permissions -rw-r--r--
Update README
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
8dc05ff5dbe2 Add (MIT) license
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
     1
// Copyright (C) 2016 Mikael Berthe <mikael@lilotux.net>. All rights reserved.
8dc05ff5dbe2 Add (MIT) license
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
     2
// Use of this source code is governed by the MIT license,
8dc05ff5dbe2 Add (MIT) license
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
     3
// which can be found in the LICENSE file.
8dc05ff5dbe2 Add (MIT) license
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
     4
9
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
package takuzu
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
import "fmt"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
// This file contains the takuzu validation error type.
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
const (
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
	ErrorNil = iota
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
	ErrorDuplicate
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
	ErrorTooManyValues
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	ErrorTooManyAdjacentValues
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
)
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
type validationError struct {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
	ErrorType    int
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
	LineNumber   *int
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
	ColumnNumber *int
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
	CellValue    *int
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
}
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
func (e validationError) Error() string {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
	var axis string
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
	var n int
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	// 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
    30
	// 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
    31
	if e.LineNumber != nil {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
		axis = "line"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
		n = *e.LineNumber
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
	} else if e.ColumnNumber != nil {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
		axis = "column"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
		n = *e.ColumnNumber
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
	}
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
	switch e.ErrorType {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
	case ErrorNil:
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
		return ""
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
	case ErrorDuplicate:
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
		if axis == "" {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
			return "internal validation error"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
		}
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
		return fmt.Sprintf("duplicate %ss (%d)", axis, n)
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
	case ErrorTooManyValues:
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
		if axis == "" || e.CellValue == nil {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
			return "internal validation error"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
		}
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
		var numberStr string
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
		if *e.CellValue == 0 {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
			numberStr = "zeroes"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
		} else if *e.CellValue == 1 {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
			numberStr = "ones"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
		} else {
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
			return "internal validation error"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
		}
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
		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
    60
	case ErrorTooManyAdjacentValues:
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
		if axis == "" || e.CellValue == nil {
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
		}
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
		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
    65
	}
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
	return "internal validation error"
4b3436c03726 Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
}