"""加载并校验 rules.yaml。"""

from __future__ import annotations
from functools import lru_cache
from pathlib import Path
import yaml

from fastapi_modules.fastapi_leaudit.govdoc_engine.dsl.schema import RuleSet


def _load_uncached(path: Path) -> RuleSet:
    with open(path, encoding="utf-8") as f:
        data = yaml.safe_load(f)
    return RuleSet.model_validate(data)


@lru_cache(maxsize=32)
def _load_cached(path_str: str, mtime: float) -> RuleSet:
    return _load_uncached(Path(path_str))


def load_rules(path: str | Path) -> RuleSet:
    path = Path(path)
    return _load_cached(str(path.resolve()), path.stat().st_mtime)
