Skip to content

Utils Crate

Common utilities for the Recoco ecosystem.

This crate provides shared building blocks used across Recoco’s core and operation modules. While Recoco primarily uses this crate internally, these utilities can be useful for developing custom operations or for standalone use in Rust projects.

[dependencies]
recoco-utils = { version = "0.2", features = ["batching", "fingerprint"] }

recoco-utils is highly modular with no default features to keep dependencies minimal. Enable only what you need.

FeatureDescriptionKey DependenciesUse When
batchingAsync batch processing with concurrency controltokio-util, serdeBuilding efficient data pipelines with batch operations
bytes_decodeSmart encoding detection and UTF-8 decodingencoding_rsProcessing files with unknown or mixed encodings
concur_controlConcurrency limiting and rate control primitivestokioManaging concurrent operations and backpressure
deserializeJSON deserialization helpers with better error messagesserde, serde_json, serde_path_to_errorParsing JSON with detailed error reporting
fingerprintContent hashing (BLAKE3) and fingerprintingblake3, base64, hexChange detection, deduplication, caching
immutableImmutable data structures (Arc-based collections)NoneSafe concurrent access to shared data
retryableExponential backoff retry logictokio, rand, timeNetwork calls, external APIs, unreliable operations
str_sanitizeString cleaning and SQL-safe sanitizationserde, sqlxInput validation, SQL injection prevention
yamlYAML parsing and serializationyaml-rust2, base64Configuration files, structured data

[!NOTE] This list isn’t exhaustive. It excludes features specific to recoco-core. The above features cover all functionality of the crate, providing granular by-module access.

Efficient batch processing with concurrency control:

use recoco_utils::batching::{Batcher, BatchConfig};
let config = BatchConfig {
max_batch_size: 100,
max_wait_ms: 1000,
max_inflight: 10,
};
let batcher = Batcher::new(config, |batch| async move {
// Process batch
Ok(())
}).await?;
batcher.send(item).await?;

Content-addressable hashing with BLAKE3:

use recoco_utils::fingerprint::{fingerprint, Fingerprint};
let hash = fingerprint(b"hello world");
let hex_string = hash.to_hex();
let base64_string = hash.to_base64();

Exponential backoff for unreliable operations:

use recoco_utils::retryable::{retry_with_backoff, RetryConfig};
let result = retry_with_backoff(
|| async {
// Your operation that might fail
api_call().await
},
RetryConfig {
max_attempts: 5,
initial_delay_ms: 100,
max_delay_ms: 10000,
backoff_multiplier: 2.0,
}
).await?;

Limit concurrent operations:

use recoco_utils::concur_control::Semaphore;
let sem = Semaphore::new(10); // Max 10 concurrent operations
let _permit = sem.acquire().await?;
// Do work while holding permit
// Permit is released when dropped

Arc-based collections for safe sharing:

use recoco_utils::immutable::{ImmArcVec, ImmArcMap};
let vec = ImmArcVec::from(vec![1, 2, 3]);
let cloned = vec.clone(); // Cheap Arc clone
let map = ImmArcMap::from([("key", "value")]);

Smart encoding detection:

use recoco_utils::bytes_decode::decode_bytes_to_string;
let text = decode_bytes_to_string(&bytes)?;
// Automatically detects UTF-8, UTF-16, latin1, etc.

Some features depend on others. Most are fully independent, except:

  • batching requires concur_control, fingerprint, and retryable
  • fingerprint requires deserialize

Enabling a feature automatically enables its dependencies.

recoco-utils = { version = "0.2", features = ["batching", "fingerprint", "retryable"] }
recoco-utils = { version = "0.2", features = ["server", "deserialize", "uuid"] }
recoco-utils = { version = "0.2", features = ["s3", "azure", "retryable"] }
recoco-utils = { version = "0.2", features = ["sqlx", "uuid", "fingerprint"] }
recoco-utils = { version = "0.2", features = ["openai", "batching", "retryable"] }

This crate is part of the Recoco workspace. See the main repository for development guidelines.

Apache-2.0. See main repository for details.