github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/linter/report/failure.go (about)

     1  package report
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"github.com/yoheimuta/go-protoparser/v4/parser/meta"
     9  )
    10  
    11  const errorLevel = "error"
    12  
    13  // Failure represents a lint error information.
    14  type Failure struct {
    15  	pos      meta.Position
    16  	message  string
    17  	ruleID   string
    18  	severity string
    19  }
    20  
    21  // Failuref creates a new Failure and the formatting works like fmt.Sprintf.
    22  func Failuref(
    23  	pos meta.Position,
    24  	ruleID string,
    25  	format string,
    26  	a ...interface{},
    27  ) Failure {
    28  	return FailureWithSeverityf(
    29  		pos,
    30  		ruleID,
    31  		errorLevel,
    32  		format,
    33  		a...,
    34  	)
    35  }
    36  
    37  // FailureWithSeverityf creates a new Failure accepting a severity level description and the formatting works like fmt.Sprintf.
    38  func FailureWithSeverityf(
    39  	pos meta.Position,
    40  	ruleID string,
    41  	severity string,
    42  	format string,
    43  	a ...interface{},
    44  ) Failure {
    45  	return Failure{
    46  		pos:      pos,
    47  		message:  fmt.Sprintf(format, a...),
    48  		ruleID:   ruleID,
    49  		severity: severity,
    50  	}
    51  }
    52  
    53  // String stringifies Failure.
    54  func (f Failure) String() string {
    55  	return fmt.Sprintf("[%s] %s", f.pos, f.message)
    56  }
    57  
    58  // Message returns a raw message.
    59  func (f Failure) Message() string {
    60  	return f.message
    61  }
    62  
    63  // Pos returns a raw position.
    64  func (f Failure) Pos() meta.Position {
    65  	return f.pos
    66  }
    67  
    68  // RuleID returns a rule ID.
    69  func (f Failure) RuleID() string {
    70  	return f.ruleID
    71  }
    72  
    73  // Severity represents the severity of a severity
    74  func (f Failure) Severity() string {
    75  	return f.severity
    76  }
    77  
    78  // FilenameWithoutExt returns a filename without the extension.
    79  func (f Failure) FilenameWithoutExt() string {
    80  	name := f.pos.Filename
    81  	return strings.TrimSuffix(name, filepath.Ext(name))
    82  }