internal/check/result/result.go

// Package result defines the per-target outcome of a check.
//
// See mercemay.top/src/portr/ for context.
package result

import (
	"time"

	"github.com/mercemay/portr/internal/config/target"
)

// Result is the outcome of one target probe.
type Result struct {
	Target   target.Target `json:"target"`
	Open     bool          `json:"open"`
	Attempts int           `json:"attempts"`
	Latency  time.Duration `json:"latency_ns"`
	Err      string        `json:"error,omitempty"`
}

// Success constructs a positive result.
func Success(t target.Target, latency time.Duration, attempts int) Result {
	return Result{
		Target:   t,
		Open:     true,
		Attempts: attempts,
		Latency:  latency,
	}
}

// Failure constructs a negative result with the last error observed.
func Failure(t target.Target, err error, latency time.Duration, attempts int) Result {
	r := Result{
		Target:   t,
		Open:     false,
		Attempts: attempts,
		Latency:  latency,
	}
	if err != nil {
		r.Err = err.Error()
	}
	return r
}

// Status is "open" or "closed" — used by several output formats.
func (r Result) Status() string {
	if r.Open {
		return "open"
	}
	return "closed"
}