#!/usr/bin/env python3
import sys
import json
import re

data = json.load(sys.stdin)

if data.get("tool_name") != "Bash":
    sys.exit(0)

cmd = data.get("tool_input", {}).get("command", "")
if not re.search(r"\bcargo\s+(check|build|test|clippy|run)\b", cmd):
    sys.exit(0)

output = data.get("tool_response", {}).get("output", "")
if not output:
    sys.exit(0)

blocks = re.split(r"\n{2,}", output)
warning_blocks = [b for b in blocks if re.match(r"\s*warning:", b)]
other_blocks = [b for b in blocks if not re.match(r"\s*warning:", b)]

if not warning_blocks:
    sys.exit(0)

filtered = "\n\n".join(other_blocks).rstrip("\n")
filtered += f"\n\n[{len(warning_blocks)} warning(s) filtered]\n"

print(json.dumps({"type": "result", "content": filtered}))
