"""Scalar parsing helpers for stage 010 identity metadata."""


def required_string(table: dict[str, object], key: str, context: str) -> str:
    """Return a required non-empty string field."""
    value = table.get(key)
    if not isinstance(value, str) or not value.strip():
        msg = f"{context} requires non-empty string field {key!r}"
        raise ValueError(msg)
    return value.strip()


def optional_string(table: dict[str, object], key: str) -> str | None:
    """Return an optional non-empty string field."""
    value = table.get(key)
    if value is None:
        return None
    if not isinstance(value, str):
        msg = f"Expected string field {key!r}"
        raise ValueError(msg)

    stripped = value.strip()
    return stripped or None
