Create llms.txt from Rust Source Code

2026-08-30 浏览 (1)


description: Generate llms.txt from local Rust source code argument-hint: "[source_path] [output_path]"

Create llms.txt from Rust Source Code

Generate comprehensive llms.txt documentation from local Rust project source code.

Arguments: $ARGUMENTS

  • First argument: source_path (optional) - Rust project path, defaults to current directory
  • Second argument: output_path (optional) - output path, defaults to ~/tmp/{timestamp}-{crate}-llms.txt

Tool Priority

  1. rustdoc JSON (preferred) - Most complete API extraction
  2. Source code parsing (fallback) - If rustdoc unavailable

Instructions

1. Validate Project

# Check if Cargo.toml exists
if [ ! -f "${source_path}/Cargo.toml" ]; then
    echo "Error: No Cargo.toml found at ${source_path}"
    exit 1
fi

2. Read Project Metadata

Extract from Cargo.toml:

  • name - crate name
  • version - crate version
  • description - crate description
  • [features] - feature flags
  • [dependencies] - dependencies list
# Parse Cargo.toml
grep -E "^name|^version|^description" Cargo.toml

3. Check for Workspace

# Detect workspace
if grep -q "\[workspace\]" Cargo.toml; then
    # Parse members
    grep -A 20 "^\[workspace\]" Cargo.toml | grep -E "members\s*="
    # Process each member separately
fi

Workspace handling:

  • If [workspace] section exists, identify all members
  • Generate llms.txt for each member crate
  • Or combine into single llms.txt with sections per crate

4. Extract API Documentation

Method A: rustdoc JSON (Preferred)

# Generate JSON documentation
cargo +nightly rustdoc -- -Z unstable-options --output-format json 2>/dev/null

# Output location
ls target/doc/*.json

rustdoc JSON contains:

  • Complete module hierarchy
  • All pub items with documentation
  • Type signatures and generics
  • Code examples from doc comments
  • Feature flag requirements

Parse JSON for:

.index[*] | select(.visibility == "public") | {
  name: .name,
  kind: .kind,
  docs: .docs,
  sig: .inner.decl
}

Method B: Source Code Parsing (Fallback)

If rustdoc fails (no nightly, compilation errors):

# Extract crate-level documentation
grep "^//!" src/lib.rs | sed 's/^\/\/! //'

# Extract module documentation
find src -name "*.rs" -exec grep -l "^//!" {} \;

# Extract pub items with doc comments
grep -B 10 "^pub " src/**/*.rs | grep -E "///|^pub "

# Extract pub item signatures
grep -E "^pub (fn|struct|enum|trait|type|mod|const|static)" src/**/*.rs

Extraction targets:

PatternCaptures
//!Module-level docs
///Item-level docs
pub fnPublic functions
pub structPublic structs
pub enumPublic enums
pub traitPublic traits
pub typeType aliases
pub modPublic modules

5. Read README.md

if [ -f "${source_path}/README.md" ]; then
    # Extract overview section (first 100 lines or until ## section)
    head -100 README.md
fi

6. Extract Feature Flags

# From Cargo.toml [features] section
grep -A 50 "^\[features\]" Cargo.toml | grep -B 50 "^\[" | head -50

7. Generate llms.txt

Consolidate all extracted content into this format:

# {CrateName}

> {Description from Cargo.toml}

**Version:** {version} | **Source:** local

---

## Overview

{Content from README.md or crate-level //! docs}

## Modules

### {module_name}

{Module documentation from //!}

#### Key Types

| Type | Description |
|------|-------------|
| `StructName` | From /// docs |
| `EnumName` | From /// docs |

#### Key Functions

```rust
/// Function documentation
pub fn function_name(param: Type) -> ReturnType
```

## Code Examples

```rust
// Examples extracted from /// docs or README
```

---

## Feature Flags

| Feature | Dependencies | Description |
|---------|--------------|-------------|
| `feature_name` | dep1, dep2 | From Cargo.toml comments |

---

## Dependencies

| Crate | Version | Features |
|-------|---------|----------|
| `dep_name` | 1.0 | feature1, feature2 |

---

## Source Structure

```
src/
├── lib.rs          - Main library entry
├── module1/
│   ├── mod.rs      - Module docs
│   └── types.rs    - Type definitions
└── module2.rs      - Single-file module
```

8. Save Output

# Generate timestamp
timestamp=$(date +%Y%m%d%H%M)

# Get crate name
crate_name=$(grep "^name" Cargo.toml | head -1 | cut -d'"' -f2)

# Output path
output="${output_path:-$HOME/tmp/${timestamp}-${crate_name}-llms.txt}"

# Ensure directory exists
mkdir -p "$(dirname "$output")"

# Write file
echo "Output saved to: $output"

Fallback Strategy

1. Try rustdoc JSON
   ↓ (if failed)
2. Use source code parsing
   ↓ (always)
3. Supplement with Cargo.toml + README.md

Automatic fallback triggers:

  • No nightly toolchain installed
  • Project has compilation errors
  • Missing dependencies
  • Build script failures

When falling back, inform user:

rustdoc JSON generation failed, using source code parsing.
Some type information may be incomplete.

Quality Requirements

  • All pub items documented
  • Module hierarchy preserved
  • Code examples included
  • Feature flags documented
  • Dependencies listed
  • Source structure shown
  • Consistent markdown formatting
  • Version information accurate

Workspace Handling

For workspaces with multiple crates:

Option 1: Combined llms.txt

~/tmp/{timestamp}-{workspace}-llms.txt

Contains sections for each member crate.

Option 2: Separate files

~/tmp/{timestamp}-{crate1}-llms.txt
~/tmp/{timestamp}-{crate2}-llms.txt

Ask user which approach they prefer for workspaces.


Workflow Integration

This command integrates with the Skills creation workflow:

Local Rust Source
        ↓
/create-llms-from-source {path}
        ↓
~/tmp/{timestamp}-{crate}-llms.txt
        ↓
/create-skills-via-llms {crate} {llms_path}
        ↓
~/.claude/skills/{crate}-*/

Or via sync-crate-skills:

/sync-crate-skills --from-source {path}

Example Usage

# Generate llms.txt for current directory
/create-llms-from-source

# Generate for specific project
/create-llms-from-source /path/to/my-rust-project

# Specify output path
/create-llms-from-source /path/to/project ~/docs/my-crate-llms.txt

# For workspace project
/create-llms-from-source /path/to/workspace

Limitations

  • Private items (pub(crate), pub(super)) are excluded
  • Macro-generated code may not be fully captured in source parsing mode
  • Generic constraints shown as-is without resolution
  • Inline documentation preferred over external doc files

你可能感兴趣的文章

/crate-info

/docs Command

/skill-index

Create llms.txt from URLs

/rust-features

Achievement System

Update Crate Skill

/unsafe-check

AI Daily Report

Sync Crate Skills

  • 所属分类: AI
  • 本文标签: rust
  • 版权声明: 本文链接 https://seaxiang.com/blog/AWR7UcCd