from __future__ import annotations

from pathlib import Path
from typing import Any

from .util import load_json, write_json


class RunCache:
    def __init__(self, root: Path) -> None:
        self.root = root

    def path(self, key: str) -> Path:
        return self.root / f"{key}.json"

    def load(self, key: str) -> dict[str, Any] | None:
        path = self.path(key)
        return load_json(path) if path.is_file() else None

    def store(self, key: str, value: dict[str, Any]) -> None:
        write_json(self.path(key), value)

