from pathlib import Path
from typing import Dict
from typing import Optional
import toml


def read_pyproject_toml(working_dir: Path) -> Dict[str, Optional[str]]:
    """
    Read project's `pyproject.toml` file

    Args:
        working_dir: CWD. Usually a root of source code

    Returns:
        Configurations described in toml file

    Raises:
        toml.TomlDecodeError: Failed to decode
        OSError: Failed to read a file

    """
    pyproject_toml_path: Path = working_dir / "pyproject.toml"
    if not pyproject_toml_path.is_file():
        return dict()

    pyproject_toml = toml.load(pyproject_toml_path)
    config_d = pyproject_toml.get("tool", {}).get("pytestdocgen", {})
    config = dict()

    for k in config_d:
        config[f"--{str(k)}"] = config_d[k]

    return config