| Crates.io | vx-tool-standard |
| lib.rs | vx-tool-standard |
| version | 0.4.0 |
| created_at | 2025-06-19 10:50:13.156327+00 |
| updated_at | 2025-06-19 13:53:37.585575+00 |
| description | Standard interfaces and utilities for vx tool implementations |
| homepage | https://github.com/loonghao/vx |
| repository | https://github.com/loonghao/vx |
| max_upload_size | |
| id | 1718169 |
| size | 68,177 |
Standard tool runtime interface and utilities for vx tool management.
vx-tool-standard provides a standardized interface and utilities for implementing tool runtimes in the vx ecosystem. It defines common patterns, traits, and helper functions that tool implementations can use to ensure consistency and interoperability.
Add this to your Cargo.toml:
[dependencies]
vx-tool-standard = "0.2.6"
use vx_tool_standard::{ToolRuntime, VxResult, Platform};
use async_trait::async_trait;
struct MyToolRuntime {
name: String,
version: Option<String>,
}
#[async_trait]
impl ToolRuntime for MyToolRuntime {
async fn is_available(&self) -> VxResult<bool> {
// Check if tool is available on the system
Ok(which::which(&self.name).is_ok())
}
async fn get_version(&self) -> VxResult<Option<String>> {
// Get installed version
Ok(self.version.clone())
}
async fn get_path(&self) -> VxResult<Option<PathBuf>> {
// Get tool executable path
which::which(&self.name)
.map(Some)
.map_err(|e| e.into())
}
async fn execute(&self, args: &[String]) -> VxResult<i32> {
// Execute tool with arguments
let status = std::process::Command::new(&self.name)
.args(args)
.status()?;
Ok(status.code().unwrap_or(1))
}
}
use vx_tool_standard::{StandardConfig, Platform};
let config = StandardConfig::builder()
.name("my-tool")
.version("1.0.0")
.description("My awesome tool")
.for_platforms(vec![Platform::Windows, Platform::Linux, Platform::MacOS])
.build();
println!("Tool: {} v{}", config.name(), config.version());
use vx_tool_standard::Platform;
let current_platform = Platform::current();
match current_platform {
Platform::Windows => {
// Windows-specific logic
println!("Running on Windows");
}
Platform::Linux => {
// Linux-specific logic
println!("Running on Linux");
}
Platform::MacOS => {
// macOS-specific logic
println!("Running on macOS");
}
Platform::Unknown => {
println!("Unknown platform");
}
}
use vx_tool_standard::{VxResult, VxError};
fn tool_operation() -> VxResult<String> {
// Operation that might fail
if some_condition {
Ok("Success".to_string())
} else {
Err(VxError::ToolNotFound("my-tool".to_string()))
}
}
match tool_operation() {
Ok(result) => println!("Success: {}", result),
Err(VxError::ToolNotFound(tool)) => {
eprintln!("Tool not found: {}", tool);
}
Err(e) => eprintln!("Error: {}", e),
}
use vx_tool_standard::{StandardConfig, Configurable};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
struct MyToolConfig {
#[serde(flatten)]
standard: StandardConfig,
custom_option: String,
debug_mode: bool,
}
impl Configurable for MyToolConfig {
type Config = Self;
fn config(&self) -> &Self::Config {
self
}
}
use vx_tool_standard::testing::{MockToolRuntime, TestEnvironment};
#[tokio::test]
async fn test_tool_runtime() {
let mut mock = MockToolRuntime::new("test-tool");
mock.expect_is_available().returning(|| Ok(true));
mock.expect_get_version().returning(|| Ok(Some("1.0.0".to_string())));
assert!(mock.is_available().await.unwrap());
assert_eq!(mock.get_version().await.unwrap(), Some("1.0.0".to_string()));
}
#[test]
fn test_with_environment() {
let env = TestEnvironment::new()
.with_tool("node", "18.17.0")
.with_tool("npm", "9.6.7");
env.run(|| {
// Test code that depends on tools being available
assert!(which::which("node").is_ok());
assert!(which::which("npm").is_ok());
});
}
use vx_tool_standard::{ToolRuntime, StandardConfig};
use vx_plugin::VxTool;
struct StandardTool {
config: StandardConfig,
runtime: Box<dyn ToolRuntime>,
}
impl VxTool for StandardTool {
fn name(&self) -> &str {
self.config.name()
}
fn description(&self) -> &str {
self.config.description()
}
// Implement other VxTool methods using runtime
}
use vx_tool_standard::StandardConfig;
use vx_config::VxConfig;
let vx_config = VxConfig::load()?;
let tool_config = StandardConfig::from_vx_config(&vx_config, "my-tool")?;
Check the examples/ directory for complete examples:
basic_tool.rs - Basic tool runtime implementationconfigurable_tool.rs - Tool with custom configurationplatform_specific.rs - Platform-specific tool behaviortesting_example.rs - Testing tool implementationscargo test
Run with logging:
RUST_LOG=vx_tool_standard=debug cargo test
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.