Files
rsser/configuration/nntp.go
T
2026-08-05 12:06:46 +05:00

45 lines
1.2 KiB
Go

package configuration
import (
"errors"
"fmt"
"net"
)
var (
errNNTPAddressIsEmpty = errors.New("address is empty")
errNNTPInvalidAddress = errors.New("address is invalid")
errNNTPInvalidConnTimeout = errors.New("connect timeout is invalid")
errNNTPUserOrPassIsEmpty = errors.New("username or password is empty")
)
// NNTP is an NNTP output configuration.
type NNTP struct {
Address string `json:"address"`
Username string `json:"username"`
Password string `json:"password"`
ConnectTimeout int64 `json:"connect_timeout"`
}
// Validate validates configuration.
func (n *NNTP) Validate() error {
if n.Address == "" {
return fmt.Errorf("validate NNTP output configuration: %w", errNNTPAddressIsEmpty)
}
_, _, err := net.SplitHostPort(n.Address)
if err != nil {
return fmt.Errorf("validate NNTP output configuration: %w: %w", errNNTPInvalidAddress, err)
}
if (n.Username != "" && n.Password == "") || (n.Username == "" && n.Password != "") {
return fmt.Errorf("validate NNTP output configuration: %w", errNNTPUserOrPassIsEmpty)
}
if n.ConnectTimeout < 1 {
return fmt.Errorf("validate NNTP output configuration: %w", errNNTPInvalidConnTimeout)
}
return nil
}