errors.go
author Mikael Berthe <mikael@lilotux.net>
Sat, 07 Apr 2018 22:49:51 +0200
changeset 15 eac7d78ff641
parent 10 8dc05ff5dbe2
permissions -rw-r--r--
Update gotak import path to fix Travis build Update import path in the gotak subdir so that Travis tests can be played against the Github repository. Also, add missing dependencies to the Travis YAML file.

// 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"
}