package httpsfv

import (
	
	
	
)

// ErrInvalidKeyFormat is returned when the format of a parameter or dictionary key is invalid.
var ErrInvalidKeyFormat = errors.New("invalid key format")

// isKeyChar checks if c is a valid key characters.
func isKeyChar( byte) bool {
	if isLowerCaseAlpha() || isDigit() {
		return true
	}

	switch  {
	case '_', '-', '.', '*':
		return true
	}

	return false
}

// checkKey checks if the given value is a valid parameter key according to
// https://httpwg.org/specs/rfc9651.html#param.
func checkKey( string) error {
	if len() == 0 {
		return fmt.Errorf("a key cannot be empty: %w", ErrInvalidKeyFormat)
	}

	if !isLowerCaseAlpha([0]) && [0] != '*' {
		return fmt.Errorf("a key must start with a lower case alpha character or *: %w", ErrInvalidKeyFormat)
	}

	for  := 1;  < len(); ++ {
		if !isKeyChar([]) {
			return fmt.Errorf("the character %c isn't allowed in a key: %w", [], ErrInvalidKeyFormat)
		}
	}

	return nil
}

// marshalKey serializes as defined in
// https://httpwg.org/specs/rfc9651.html#ser-key.
func marshalKey( io.StringWriter,  string) error {
	if  := checkKey();  != nil {
		return 
	}

	,  := .WriteString()

	return 
}

// parseKey parses as defined in
// https://httpwg.org/specs/rfc9651.html#parse-key.
func parseKey( *scanner) (string, error) {
	if .eof() {
		return "", &UnmarshalError{.off, ErrInvalidKeyFormat}
	}

	 := .data[.off]
	if !isLowerCaseAlpha() &&  != '*' {
		return "", &UnmarshalError{.off, ErrInvalidKeyFormat}
	}

	 := .off
	.off++

	for !.eof() {
		if !isKeyChar(.data[.off]) {
			break
		}
		.off++
	}

	return .data[:.off], nil
}