11 releases
Uses new Rust 2024
| 0.2.4 | Jul 7, 2025 |
|---|---|
| 0.2.3 | Jul 6, 2025 |
| 0.1.7 | Jul 5, 2025 |
#1397 in Rust patterns
422 downloads per month
Used in 3 crates
32KB
696 lines
raz-common
Common utilities and shared types for the RAZ project ecosystem.
Features
- Environment Management: Parse and build environment variables
- Error Handling: Common error types with context support
- Output Formatting: Consistent output formatting across crates
- Shell Commands: Safe shell command execution utilities
- Time Utilities: Elapsed time tracking and formatting
- Cargo Flags: Common cargo flag definitions and parsing
Usage
use raz_common::{EnvBuilder, ShellCommand, Result};
// Build environment variables
let env = EnvBuilder::new()
.add("RUST_BACKTRACE", "1")
.add("RUST_LOG", "debug")
.build();
// Execute shell commands safely
let output = ShellCommand::new("cargo")
.arg("test")
.envs(&env)
.execute()?;
// Use common error handling
use raz_common::{CommonError, ErrorContext};
fn process_file(path: &str) -> Result<()> {
std::fs::read_to_string(path)
.context("Failed to read file")?;
Ok(())
}
Components
Environment Management
use raz_common::{EnvParser, EnvBuilder};
// Parse environment strings
let vars = EnvParser::parse("RUST_BACKTRACE=1 RUST_LOG=debug")?;
// Build environment programmatically
let env = EnvBuilder::new()
.add("KEY", "value")
.build();
Error Handling
use raz_common::{CommonError, ErrorContext};
// Add context to errors
let result = std::fs::read("file.txt")
.context("Failed to read configuration")?;
// Create custom errors
let err = CommonError::InvalidInput("Invalid format".to_string());
Output Formatting
use raz_common::OutputFormatter;
let formatter = OutputFormatter::new();
formatter.success("Operation completed");
formatter.warning("This might cause issues");
formatter.error("Operation failed");
Time Utilities
use raz_common::{Elapsed, TimeUtils};
let elapsed = Elapsed::start();
// ... do work ...
println!("Operation took: {}", elapsed.format());
Integration
This crate is used throughout the RAZ ecosystem:
raz-coreuses it for error handling and environment managementraz-cliuses it for output formatting and shell commandsraz-validationuses it for error contextsraz-overrideuses it for time tracking
License
MIT
Dependencies
~1.6–4.5MB
~81K SLoC