simplejrpc 0.1.0

Unix-socket JSON-RPC 2.0 SDK with middleware, config, and validation
Documentation
//! Small utilities (Go `boxs` / `gpath` subset).
//!
//! These helpers are intentionally tiny; prefer them over ad-hoc one-liners in
//! application code when you want parity with the Go SDK naming.
//!
//! # Examples
//!
//! ```
//! use simplejrpc::util::{cond, join_ints};
//!
//! assert_eq!(cond(true, "a", "b"), "a");
//! assert_eq!(cond(false, "a", "b"), "b");
//! assert_eq!(join_ints(&[1, 2, 3]), "1,2,3");
//! assert_eq!(join_ints(&[]), "");
//! ```

/// Ternary-like helper (Go `boxs/cond.If`).
///
/// Returns `a` when `condition` is true, otherwise `b`.
///
/// # Examples
///
/// ```
/// use simplejrpc::util::cond;
/// assert_eq!(cond(2 > 1, 10, 20), 10);
/// assert_eq!(cond(2 < 1, 10, 20), 20);
/// ```
pub fn cond<T>(condition: bool, a: T, b: T) -> T {
    if condition {
        a
    } else {
        b
    }
}

/// Join integers with commas (Go `slice.JoinIntSlice`).
///
/// # Examples
///
/// ```
/// use simplejrpc::util::join_ints;
/// assert_eq!(join_ints(&[7, 8]), "7,8");
/// assert_eq!(join_ints(&[42]), "42");
/// ```
pub fn join_ints(vals: &[i64]) -> String {
    vals.iter()
        .map(|v| v.to_string())
        .collect::<Vec<_>>()
        .join(",")
}