simulator-client 0.31.0

Async WebSocket client for the Solana simulator backtest API
Documentation
//! Venue templates for rerouting swaps directly to a venue.
//!
//! Load a template file with [`parse_templates`] and pass the result to [`crate::CreateSession`]'s
//! `reroute_venue_templates`.

use std::collections::HashSet;

use simulator_api::VenueTemplate;

/// The templates a JSON array names, refused if it names none or repeats a label. `source` only
/// names the input in the error.
///
/// Labels are a template's identity on every row the server writes, so it refuses a repeat;
/// caught here first to save the round trip.
///
/// # Errors
///
/// A message naming `source` when `raw` does not parse, is empty, or repeats a label.
pub fn parse_templates(source: &str, raw: &str) -> Result<Vec<VenueTemplate>, String> {
    let templates: Vec<VenueTemplate> = crate::error::from_json(raw.as_bytes())
        .map_err(|error| format!("parsing {source}: {error}"))?;
    if templates.is_empty() {
        return Err(format!("{source}: names no templates"));
    }
    let mut labels = HashSet::new();
    if let Some(duplicate) = templates
        .iter()
        .map(|template| template.label.as_str())
        .find(|label| !labels.insert(*label))
    {
        return Err(format!(
            "{source}: template label {duplicate:?} appears more than once"
        ));
    }
    Ok(templates)
}