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 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 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
}
#[derive(Serialize, Deserialize)]
pub struct WatchEvent {
pub path: String,
pub event: String,
}
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);
}
}