Crate lmrc_cargo_workspace

Crate lmrc_cargo_workspace 

Source
Expand description

§Cargo Workspace Manager

A comprehensive library for managing Cargo workspaces programmatically.

This library provides a high-level API for:

  • Discovering and analyzing Cargo workspaces
  • Detecting workspace members (binaries and libraries)
  • Analyzing dependency relationships between crates
  • Building and testing workspace members programmatically (including selective and parallel builds)
  • Managing versions across workspace members using semver
  • Parsing and updating Cargo.toml files while preserving formatting
  • Cargo metadata integration for resolved dependency information
  • Workspace validation and linting (formatting, clippy, dependencies)

§Quick Start

use lmrc_cargo_workspace::{
    workspace::Workspace,
    build::{WorkspaceBuilder, BuildConfig},
    version::{VersionManager, BumpType, VersionUpdateStrategy},
    dependency::DependencyGraph,
};
use std::path::Path;

// Discover workspace
let workspace = Workspace::discover(Path::new("."))?;
println!("Found workspace at: {}", workspace.root.display());
println!("Members: {}", workspace.members.len());

// Analyze dependencies
let graph = DependencyGraph::from_workspace(&workspace)?;
let build_order = graph.topological_sort()?;
println!("Build order: {:?}", build_order);

// Build workspace
let builder = WorkspaceBuilder::new(&workspace);
let result = builder.build_all(&BuildConfig::default())?;
if result.success {
    println!("Build succeeded!");
}

// Bump versions
let version_mgr = VersionManager::new(&workspace);
let changes = version_mgr.preview_changes(
    VersionUpdateStrategy::BumpAll(BumpType::Patch)
)?;
for change in &changes {
    println!("{}", change.description());
}

§Features

§Workspace Discovery

Automatically find and parse Cargo workspaces:

use lmrc_cargo_workspace::workspace::Workspace;
use std::path::Path;

// Discover workspace from current directory
let workspace = Workspace::discover(Path::new("."))?;

// Or load from specific path
let workspace = Workspace::from_path(Path::new("/path/to/workspace"))?;

// Access workspace members
for member in &workspace.members {
    println!("Member: {} at {}", member.name, member.path.display());
}

§Dependency Analysis

Analyze dependency relationships and build orders:

use lmrc_cargo_workspace::{workspace::Workspace, dependency::DependencyGraph};
use std::path::Path;

let workspace = Workspace::discover(Path::new("."))?;
let graph = DependencyGraph::from_workspace(&workspace)?;

// Get dependencies of a crate
let deps = graph.workspace_dependencies("my-crate");
for dep in deps {
    println!("Depends on: {}", dep.name);
}

// Get reverse dependencies
let dependents = graph.dependents("my-lib");
println!("Crates depending on my-lib: {:?}", dependents);

// Get build order
let order = graph.topological_sort()?;
println!("Build in this order: {:?}", order);

§Version Management

Manage versions across workspace members:

use lmrc_cargo_workspace::{
    workspace::Workspace,
    version::{VersionManager, BumpType, VersionUpdateStrategy},
};
use std::path::Path;
use semver::Version;

let workspace = Workspace::discover(Path::new("."))?;
let version_mgr = VersionManager::new(&workspace);

// Bump all crates by patch version
let changes = version_mgr.update_versions(
    VersionUpdateStrategy::BumpAll(BumpType::Patch)
)?;

// Apply changes
version_mgr.apply_changes(&changes)?;

§Build and Test

Build and test workspace members programmatically:

use lmrc_cargo_workspace::{
    workspace::Workspace,
    build::{WorkspaceBuilder, BuildConfig, TestConfig, BuildProfile},
};
use std::path::Path;

let workspace = Workspace::discover(Path::new("."))?;
let builder = WorkspaceBuilder::new(&workspace);

// Build in release mode
let mut config = BuildConfig::default();
config.profile = BuildProfile::Release;
let result = builder.build_all(&config)?;

// Run tests
let test_result = builder.test_all(&TestConfig::default())?;

§Manifest Manipulation

Parse and update Cargo.toml files while preserving formatting:

use lmrc_cargo_workspace::manifest::Manifest;
use std::path::Path;
use semver::Version;

let mut manifest = Manifest::from_path(Path::new("Cargo.toml"))?;

// Update version
manifest.set_package_version(&Version::new(2, 0, 0))?;

// Update dependency
manifest.update_dependency("serde", "2.0", "dependencies")?;

// Save (preserves formatting and comments)
manifest.save()?;

§Error Handling

All operations return a Result<T, Error> with comprehensive error types:

use lmrc_cargo_workspace::{workspace::Workspace, error::Error};
use std::path::Path;

match Workspace::discover(Path::new(".")) {
    Ok(workspace) => println!("Found workspace!"),
    Err(Error::WorkspaceNotFound(path)) => {
        eprintln!("No workspace found at: {}", path.display());
    }
    Err(e) => eprintln!("Error: {}", e),
}

Re-exports§

pub use error::Error;
pub use error::Result;

Modules§

build
Build and test operations for workspace crates.
dependency
Dependency analysis and graph construction.
error
Error types for cargo-workspace-manager.
manifest
Cargo.toml manifest parsing and manipulation.
metadata
Cargo metadata integration.
selective
Selective build and test operations.
validation
Workspace validation and linting.
version
Version management and semver operations.
workspace
Workspace discovery and structure analysis.