use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub mod frame;
#[derive(Serialize, Deserialize)]
pub struct ExecRequest {
pub argv: Vec<String>,
#[serde(default)]
pub env: HashMap<String, String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tty: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub rows: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cols: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cwd: Option<String>,
}
#[derive(Debug, Clone)]
pub struct PortMapping {
pub host_port: u16,
pub guest_port: u16,
}
#[derive(Serialize, Deserialize)]
pub struct ForwardRequest {
pub port: u16,
}
#[derive(Serialize, Deserialize)]
pub struct ForwardResponse {
pub status: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MountRequest {
pub tag: String,
pub guest_path: String,
#[serde(default = "default_true")]
pub read_only: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MountResponse {
pub tag: String,
pub ok: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
}
#[derive(Serialize, Deserialize)]
pub struct ReadFileRequest {
pub path: String,
}
#[derive(Serialize, Deserialize)]
pub struct WriteFileRequest {
pub path: String,
pub len: u64,
}
#[derive(Serialize, Deserialize)]
pub struct WriteFileResponse {
pub ok: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
}
#[derive(Serialize, Deserialize)]
pub struct FsOkResponse {
pub ok: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
}
#[derive(Serialize, Deserialize)]
pub struct DownloadRequest {
pub url: String,
pub path: String,
#[serde(default)]
pub extract: bool,
}
#[derive(Serialize, Deserialize)]
pub struct DownloadProgress {
pub bytes_downloaded: u64,
pub total_bytes: Option<u64>,
}
#[derive(Serialize, Deserialize)]
pub struct MkdirRequest {
pub path: String,
#[serde(default = "default_true")]
pub recursive: bool,
}
#[derive(Serialize, Deserialize)]
pub struct ReadDirRequest {
pub path: String,
}
#[derive(Serialize, Deserialize)]
pub struct DirEntry {
pub name: String,
#[serde(rename = "type")]
pub entry_type: String,
pub size: u64,
}
#[derive(Serialize, Deserialize)]
pub struct ReadDirResponse {
pub entries: Vec<DirEntry>,
}
#[derive(Serialize, Deserialize)]
pub struct StatRequest {
pub path: String,
}
#[derive(Serialize, Deserialize)]
pub struct StatResponse {
pub size: u64,
pub mode: u32,
pub mtime: u64,
pub is_dir: bool,
pub is_file: bool,
pub is_symlink: bool,
}
#[derive(Serialize, Deserialize)]
pub struct RemoveRequest {
pub path: String,
#[serde(default)]
pub recursive: bool,
}
#[derive(Serialize, Deserialize)]
pub struct DiscardRequest {
pub path: String,
}
#[derive(Serialize, Deserialize)]
pub struct RenameRequest {
pub old_path: String,
pub new_path: String,
}
#[derive(Serialize, Deserialize)]
pub struct CopyRequest {
pub src: String,
pub dst: String,
#[serde(default)]
pub recursive: bool,
}
#[derive(Serialize, Deserialize)]
pub struct ChmodRequest {
pub path: String,
pub mode: u32,
}
#[derive(Serialize, Deserialize)]
pub struct WatchRequest {
pub path: String,
#[serde(default = "default_true")]
pub recursive: bool,
}
fn default_true() -> bool {
true
}
pub mod watch_kind {
pub const CREATE: u8 = 0x01;
pub const MODIFY: u8 = 0x02;
pub const DELETE: u8 = 0x03;
pub const RENAME: u8 = 0x04;
}
#[derive(Debug, Clone)]
pub struct WatchEvent {
pub kind: u8,
pub path: String,
}
impl WatchEvent {
pub fn encode(&self) -> Vec<u8> {
let mut buf = Vec::with_capacity(1 + self.path.len());
buf.push(self.kind);
buf.extend_from_slice(self.path.as_bytes());
buf
}
pub fn decode(payload: &[u8]) -> Option<Self> {
if payload.is_empty() {
return None;
}
let kind = payload[0];
let path = std::str::from_utf8(&payload[1..]).ok()?.to_string();
Some(Self { kind, path })
}
}
pub const VSOCK_PORT: u32 = 1024;
pub const VSOCK_PORT_FORWARD: u32 = 1025;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mount_request_read_only_true_by_default() {
let json = r#"{"tag":"mount0","guest_path":"/workspace"}"#;
let req: MountRequest = serde_json::from_str(json).unwrap();
assert!(req.read_only);
}
#[test]
fn mount_request_read_only_false_roundtrips() {
let req = MountRequest {
tag: "mount0".into(),
guest_path: "/workspace".into(),
read_only: false,
};
let json = serde_json::to_string(&req).unwrap();
let req2: MountRequest = serde_json::from_str(&json).unwrap();
assert!(!req2.read_only);
}
}