import yaml
from typing import Union
import pathlib
import toml
import json


def load_data(fpath: Union[str, pathlib.Path]) -> dict:
    """Load dictionary data from a JSON, YAML, or TOML file.

    The file format will be determined from the extension of the file name.

    :param fpath:   The file path from which to load.

    :return:        The loaded data as a dictionary.
    """
    if not isinstance(fpath, pathlib.Path):
        fpath = pathlib.Path(fpath)

    suffix = fpath.suffix.lower()

    if suffix == ".toml":
        data = toml.loads(fpath.read_text())
    elif suffix == ".yaml":

        with fpath.open("r") as fobj:
            data = yaml.safe_load(fobj)
    else:
        data = json.loads(fpath.read_text())

    return data