Skip to main content

rexec/
types.rs

1//! Type definitions for Rexec SDK.
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6/// Sandbox status values (string form also used on the wire).
7#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
8#[serde(rename_all = "lowercase")]
9pub enum SandboxStatus {
10    Running,
11    Stopped,
12    Creating,
13    Error,
14    #[serde(other)]
15    Unknown,
16}
17
18/// Deprecated alias for [`SandboxStatus`].
19#[deprecated(since = "1.1.0", note = "use SandboxStatus")]
20pub type ContainerStatus = SandboxStatus;
21
22/// Represents a Rexec sandbox (isolated Linux environment).
23#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct Sandbox {
25    /// Sandbox ID.
26    pub id: String,
27    /// Sandbox name.
28    #[serde(default)]
29    pub name: String,
30    /// Image alias.
31    #[serde(default)]
32    pub image: String,
33    /// Current status.
34    #[serde(default)]
35    pub status: String,
36    /// Creation timestamp.
37    #[serde(default)]
38    pub created_at: String,
39    /// Start timestamp (if running).
40    #[serde(default)]
41    pub started_at: Option<String>,
42    /// Labels.
43    #[serde(default)]
44    pub labels: HashMap<String, String>,
45    /// Environment variables.
46    #[serde(default)]
47    pub environment: HashMap<String, String>,
48}
49
50/// Deprecated alias for [`Sandbox`].
51#[deprecated(since = "1.1.0", note = "use Sandbox")]
52pub type Container = Sandbox;
53
54/// Request to create a new sandbox. Prefer image aliases (e.g. `ubuntu`).
55#[derive(Debug, Clone, Serialize, Default)]
56pub struct CreateSandboxRequest {
57    /// Image alias to use.
58    pub image: String,
59    /// Optional sandbox name.
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub name: Option<String>,
62    /// Environment variables.
63    #[serde(skip_serializing_if = "HashMap::is_empty", default)]
64    pub environment: HashMap<String, String>,
65    /// Labels.
66    #[serde(skip_serializing_if = "HashMap::is_empty", default)]
67    pub labels: HashMap<String, String>,
68}
69
70/// Deprecated alias for [`CreateSandboxRequest`].
71#[deprecated(since = "1.1.0", note = "use CreateSandboxRequest")]
72pub type CreateContainerRequest = CreateSandboxRequest;
73
74impl CreateSandboxRequest {
75    /// Create a new request with the given image alias.
76    pub fn new(image: impl Into<String>) -> Self {
77        Self {
78            image: image.into(),
79            ..Default::default()
80        }
81    }
82
83    /// Set the sandbox name.
84    pub fn name(mut self, name: impl Into<String>) -> Self {
85        self.name = Some(name.into());
86        self
87    }
88
89    /// Add an environment variable.
90    pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
91        self.environment.insert(key.into(), value.into());
92        self
93    }
94
95    /// Add a label.
96    pub fn label(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
97        self.labels.insert(key.into(), value.into());
98        self
99    }
100}
101
102/// File or directory metadata.
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct FileInfo {
105    /// File name.
106    pub name: String,
107    /// Full path.
108    pub path: String,
109    /// File size in bytes.
110    pub size: u64,
111    /// File mode (permissions).
112    pub mode: String,
113    /// Modification time.
114    pub mod_time: String,
115    /// Whether this is a directory.
116    pub is_dir: bool,
117}
118
119/// Terminal resize message.
120#[derive(Debug, Clone, Serialize)]
121pub struct ResizeMessage {
122    #[serde(rename = "type")]
123    pub msg_type: String,
124    pub cols: u16,
125    pub rows: u16,
126}
127
128impl ResizeMessage {
129    pub fn new(cols: u16, rows: u16) -> Self {
130        Self {
131            msg_type: "resize".into(),
132            cols,
133            rows,
134        }
135    }
136}