diff --git a/pyproject.toml b/pyproject.toml index e81eba1..c2ba8c8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,6 +35,9 @@ robotvibecoder = "robotvibecoder.main:main" [tool.hatch.version] source = "versioningit" +[tool.hatch.metadata] +allow-direct-references = true + [tool.hatch.envs.types] extra-dependencies = ["mypy>=1.0.0"] [tool.hatch.envs.types.scripts] diff --git a/src/robotvibecoder/config.py b/src/robotvibecoder/config.py index 13e1f06..61bf555 100644 --- a/src/robotvibecoder/config.py +++ b/src/robotvibecoder/config.py @@ -9,6 +9,7 @@ from enum import Enum import json import sys +from typing import Union from robotvibecoder.cli import print_err @@ -24,6 +25,22 @@ class MechanismKind(str, Enum): ELEVATOR = "Elevator" FLYWHEEL = "Flywheel" + @staticmethod + def try_into(value: str) -> Union["MechanismKind", None]: + """Try to parse a string into a MechanismKind. + Returns none if the string isn't a valid MechanismKind. + + :param value: The string to convert + :type value: str + :return: A MechanismKind if value is a valid Mechanism Kind, otherwise none + :rtype: MechanismKind | None + """ + for kind in MechanismKind: + if value == kind: + return kind + + return None + @dataclass class MechanismConfig: diff --git a/src/robotvibecoder/subcommands/new.py b/src/robotvibecoder/subcommands/new.py index d233482..f3b25d5 100644 --- a/src/robotvibecoder/subcommands/new.py +++ b/src/robotvibecoder/subcommands/new.py @@ -29,11 +29,14 @@ def new_config_interactive() -> MechanismConfig: print("Kind: Should be either 'Elevator', 'Arm', or 'Flywheel'") kind: str = "" - while kind not in MechanismKind: + while MechanismKind.try_into(kind) is None: kind = input("> ") - if kind not in MechanismKind: + if MechanismKind.try_into(kind) is None: print("Please input either Elevator, Arm, or Flywheel.") - kind_enum: MechanismKind = MechanismKind[kind] + kind_try = MechanismKind.try_into(kind) + kind_enum: MechanismKind = ( + kind_try if kind_try is not None else MechanismKind.ELEVATOR + ) print("CAN Bus: whatever the name of the mechanism's bus is (e.g. `canivore`)") canbus: str = input("> ")