use derive_more::IsVariant;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use structopt::StructOpt;
use strum::AsRefStr;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Request {
pub id: usize,
pub payload: RequestPayload,
}
impl From<RequestPayload> for Request {
fn from(payload: RequestPayload) -> Self {
let id = rand::random();
Self { id, payload }
}
}
#[derive(Clone, Debug, PartialEq, Eq, AsRefStr, IsVariant, StructOpt, Serialize, Deserialize)]
#[serde(
rename_all = "snake_case",
deny_unknown_fields,
tag = "type",
content = "data"
)]
#[strum(serialize_all = "snake_case")]
pub enum RequestPayload {
#[structopt(visible_aliases = &["cat"])]
FileRead {
path: PathBuf,
},
FileReadText {
path: PathBuf,
},
FileWrite {
path: PathBuf,
data: Vec<u8>,
},
FileWriteText {
path: PathBuf,
text: String,
},
FileAppend {
path: PathBuf,
data: Vec<u8>,
},
FileAppendText {
path: PathBuf,
text: String,
},
#[structopt(visible_aliases = &["ls"])]
DirRead {
path: PathBuf,
#[structopt(short, long)]
all: bool,
},
DirCreate {
path: PathBuf,
#[structopt(short, long)]
all: bool,
},
Remove {
path: PathBuf,
#[structopt(short, long)]
force: bool,
},
Copy {
src: PathBuf,
dst: PathBuf,
},
Rename {
src: PathBuf,
dst: PathBuf,
},
Metadata {
path: PathBuf,
},
ProcRun {
cmd: String,
args: Vec<String>,
},
ProcKill {
id: usize,
},
ProcStdin {
id: usize,
data: Vec<u8>,
},
ProcList {},
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Response {
pub id: usize,
pub origin_id: Option<usize>,
pub payload: ResponsePayload,
}
impl Response {
pub fn from_payload_with_origin(payload: ResponsePayload, origin_id: usize) -> Self {
let id = rand::random();
Self {
id,
origin_id: Some(origin_id),
payload,
}
}
}
impl From<ResponsePayload> for Response {
fn from(payload: ResponsePayload) -> Self {
let id = rand::random();
Self {
id,
origin_id: None,
payload,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, AsRefStr, IsVariant, Serialize, Deserialize)]
#[serde(
rename_all = "snake_case",
deny_unknown_fields,
tag = "type",
content = "data"
)]
#[strum(serialize_all = "snake_case")]
pub enum ResponsePayload {
Ok,
Error {
description: String,
},
Blob {
data: Vec<u8>,
},
Text {
data: String,
},
DirEntries {
entries: Vec<DirEntry>,
},
Metadata {
data: Metadata,
},
ProcStart {
id: usize,
},
ProcStdout {
id: usize,
data: Vec<u8>,
},
ProcStderr {
id: usize,
data: Vec<u8>,
},
ProcDone {
id: usize,
success: bool,
code: Option<i32>,
},
ProcEntries {
entries: Vec<RunningProcess>,
},
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct DirEntry {
pub path: PathBuf,
pub file_type: FileType,
pub depth: usize,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Metadata {
pub file_type: FileType,
pub len: u64,
pub readonly: bool,
pub accessed: Option<u128>,
pub created: Option<u128>,
pub modified: Option<u128>,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, AsRefStr, IsVariant, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
#[strum(serialize_all = "snake_case")]
pub enum FileType {
Dir,
File,
SymLink,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct RunningProcess {
pub cmd: String,
pub args: Vec<String>,
pub id: usize,
}