Skip to content

Documentation Comments

jamster edited this page Nov 22, 2026 · 1 revision

Documentation Comments

Documentation comments are required for all Rust and TypeScript code. They are not enforced in Svelte or HTML files. Comment where it helps, but there is no mandatory format there.

Rust: File Headers

Every .rs file except mod.rs must begin with a //! doc comment. The first line is a short one-line description. Leave a blank //! on the second line, then expand from the third line onward.

//! Auth middleware: validate and extract JWT claims from incoming requests.
//!
//! This module provides an Axum extractor that reads the `Authorization` header,
//! verifies the JWT signature against the configured secret, and rejects requests
//! with expired or malformed tokens before they reach route handlers.

Rust: Functions

Every function needs a /// doc comment. Public functions need every applicable section. Private functions follow the same structure but omit # Example.

/// Verify a JWT token string and return the decoded claims.
///
/// Decodes and validates the token signature, expiry, and issuer claim.
/// Returns the inner `Claims` struct on success so callers can access
/// the user ID and granted scopes without re-parsing the token.
///
/// # Errors
///
/// Returns an `Err` when:
/// - the token is malformed or cannot be decoded,
/// - the signature does not match the configured secret,
/// - the token has expired,
/// - or required claims (`sub`, `exp`) are missing.
///
/// # Panics
///
/// Does not panic under normal operation. Will panic if the JWT secret
/// has not been loaded into the application state (misconfiguration).
///
/// # Arguments
/// * `token` - The raw JWT string, typically extracted from the `Authorization` header.
/// * `secret` - The HMAC secret used to verify the token signature.
///
/// # Return
/// Returns `Claims` on success containing the user ID and token metadata.
/// Returns an error if validation fails for any reason.
///
/// # Example
/// ```rust
/// let claims = verify_token(&token, &secret)?;
/// println!("Authenticated user: {}", claims.sub);
/// ```
pub fn verify_token(token: &str, secret: &[u8]) -> Result<Claims, AuthError> {
    // ...
}
/// Strip the `Bearer ` prefix from a raw Authorization header value.
///
/// Returns the token string slice without the prefix, or an error
/// if the header value does not start with `Bearer `.
///
/// # Errors
///
/// Returns an `Err` if the header value is missing the `Bearer ` prefix
/// or is otherwise malformed.
///
/// # Arguments
/// * `header` - The raw value of the `Authorization` header.
///
/// # Return
/// Returns a `&str` slice pointing to the token portion of the header.
fn extract_bearer(header: &str) -> Result<&str, AuthError> {
    // ...
}

Rust sections

Section pub fn private fn
One-line summary always always
Extended description where helpful where helpful
# Errors if returns Result if returns Result
# Panics if can panic if can panic
# Arguments always always
# Return always always
# Example always omit

TypeScript: File Headers

Every .ts file must begin with a /** doc comment. The first line is a short one-line description. Leave a blank line, then expand with more detail.

/**
 * Auth token management and validation.
 *
 * Provides utilities for storing, retrieving, and validating JWT tokens.
 * Handles token refresh logic, expiry checks with a 5-minute buffer, and
 * secure storage via the Tauri Store plugin.
 */

Svelte component files (.svelte) don't need file headers unless they export utility functions.

TypeScript: Functions

Every exported function needs a /** doc comment with all applicable sections. Use the imperative in the one-liner ("Get", "Validate", "Retrieve").

/**
 * Retrieve the currently stored access token.
 *
 * Returns null if no token is stored or if the store operation fails.
 * Does not validate expiry; use `isTokenValid()` to ensure the token is still active.
 *
 * @returns The access token string, or null if not found.
 * @throws {Error} If the store operation fails.
 *
 * @example
 * ```typescript
 * const token = await getAccessToken();
 * if (token) {
 *   console.log('Token exists');
 * }
 * ```
 */
export async function getAccessToken(): Promise<string | null> {
  // ...
}
/**
 * Decode a JWT token string without verification.
 *
 * Extracts and parses the payload segment of a JWT, returning the decoded
 * claims object. This function does not validate the signature or expiry.
 * Use `isTokenValid()` for security-critical checks.
 *
 * @param token - The raw JWT string.
 * @returns The decoded claims object, or null if the token is malformed.
 * @throws {Error} If the payload is not valid JSON.
 */
function decodeToken(token: string): Record<string, unknown> | null {
  // ...
}

TypeScript sections

Section export fn private fn
One-line summary always always
Extended description always always
@param when the function takes parameters when the function takes parameters
@returns always always
@throws when applicable when applicable
@example always omit

Clone this wiki locally