File size: 9,160 Bytes
9425aed | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | # Rust API Documentation Guide
**Project:** QATAAUM Quantum Assembly Runtime
**Version:** 1.0
**Date:** 2026-07-22
## Generating Documentation
QATAAUM uses Rust's built-in documentation system (`rustdoc`) to generate comprehensive API documentation from source code comments.
### Quick Start
```powershell
# Generate documentation for all workspace crates
cargo doc --no-deps --workspace
# Generate and open in browser
cargo doc --no-deps --workspace --open
# Generate with private items
cargo doc --no-deps --workspace --document-private-items
```
### Documentation Location
Generated documentation is placed in:
```
target/doc/
βββ qataaum_parser/
βββ qataaum_semantic/
βββ qataaum_ir/
βββ qataaum_passes/
βββ qataaum_routing/
βββ qataaum_simulator/
βββ shadow_rpg_q/
βββ qataaum_ibmi_ffi/
βββ index.html
```
## Documentation Standards
### Module-Level Documentation
Every module should have a module-level doc comment:
```rust
//! # Parser Module
//!
//! This module provides lexical analysis and parsing for OpenQASM 2.0, OpenQASM 3.x,
//! and MetaQASM-4 quantum assembly languages.
//!
//! ## Example
//!
//! ```
//! use qataaum_parser::{Lexer, Parser};
//!
//! let source = "OPENQASM 2.0; qreg q[2]; h q[0];";
//! let mut lexer = Lexer::new(source);
//! let tokens = lexer.tokenize()?;
//! let mut parser = Parser::new(tokens);
//! let ast = parser.parse()?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
```
### Type Documentation
All public types should be documented:
```rust
/// A lexer for OpenQASM source code.
///
/// The lexer tokenizes source code into a stream of tokens that can be
/// consumed by the parser. It handles:
/// - Keywords and identifiers
/// - Numeric literals (integers and floats)
/// - String literals
/// - Operators and punctuation
/// - Comments (which are discarded)
///
/// # Examples
///
/// ```
/// use qataaum_parser::Lexer;
///
/// let mut lexer = Lexer::new("OPENQASM 2.0;");
/// let tokens = lexer.tokenize()?;
/// assert_eq!(tokens.len(), 3); // OPENQASM, 2.0, ;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct Lexer<'a> {
source: &'a str,
position: usize,
}
```
### Function Documentation
All public functions should be documented:
```rust
/// Tokenize the entire source code.
///
/// This method consumes the lexer and returns a vector of tokens.
/// Comments are discarded during tokenization.
///
/// # Errors
///
/// Returns `LexerError` if:
/// - An unexpected character is encountered
/// - A string literal is not terminated
/// - A numeric literal is malformed
///
/// # Examples
///
/// ```
/// use qataaum_parser::Lexer;
///
/// let mut lexer = Lexer::new("qreg q[2];");
/// let tokens = lexer.tokenize()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn tokenize(&mut self) -> Result<Vec<Token>, LexerError> {
// Implementation
}
```
### Error Documentation
Error types should document all variants:
```rust
/// Errors that can occur during lexical analysis.
#[derive(Debug, thiserror::Error)]
pub enum LexerError {
/// An unexpected character was encountered.
///
/// The first field is the character, the second is its position.
#[error("unexpected character '{0}' at position {1}")]
UnexpectedCharacter(char, usize),
/// A string literal was not terminated before EOF.
///
/// The field is the position where the string started.
#[error("unterminated string literal at position {0}")]
UnterminatedString(usize),
}
```
## Documentation Features
### Code Examples
Use triple backticks with language identifier:
```rust
/// # Examples
///
/// ```
/// use qataaum_simulator::StateVectorSimulator;
///
/// let mut sim = StateVectorSimulator::new(2);
/// sim.h(0)?;
/// sim.cx(0, 1)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
```
### Panics Section
Document when functions can panic:
```rust
/// # Panics
///
/// Panics if `qubit` is greater than or equal to the number of qubits.
pub fn apply_gate(&mut self, qubit: usize) {
assert!(qubit < self.num_qubits, "qubit index out of bounds");
}
```
### Safety Section
Document unsafe code:
```rust
/// # Safety
///
/// The caller must ensure that `ptr` is valid and points to a properly
/// initialized `QataaumCompiler` instance.
pub unsafe fn qataaum_compiler_free(ptr: *mut QataaumCompiler) {
if !ptr.is_null() {
drop(Box::from_raw(ptr));
}
}
```
### Links
Link to other items:
```rust
/// Parse tokens into an AST.
///
/// See [`Lexer::tokenize`] for token generation.
/// Returns a [`Program`] on success.
pub fn parse(&mut self) -> Result<Program, ParseError> {
// Implementation
}
```
## Crate-Level Documentation
Each crate should have a `lib.rs` with crate-level documentation:
```rust
//! # QATAAUM Parser
//!
//! This crate provides lexical analysis and parsing for quantum assembly languages.
//!
//! ## Supported Languages
//!
//! - OpenQASM 2.0
//! - OpenQASM 3.x (public revisions)
//! - MetaQASM-4 (experimental)
//!
//! ## Architecture
//!
//! The parser is organized into three main components:
//!
//! 1. **Lexer:** Tokenizes source code
//! 2. **Parser:** Builds abstract syntax trees
//! 3. **AST:** Represents program structure
//!
//! ## Example
//!
//! ```
//! use qataaum_parser::{Lexer, Parser};
//!
//! let source = "OPENQASM 2.0; qreg q[2]; h q[0]; cx q[0],q[1];";
//! let mut lexer = Lexer::new(source);
//! let tokens = lexer.tokenize()?;
//! let mut parser = Parser::new(tokens);
//! let ast = parser.parse()?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
```
## Testing Documentation
Documentation examples are automatically tested:
```rust
/// # Examples
///
/// ```
/// use qataaum_parser::Lexer;
///
/// let mut lexer = Lexer::new("OPENQASM 2.0;");
/// let tokens = lexer.tokenize().unwrap();
/// assert_eq!(tokens.len(), 3);
/// ```
```
Run documentation tests:
```powershell
cargo test --doc
```
## Hidden Documentation
Hide implementation details:
```rust
/// Public function
///
/// # Examples
///
/// ```
/// # use qataaum_parser::Lexer;
/// # let mut lexer = Lexer::new("test");
/// // Hidden setup code above
/// let tokens = lexer.tokenize()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
```
Lines starting with `#` are hidden in rendered docs but still compiled.
## Documentation Attributes
### `#[doc(hidden)]`
Hide items from documentation:
```rust
#[doc(hidden)]
pub fn internal_helper() {
// Not shown in public docs
}
```
### `#[doc(alias = "...")]`
Add search aliases:
```rust
#[doc(alias = "CNOT")]
#[doc(alias = "controlled-not")]
pub fn cx(&mut self, control: usize, target: usize) {
// Searchable by "CNOT" or "controlled-not"
}
```
## Cross-Crate Documentation
Link to items in other crates:
```rust
/// Uses [`qataaum_parser::Parser`] to parse source code.
pub fn compile(source: &str) -> Result<Program, CompileError> {
// Implementation
}
```
## Documentation Best Practices
1. **Be Concise:** First sentence should be a brief summary
2. **Use Examples:** Show typical usage patterns
3. **Document Errors:** Explain when and why errors occur
4. **Link Related Items:** Help users discover related functionality
5. **Test Examples:** Ensure examples compile and run
6. **Update Regularly:** Keep docs in sync with code changes
## Publishing Documentation
### Local Preview
```powershell
cargo doc --no-deps --workspace --open
```
### CI/CD Integration
Add to GitHub Actions:
```yaml
- name: Generate Documentation
run: cargo doc --no-deps --workspace
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./target/doc
```
### docs.rs
Documentation is automatically published to docs.rs when crates are published to crates.io.
## Troubleshooting
### Missing Documentation Warnings
Enable warnings:
```powershell
$env:RUSTDOCFLAGS="-D missing-docs"
cargo doc --no-deps --workspace
```
### Broken Links
Check for broken intra-doc links:
```powershell
$env:RUSTDOCFLAGS="-D rustdoc::broken-intra-doc-links"
cargo doc --no-deps --workspace
```
### Private Items
Include private items in documentation:
```powershell
cargo doc --no-deps --workspace --document-private-items
```
## Additional Resources
- [Rust Documentation Guidelines](https://doc.rust-lang.org/rustdoc/)
- [RFC 1574: API Documentation Conventions](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html)
- [The Rust Book: Documentation](https://doc.rust-lang.org/book/ch14-02-publishing-to-crates-io.html#making-useful-documentation-comments)
---
**Generated:** 2026-07-22
**Version:** 1.0
**License:** Apache-2.0 |