lmrc-cargo-workspace
Part of the LMRC Stack - Infrastructure-as-Code toolkit for building production-ready Rust applications
A comprehensive Rust library for managing Cargo workspaces programmatically.
Features
- Workspace Discovery: Automatically find and parse Cargo workspaces
- Member Detection: Identify all workspace members (binaries and libraries)
- Dependency Analysis: Analyze dependency relationships and build orders
- Version Management: Update versions across workspace members using semver
- Programmatic Build/Test: Build and test with different configurations
- Selective Operations: Build/test only specific members based on patterns, changes, or dependencies
- Parallel Execution: Build and test workspace members in parallel
- Cargo Metadata Integration: Access resolved dependency information and features
- Workspace Validation: Lint for version inconsistencies, formatting issues, and code quality
- Format Preservation: Parse and update Cargo.toml files while preserving formatting and comments
- Comprehensive Error Handling: Detailed error types for all operations
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Quick Start
use ;
use Path;
Usage Examples
Workspace Discovery
use Workspace;
use Path;
// Discover workspace from current directory
let workspace = discover?;
// Or load from specific path
let workspace = from_path?;
// Access workspace members
for member in &workspace.members
Dependency Analysis
use ;
let workspace = discover?;
let graph = from_workspace?;
// Get dependencies of a crate
let deps = graph.workspace_dependencies;
for dep in deps
// Get reverse dependencies (what depends on this crate)
let dependents = graph.dependents;
println!;
// Get build order (topological sort)
let order = graph.topological_sort?;
println!;
// Check for circular dependencies
graph.check_circular_dependencies?;
Version Management
use ;
use Version;
let workspace = discover?;
let version_mgr = new;
// Bump all crates by patch version
let changes = version_mgr.update_versions?;
// Or bump specific crates
let mut bumps = new;
bumps.insert;
bumps.insert;
let changes = version_mgr.update_versions?;
// Set all crates to same version
let changes = version_mgr.update_versions?;
// Preview changes before applying
for change in &changes
// Apply changes
version_mgr.apply_changes?;
Build and Test
use ;
let workspace = discover?;
let builder = new;
// Build all members in release mode
let mut config = default;
config.profile = Release;
config.all_features = true;
let result = builder.build_all?;
// Build specific member
let result = builder.build_member?;
// Run tests
let mut test_config = default;
test_config.nocapture = true;
let result = builder.test_all?;
// Check code without building
let result = builder.check_all?;
Manifest Manipulation
use Manifest;
use Version;
use Path;
let mut manifest = from_path?;
// Update package version
manifest.set_package_version?;
// Update dependency version
manifest.update_dependency?;
// Check if it's a workspace
if manifest.is_workspace
// Save changes (preserves formatting and comments)
manifest.save?;
Selective Build and Test
use ;
let workspace = discover?;
// Build only binaries
let results = new
.binaries_only
.build?;
// Build crates matching a pattern
let results = new
.by_pattern?
.build?;
// Test only changed crates since a git ref
let results = new
.changed_since?
.with_dependents? // Include crates that depend on changes
.test?;
// Build in parallel respecting dependency order
let results = new
.all
.build_parallel?;
Cargo Metadata Integration
use CargoMetadata;
let metadata = load?;
// Check for duplicate dependency versions
let duplicates = metadata.find_duplicate_versions;
for in duplicates
// Collect license information
let licenses = metadata.collect_licenses;
for in licenses
// Access resolved dependency information
for package in metadata.workspace_packages
Workspace Validation
use Validator;
let validator = new;
// Check version consistency
let issues = validator.check_version_consistency?;
// Check for circular dependencies
let issues = validator.check_circular_dependencies?;
// Check code formatting
let issues = validator.check_formatting?;
// Check with clippy
let issues = validator.check_clippy?;
// Run all validations
let result = validator.validate_all?;
if result.has_errors
if result.has_warnings
println!;
API Documentation
Full API documentation is available at docs.rs.
Modules
workspace: Workspace discovery and member enumerationmanifest: Cargo.toml parsing and manipulation with format preservationdependency: Dependency graph analysis and topological sortingversion: Semver version management and updatesbuild: Programmatic build and test operationsselective: Selective build/test operations based on patterns, changes, or dependenciesmetadata: Cargo metadata integration for resolved dependency informationvalidation: Workspace validation and lintingerror: Comprehensive error types
Error Handling
All operations return Result<T, Error> with detailed error information:
use ;
match discover
Requirements
- Rust 2021 edition or later
- Cargo must be installed for build/test operations
Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.
License
Part of the LMRC Stack project. Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Related Projects
- cargo - The Rust package manager
- cargo-edit - CLI tool for managing Cargo dependencies
- cargo-workspace - Workspace management tool
Acknowledgments
This library uses: