Rust SDK#

Async client for the Rustbox judge API on tokio and reqwest, with typed results and one typed error. Rust 1.82 or newer.

Install#

cargo add rustbox-sdk
cargo add tokio --features macros,rt-multi-thread

Quickstart#

use rustbox_sdk::{RunRequest, Rustbox, Verdict};

#[tokio::main]
async fn main() -> rustbox_sdk::Result<()> {
    let client = Rustbox::new(&std::env::var("RUSTBOX_API_KEY").unwrap_or_default())?;
    let execution = client
        .run(&RunRequest {
            language: "python".into(),
            code: "print(6 * 7)".into(),
            stdin: String::new(),
        })
        .await?;
    if execution.result.verdict == Some(Verdict::Accepted) {
        println!("{:?}", execution.output.stdout);
    }
    Ok(())
}

run returns an Execution once the run has finished: status, result.verdict, result.exit_code, output.stdout, output.stderr, metrics and evidence. Missing values are None.

Judge against test cases#

use rustbox_sdk::{JudgeCase, JudgeRequest};

let judged = client
    .judge(&JudgeRequest {
        language: "python".into(),
        code: "a, b = map(int, input().split())\nprint(a + b)".into(),
        cases: vec![
            JudgeCase { stdin: "1 2".into(), expected: "3".into() },
            JudgeCase { stdin: "2 2".into(), expected: "4".into() },
        ],
    })
    .await?;

Queue now, read later#

let accepted = client.submit(&run, Some("6a0e7c1d-2b3f-4a5e-8d9c-0b1a2c3d4e5f")).await?;
let execution = client.get_result(&accepted.id).await?;

With an idempotency key the submit is retried on transient failures; with None it is sent once.

Errors#

use rustbox_sdk::RustboxError;

match client.run(&run).await {
    Ok(execution) => println!("{:?}", execution.result.verdict),
    Err(RustboxError::Auth { .. }) => {}                        // 401/403: check the API key
    Err(RustboxError::RateLimit { retry_after_secs }) => {}     // 429
    Err(RustboxError::Server { status }) => {}                  // 5xx after the SDK's retries
    Err(RustboxError::Timeout(_)) => {}                         // a request timed out
    Err(RustboxError::StillRunning { run_id }) => {}            // polling budget used up: get_result(&run_id) later
    Err(RustboxError::Api { status, message }) => {}            // other 4xx
    Err(other) => eprintln!("{other}"),                         // config, transport, decode
}

RustboxError::status() returns the HTTP status where there is one.

Configuration#

use rustbox_sdk::{Options, Rustbox};

let client = Rustbox::with_options(&api_key, Options { region: Some("in".into()), ..Options::default() })?;

base_url pins an exact endpoint. RUSTBOX_BASE_URL and RUSTBOX_REGION are read when the options leave them None.

Source#

github.com/orkait/rustbox-sdk/rust