"""Shared utilities for the Piggie SDK."""

from __future__ import annotations

import re

from piggie.exceptions import LabelError

_LABEL_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")


def validate_label(label: str) -> str:
    """Validate a Cypher label/type name against identifier rules.

    Raises :class:`~piggie.exceptions.LabelError` if the name is invalid.
    Returns the label unchanged if valid.
    """
    if not _LABEL_RE.match(label):
        raise LabelError(
            f"Invalid label/type name: {label!r}. "
            "Must match [A-Za-z_][A-Za-z0-9_]*"
        )
    return label
