from pynput.mouse import Button


def to_button(value):
    """Convert value to Button enum."""
    # pylint: disable=C0415

    if isinstance(value, Button):
        return value

    sanitized = str(value).lower().strip().replace(" ", "_")
    try:
        return Button[sanitized]
    except KeyError as err:
        raise ValueError(f"Unknown mouse button: {value}") from err