agent-first-data 0.40.0

A naming convention that lets AI agents understand your data without being told what it means, plus the afdata CLI and a Rust library for reading Markdown structure and safely editing structured JSON, TOML, YAML, dotenv, and INI documents.
Documentation
#[cfg(all(feature = "stream-redirect", not(feature = "libc")))]
compile_error!("the `stream-redirect` feature must enable the `libc` feature");

#[cfg(all(unix, not(feature = "libc")))]
#[test]
fn capped_read_without_libc_keeps_follow_and_refuses_no_follow() {
    use agent_first_data::document::{DocumentFile, SymlinkPolicy};

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("config.json");
    std::fs::write(&path, r#"{"k": "v"}"#).unwrap();

    assert!(
        DocumentFile::open_capped_with_policy(&path, None, 1024, SymlinkPolicy::Follow).is_ok()
    );
    let error = DocumentFile::open_capped_with_policy(&path, None, 1024, SymlinkPolicy::NoFollow)
        .unwrap_err();
    assert_eq!(error.code(), "document_unsupported_operation");
    assert!(error.to_string().contains("Cargo feature `libc`"));
}

/// Windows needs no Cargo feature: `NoFollow` opens the final component itself
/// and refuses the handle when it is a reparse point, so a regular file still
/// opens and a symlink does not.
#[cfg(windows)]
#[test]
fn capped_read_on_windows_refuses_a_symlink_and_keeps_regular_files() {
    use agent_first_data::document::{DocumentFile, SymlinkPolicy};

    let dir = tempfile::tempdir().unwrap();
    let target = dir.path().join("target.json");
    let link = dir.path().join("link.json");
    std::fs::write(&target, r#"{"k": "v"}"#).unwrap();
    std::os::windows::fs::symlink_file(&target, &link).unwrap();

    for policy in [SymlinkPolicy::Follow, SymlinkPolicy::NoFollow] {
        assert!(
            DocumentFile::open_capped_with_policy(&target, None, 1024, policy).is_ok(),
            "{policy:?}"
        );
    }
    assert!(
        DocumentFile::open_capped_with_policy(&link, None, 1024, SymlinkPolicy::Follow).is_ok()
    );
    let error = DocumentFile::open_capped_with_policy(&link, None, 1024, SymlinkPolicy::NoFollow)
        .unwrap_err();
    assert_eq!(error.code(), "document_io_failed");
    assert!(error.to_string().contains("reparse point"), "{error}");
}

#[cfg(all(unix, feature = "libc"))]
#[test]
fn capped_read_with_libc_atomically_refuses_a_symlink() {
    use agent_first_data::document::{DocumentFile, SymlinkPolicy};

    let dir = tempfile::tempdir().unwrap();
    let target = dir.path().join("target.json");
    let link = dir.path().join("link.json");
    std::fs::write(&target, r#"{"k": "v"}"#).unwrap();
    std::os::unix::fs::symlink(&target, &link).unwrap();

    assert!(
        DocumentFile::open_capped_with_policy(&link, None, 1024, SymlinkPolicy::Follow).is_ok()
    );
    let error = DocumentFile::open_capped_with_policy(&link, None, 1024, SymlinkPolicy::NoFollow)
        .unwrap_err();
    assert_eq!(error.code(), "document_io_failed");
}