Crate kodegen_config

Crate kodegen_config 

Source
Expand description

§kodegen-config

Centralized configuration path resolution for KODEGEN.ᴀɪ

§Features

  • Cross-platform: Windows, macOS, Unix/Linux support via XDG Base Directory spec
  • Dual config support: Git-local (.kodegen/) and user-global (~/.config/kodegen/)
  • Per-file precedence: Config files resolved by checking local first, then user
  • Auto-initialization: Creates directory structures on first use
  • Rich error context: All operations return Result<T> with detailed error messages

§Error Handling Pattern

All path resolution functions return Result<PathBuf> for consistency:

Root directories:

Subdirectories (all under root):

File resolution:

This uniform Result pattern provides:

  1. Consistency - All similar operations use the same error handling pattern
  2. Rich error context - Errors explain what failed and where the system searched
  3. Flexible handling - Callers can propagate (?), unwrap, or convert to Option via .ok()

§Usage Examples

use kodegen_config::KodegenConfig;
use anyhow::Result;

fn my_function() -> Result<()> {
    // Propagate errors with ?
    let user_config = KodegenConfig::user_config_dir()?;
    let local_config = KodegenConfig::local_config_dir()?;
    let toolset = KodegenConfig::resolve_toolset("core")?;
     
    println!("User config: {}", user_config.display());
    println!("Local config: {}", local_config.display());
    println!("Toolset: {}", toolset.display());
    Ok(())
}

§Handling Missing Files as Non-Errors

use kodegen_config::KodegenConfig;
use anyhow::Result;

fn try_load_local_config() -> Result<()> {
    // Convert Result to Option if you want to treat "not found" as non-error
    if let Ok(local_dir) = KodegenConfig::local_config_dir() {
        println!("In git repo, local config: {}", local_dir.display());
    } else {
        println!("Not in git repo, that's fine");
    }
     
    // Or use unwrap_or for fallback
    let config_dir = KodegenConfig::local_config_dir()
        .unwrap_or_else(|_| KodegenConfig::user_config_dir().unwrap());
     
    Ok(())
}

§Inspecting Error Details

use kodegen_config::KodegenConfig;

match KodegenConfig::resolve_toolset("nonexistent") {
    Ok(path) => println!("Found: {}", path.display()),
    Err(e) => {
        // Error message includes all searched paths
        eprintln!("Error: {}", e);
        // Output: "Toolset 'nonexistent' not found. Searched:
        //           /repo/.kodegen/toolset/nonexistent.json
        //           /home/user/.config/kodegen/toolset/nonexistent.json"
    }
}

Re-exports§

pub use constants::*;

Modules§

constants
Centralized constants for kodegen infrastructure

Structs§

KodegenConfig
Main configuration path resolver

Constants§

X_KODEGEN_CONNECTION_ID
Header containing the connection ID for this stdio connection instance. Used by backend servers for connection-scoped resource isolation (terminals, browsers, etc.)
X_KODEGEN_GITROOT
Header containing the git repository root directory. Used for repository-aware operations and path resolution.
X_KODEGEN_PWD
Header containing the current working directory from which kodegen was spawned. Used by backend servers for path resolution and as default CWD for operations.

Functions§

shorten_path_for_display
Display a path in the most concise human-readable format