herolib-clients
A unified client library providing database clients, messaging clients, and API clients.
Documentation
Features
- Pure Rust: No C dependencies, safe and portable
- Builder Pattern: All clients use fluent builder APIs for configuration
- Async Support: Built on Tokio for efficient async operations
- Rhai Integration: Full scripting support for all clients
- Connection Management: Automatic connection handling, pooling, and reconnection
Clients
| Client | Description |
|---|---|
| MQTT | MQTT messaging client with QoS 0/1/2 support |
| Redis | Redis client with connection management |
| PostgreSQL | PostgreSQL client with connection pooling |
| Mycelium | Mycelium mesh network client |
| HeroDB | HeroDB modular database client |
| Hetzner | Hetzner Robot API client |
| Vast.ai | Vast.ai GPU marketplace API client |
| OpenCode | OpenCode AI coding assistant server client |
Installation
Add to your Cargo.toml:
[]
= "0.1"
Interactive TUI REPL
The package includes a full-featured interactive REPL with:
- Tab Completion - Complete functions, methods, and Rhai keywords
- Syntax Highlighting - Color-coded keywords, functions, strings, and more
- Command History - Persistent history saved to
~/.herolib_clients_history - Multi-line Input - End lines with
\or use unclosed braces - Module-specific Help - Detailed help for each client module
# Build and run the REPL
# Or use the run.sh script
Example session:
herolib-clients REPL v0.1.5
Type /help for commands, Tab to complete, /quit to exit
>> let client = redis_client().host("localhost").port(6379).build()
>> client.set("key", "value")
>> print(client.get("key"))
value
>> /redis # Show Redis-specific help
>> /functions # List all available functions
>> /methods # List all builder and client methods
>> /quit
REPL Commands:
| Command | Description |
|---|---|
/help |
Show help and features |
/functions |
List all available Rhai functions |
/methods |
List all builder and client methods |
/redis |
Show Redis module help with examples |
/postgres |
Show PostgreSQL module help |
/mqtt |
Show MQTT module help |
/mycelium |
Show Mycelium module help |
/load <file> |
Load and execute a .rhai script |
/clear |
Clear the variable scope |
/quit |
Exit the REPL |
Quick Start
Rust Usage
use ;
// MQTT - Publish with reliable delivery
use ;
let config = new
.host
.port
.credentials
.clean_session
.build;
let client = new?;
client.connect.await?;
client.publish_with_delivery.await?;
// Redis - Key-value operations
use ;
let config = new
.host
.port
.password
.build?;
let mut cmd = cmd;
cmd.arg.arg;
execute?;
// PostgreSQL - Query execution
use ;
let config = new
.host
.port
.user
.password
.database
.build?;
let rows = query?;
Rhai Scripting
All clients use the builder pattern for configuration in Rhai scripts:
// MQTT with QoS 2 (exactly once delivery)
let mqtt = mqtt_client()
.host("localhost")
.port(1883)
.credentials("user", "pass")
.clean_session(false)
.build();
mqtt.connect();
mqtt.subscribe("orders/#", 2);
mqtt.publish("orders/new", "order-123", 2);
mqtt.disconnect();
// Redis with builder pattern
let redis = redis_client()
.host("localhost")
.port(6379)
.password("secret")
.db(0)
.build();
redis.set("key", "value");
let value = redis.get("key");
redis.hset("hash", "field", "value");
// PostgreSQL with builder pattern
let pg = postgres_client()
.host("localhost")
.port(5432)
.user("postgres")
.password("secret")
.database("mydb")
.build();
let rows = pg.query("SELECT * FROM users");
pg.execute("INSERT INTO users (name) VALUES ('John')");
// Hetzner with builder pattern
let hetzner = hetzner_client()
.username("user")
.password("pass")
.build();
let servers = hetzner.get_servers();
for server in servers {
print(`Server: ${server.server_name}`);
}
// Vast.ai GPU search with builder pattern
let query = search_query()
.available()
.gpu_name("RTX_4090")
.min_reliability(0.95)
.limit(10)
.order_by("dph_total", "asc");
let offers = search_offers(query);
for offer in offers {
print(`${offer.num_gpus}x ${offer.gpu_name} @ $${offer.dph_total}/hr`);
}
// Instance management
let instances = list_instances();
let ssh_keys = list_ssh_keys();
// OpenCode - AI coding assistant
opencode_url_set("http://127.0.0.1:2467");
let project = opencode_project_current();
print(`Project: ${project.worktree}`);
let session = opencode_session_create_with_title("My Session");
let response = opencode_prompt(session.id, "Write a hello world function");
let files = opencode_file_list_path("src");
for f in files {
print(`${f.name}`);
}
// HeroDB - Modular database with multiple backends
herodb_connect_default();
herodb_create_instance("my_cache", "INMEMORY", "");
herodb_select(1);
herodb_set("key", "value");
let val = herodb_get("key");
// Vector search with LanceDB backend
herodb_create_instance("vectors", "LANCEDB", "/tmp/vectors");
herodb_lancedb_add("doc:1", [0.1, 0.2, 0.3, 0.4]);
let results = herodb_lancedb_search([0.1, 0.2, 0.3, 0.4], 5);
// Full-text search with Tantivy backend
herodb_create_instance("search", "TANTIVY", "/tmp/search");
herodb_tantivy_index("doc:1", #{ title: "Hello", body: "World" });
let matches = herodb_tantivy_search("Hello", 10);
Running Rhai Scripts
Use hero-clients to run Rhai scripts directly:
# Build the binary
# Run a script
# With environment variables
VAST_API_KEY=your-key
Example scripts are provided in rhaiexamples/:
herodb/- HeroDB modular database operationshetzner/- Dedicated server managementmqtt/- Message broker operationsmycelium/- Mesh network operationsopencode/- AI coding assistant operationspostgres/- Database operationsredis/- Key-value store operationsvastai/- GPU marketplace operations
Configuration
All clients support configuration through:
- Builder Pattern (recommended)
- Environment Variables
Environment Variables
| Variable | Client | Description |
|---|---|---|
REDIS_HOST |
Redis | Redis server host |
REDIS_PORT |
Redis | Redis server port |
REDIS_PASSWORD |
Redis | Redis authentication password |
REDISDB |
Redis | Redis database number |
POSTGRES_HOST |
PostgreSQL | PostgreSQL server host |
POSTGRES_PORT |
PostgreSQL | PostgreSQL server port |
POSTGRES_USER |
PostgreSQL | PostgreSQL username |
POSTGRES_PASSWORD |
PostgreSQL | PostgreSQL password |
POSTGRES_DB |
PostgreSQL | PostgreSQL database name |
HETZNER_USERNAME |
Hetzner | Hetzner Robot API username |
HETZNER_PASSWORD |
Hetzner | Hetzner Robot API password |
HETZNER_API_URL |
Hetzner | Hetzner Robot API URL |
VAST_API_KEY |
Vast.ai | Vast.ai API key |
OPENCODE_URL |
OpenCode | OpenCode server URL (default: http://127.0.0.1:2467) |
License
Apache-2.0