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:
user_config_dir()- User-global root directory (~/.config/kodegen)local_config_dir()- Git workspace-local config directory (.kodegen/)
Subdirectories (all under root):
config_dir()- Configuration files (root/config/)toolset_dir()- Tool definitions (root/toolset/)state_dir()- Runtime state: PIDs, sockets (root/state/)log_dir()- Log files (root/logs/)data_dir()- Persistent data: DB, certs (root/data/)cache_dir()- Temporary cache: builds, downloads (root/cache/)bin_dir()- Binaries (root/bin/)
File resolution:
resolve_toolset()- Resolve toolset file with precedenceresolve_config_file()- Resolve config file with precedence
This uniform Result pattern provides:
- Consistency - All similar operations use the same error handling pattern
- Rich error context - Errors explain what failed and where the system searched
- Flexible handling - Callers can propagate (
?), unwrap, or convert toOptionvia.ok()
§Usage Examples
§Error Propagation (Recommended)
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§
- Kodegen
Config - 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