"""Protocol drivers. Each driver turns a raw analyzer message into a normalised
ParsedMessage. Add a new analyzer protocol by adding a module here and
registering it in DRIVERS.

HL7 drivers (dymind_hl7) are modules with parse()/build_ack(); ASTM drivers are
class instances exposing parse()/build_order(). Both are looked up the same way;
the transport layer (TCP vs serial) is chosen by the device connection, and the
ASTM drivers carry `protocol = "astm"` so the app routes them to the serial
ASTM exchange.
"""

from __future__ import annotations

from . import dymind_hl7
from .maglumi_astm import driver as maglumi_astm
from .mispa_astm import driver as mispa_astm

# driver name (used in config) → driver object
DRIVERS = {
    "dymind_hl7": dymind_hl7,
    "maglumi_astm": maglumi_astm,
    "mispa_astm": mispa_astm,
}


def get_driver(name: str):
    """Return the driver for a config name, or raise KeyError."""
    return DRIVERS[name]


def is_astm(driver: object) -> bool:
    return getattr(driver, "protocol", None) == "astm"
