pikopiko-cli 0.1.0

Command-line tool and MCP server to check, inspect, render and play MML with pikopiko.
Documentation
//! MCP の resource にする取扱説明書の写し(`guide/`)が、正本の `docs/guide/` と同じ中身か。
//! 食い違ったら `scripts/release.sh sync` で写し直す。
//!
//! 写しはクレートの中にあり、正本の相対リンク(ほかのページ `](NN-…`・文法仕様 `](../…`)は写しの中では
//! 切れる。そのため写しでは公開リポジトリの URL に置き換えてある。置き換えの規則は
//! `scripts/release.sh` の `guide_copy` と同じにする(どちらかを変えたら両方を直す)。

use std::path::Path;

/// 正本の文書の置き場(公開リポジトリ)。
const DOCS_URL: &str = "https://github.com/lovelygenerator/pikopiko/blob/main/docs";

/// 正本の本文から写しの本文を作る(`release.sh` の `guide_copy` と同じ規則)。
fn guide_copy(src: &str) -> String {
    let src = src.replace("](../", &format!("]({DOCS_URL}/"));
    let mut out = String::with_capacity(src.len());
    let mut rest = src.as_str();
    while let Some(i) = rest.find("](") {
        out.push_str(&rest[..i + 2]);
        rest = &rest[i + 2..];
        let b = rest.as_bytes();
        if b.len() >= 3 && b[0].is_ascii_digit() && b[1].is_ascii_digit() && b[2] == b'-' {
            out.push_str(DOCS_URL);
            out.push_str("/guide/");
        }
    }
    out.push_str(rest);
    out
}

#[test]
fn guide_copies_match_the_docs() {
    let root = Path::new(env!("CARGO_MANIFEST_DIR"));
    for (doc, copy) in [("04-mml.md", "mml.md"), ("10-ai.md", "ai.md")] {
        let Ok(want) = std::fs::read_to_string(root.join("../../docs/guide").join(doc)) else {
            // crates.io から取ったパッケージにはリポジトリの文書がない
            return;
        };
        let have = std::fs::read_to_string(root.join("guide").join(copy)).unwrap();
        assert!(
            guide_copy(&want) == have,
            "docs/guide/{doc} と guide/{copy} が違う。`scripts/release.sh sync` で写し直す"
        );
    }
}

#[test]
fn copies_have_no_relative_links() {
    // 写しの中で切れる相対リンクが残っていない(ページ内の `#…` と URL だけ)
    let root = Path::new(env!("CARGO_MANIFEST_DIR"));
    for copy in ["mml.md", "ai.md"] {
        let text = std::fs::read_to_string(root.join("guide").join(copy)).unwrap();
        for (i, _) in text.match_indices("](") {
            let target = &text[i + 2..];
            assert!(
                target.starts_with("http") || target.starts_with('#'),
                "guide/{copy} に相対リンクが残っている: {}",
                &target[..target.len().min(40)]
            );
        }
    }
}