Skip to main content

rigspark_cli/
lib.rs

1mod terminal_events;
2use rigspark_core::sizing::{SizingRequest, evaluate};
3use serde::Serialize;
4use std::io::{Read, Write};
5
6pub mod accessible;
7pub mod accessible_catalog;
8pub mod accessible_installed;
9pub mod accessible_read_only;
10pub mod accessible_recommend;
11pub mod accessible_text;
12pub mod cancellation;
13pub mod dialog_smoke;
14pub mod distribution;
15pub mod gui_launcher;
16pub mod performance;
17pub mod retirement;
18pub mod terminal;
19pub mod tui_chat;
20pub mod tui_generate;
21pub mod tui_lifecycle;
22pub mod tui_mode;
23pub mod tui_models;
24mod tui_theme;
25pub mod tui_view;
26
27pub const MAX_INPUT_BYTES: usize = 8 * 1024 * 1024;
28const MAX_BATCH: usize = 4096;
29
30#[derive(Debug, Serialize)]
31pub struct RunnerError {
32    pub code: &'static str,
33    pub message: String,
34}
35
36impl RunnerError {
37    fn new(code: &'static str, message: impl ToString) -> Self {
38        Self {
39            code,
40            message: message.to_string(),
41        }
42    }
43}
44
45pub fn run(input: impl Read, mut output: impl Write) -> Result<(), RunnerError> {
46    let mut bytes = Vec::new();
47    input
48        .take((MAX_INPUT_BYTES + 1) as u64)
49        .read_to_end(&mut bytes)
50        .map_err(|error| RunnerError::new("IO_ERROR", error))?;
51    if bytes.len() > MAX_INPUT_BYTES {
52        return Err(RunnerError::new(
53            "VALIDATION_ERROR",
54            "input exceeds 8 MiB limit",
55        ));
56    }
57    let values: Vec<serde_json::Value> = serde_json::from_slice(&bytes)
58        .map_err(|error| RunnerError::new("VALIDATION_ERROR", error))?;
59    if values.len() > MAX_BATCH {
60        return Err(RunnerError::new(
61            "VALIDATION_ERROR",
62            "batch exceeds 4096 requests",
63        ));
64    }
65    let mut results = Vec::with_capacity(values.len());
66    for value in values {
67        let request: SizingRequest = serde_json::from_value(value)
68            .map_err(|error| RunnerError::new("VALIDATION_ERROR", error))?;
69        results
70            .push(evaluate(&request).map_err(|error| RunnerError::new("VALIDATION_ERROR", error))?);
71    }
72    let encoded = serde_json::to_vec(&results)
73        .map_err(|error| RunnerError::new("SERIALIZATION_ERROR", error))?;
74    output
75        .write_all(&encoded)
76        .and_then(|()| output.write_all(b"\n"))
77        .map_err(|error| RunnerError::new("IO_ERROR", error))
78}