use serde::{Deserialize, Serialize};
use crate::{ByteChunk, RunOutput};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
#[cfg_attr(feature = "typescript", ts(export_to = "controller-api.ts"))]
pub struct SandboxInput {
pub offset: u64,
pub chunk: ByteChunk,
pub end: bool,
}
impl SandboxInput {
pub const MAX_CHUNK_BYTES: usize = 64 * 1024;
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
#[cfg_attr(feature = "typescript", ts(export_to = "controller-api.ts"))]
pub struct SandboxInputState {
pub length: u64,
pub ended: bool,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
#[cfg_attr(feature = "typescript", ts(export_to = "controller-api.ts"))]
pub struct OutputAfter {
#[serde(flatten)]
pub output: RunOutput,
pub more: bool,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn input_and_incremental_output_round_trip_as_bytes() {
let input = SandboxInput {
offset: u64::MAX,
chunk: ByteChunk::new(vec![0, 255]),
end: true,
};
let encoded = serde_json::to_string(&input).unwrap();
assert!(encoded.contains("AP8="));
assert_eq!(
serde_json::from_str::<SandboxInput>(&encoded).unwrap(),
input
);
let state = SandboxInputState {
length: 2,
ended: true,
};
assert_eq!(
serde_json::from_str::<SandboxInputState>(&serde_json::to_string(&state).unwrap())
.unwrap(),
state
);
let output = OutputAfter {
output: RunOutput {
stdout: input.chunk,
stderr: ByteChunk::default(),
},
more: false,
};
let encoded = serde_json::to_string(&output).unwrap();
assert_eq!(encoded, r#"{"stdout":"AP8=","stderr":"","more":false}"#);
assert_eq!(
serde_json::from_str::<OutputAfter>(&encoded).unwrap(),
output
);
}
}