import pathlib
import json


def validate_vscode_conf_file(
        vscode_conf_file: pathlib.Path,
        vscode_conf_entry: str) -> dict:
    """Validate c_cpp_properties.json an return it as a dict."""
    with vscode_conf_file.open(mode='r') as file:
        try:
            vscode_conf = json.load(file)
        except json.JSONDecodeError:
            raise Exception(
                f'Invalid json file: {vscode_conf_file}')
    n = len(list(filter(
        lambda entry: entry['name'] == vscode_conf_entry,
        vscode_conf['configurations'])))
    if n < 1:  # No "Mbed" entry
        raise Exception(
            f'Could not find \"{vscode_conf_entry}\" config entry in your c_cpp_properties.json ({vscode_conf_file}).')
    elif n > 1:  # Prohibit more than two "Mbed" entries
        raise Exception(
            f'More than two \"{vscode_conf_entry}\" config entries found in <{vscode_conf_file}>. '
            f'Leave one \"{vscode_conf_entry}\" entry and remove the others.')
    return vscode_conf