Skip to main content

rexec/
types.rs

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