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_lifecycle;
21pub mod tui_mode;
22pub mod tui_models;
23pub mod tui_view;
24
25pub const MAX_INPUT_BYTES: usize = 8 * 1024 * 1024;
26const MAX_BATCH: usize = 4096;
27
28#[derive(Debug, Serialize)]
29pub struct RunnerError {
30    pub code: &'static str,
31    pub message: String,
32}
33
34impl RunnerError {
35    fn new(code: &'static str, message: impl ToString) -> Self {
36        Self {
37            code,
38            message: message.to_string(),
39        }
40    }
41}
42
43pub fn run(input: impl Read, mut output: impl Write) -> Result<(), RunnerError> {
44    let mut bytes = Vec::new();
45    input
46        .take((MAX_INPUT_BYTES + 1) as u64)
47        .read_to_end(&mut bytes)
48        .map_err(|error| RunnerError::new("IO_ERROR", error))?;
49    if bytes.len() > MAX_INPUT_BYTES {
50        return Err(RunnerError::new(
51            "VALIDATION_ERROR",
52            "input exceeds 8 MiB limit",
53        ));
54    }
55    let values: Vec<serde_json::Value> = serde_json::from_slice(&bytes)
56        .map_err(|error| RunnerError::new("VALIDATION_ERROR", error))?;
57    if values.len() > MAX_BATCH {
58        return Err(RunnerError::new(
59            "VALIDATION_ERROR",
60            "batch exceeds 4096 requests",
61        ));
62    }
63    let mut results = Vec::with_capacity(values.len());
64    for value in values {
65        let request: SizingRequest = serde_json::from_value(value)
66            .map_err(|error| RunnerError::new("VALIDATION_ERROR", error))?;
67        results
68            .push(evaluate(&request).map_err(|error| RunnerError::new("VALIDATION_ERROR", error))?);
69    }
70    let encoded = serde_json::to_vec(&results)
71        .map_err(|error| RunnerError::new("SERIALIZATION_ERROR", error))?;
72    output
73        .write_all(&encoded)
74        .and_then(|()| output.write_all(b"\n"))
75        .map_err(|error| RunnerError::new("IO_ERROR", error))
76}