mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 20:52:33 +02:00
40 lines
691 B
Go
40 lines
691 B
Go
package fields
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
E "github.com/yusing/go-proxy/internal/error"
|
|
)
|
|
|
|
type Port int
|
|
|
|
func ValidatePort[String ~string](v String) (Port, E.NestedError) {
|
|
p, err := strconv.Atoi(string(v))
|
|
if err != nil {
|
|
return ErrPort, E.Invalid("port number", v).With(err)
|
|
}
|
|
return ValidatePortInt(p)
|
|
}
|
|
|
|
func ValidatePortInt[Int int | uint16](v Int) (Port, E.NestedError) {
|
|
p := Port(v)
|
|
if !p.inBound() {
|
|
return ErrPort, E.OutOfRange("port", p)
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
func (p Port) inBound() bool {
|
|
return p >= MinPort && p <= MaxPort
|
|
}
|
|
|
|
func (p Port) String() string {
|
|
return strconv.Itoa(int(p))
|
|
}
|
|
|
|
const (
|
|
MinPort = 0
|
|
MaxPort = 65535
|
|
ErrPort = Port(-1)
|
|
NoPort = Port(0)
|
|
)
|