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    #[serde(default)]
25    pub name: String,
26    /// Docker image.
27    #[serde(default)]
28    pub image: String,
29    /// Current status.
30    #[serde(default)]
31    pub status: String,
32    /// Creation timestamp.
33    #[serde(default)]
34    pub created_at: String,
35    /// Start timestamp (if running).
36    #[serde(default)]
37    pub started_at: Option<String>,
38    /// Container labels.
39    #[serde(default)]
40    pub labels: HashMap<String, String>,
41    /// Environment variables.
42    #[serde(default)]
43    pub environment: HashMap<String, String>,
44}
45
46/// Request to create a new container.
47#[derive(Debug, Clone, Serialize, Default)]
48pub struct CreateContainerRequest {
49    /// Docker image to use.
50    pub image: String,
51    /// Optional container name.
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub name: Option<String>,
54    /// Environment variables.
55    #[serde(skip_serializing_if = "HashMap::is_empty", default)]
56    pub environment: HashMap<String, String>,
57    /// Container labels.
58    #[serde(skip_serializing_if = "HashMap::is_empty", default)]
59    pub labels: HashMap<String, String>,
60}
61
62impl CreateContainerRequest {
63    /// Create a new request with the given image.
64    pub fn new(image: impl Into<String>) -> Self {
65        Self {
66            image: image.into(),
67            ..Default::default()
68        }
69    }
70
71    /// Set the container name.
72    pub fn name(mut self, name: impl Into<String>) -> Self {
73        self.name = Some(name.into());
74        self
75    }
76
77    /// Add an environment variable.
78    pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
79        self.environment.insert(key.into(), value.into());
80        self
81    }
82
83    /// Add a label.
84    pub fn label(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
85        self.labels.insert(key.into(), value.into());
86        self
87    }
88}
89
90/// File or directory metadata.
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct FileInfo {
93    /// File name.
94    pub name: String,
95    /// Full path.
96    pub path: String,
97    /// File size in bytes.
98    pub size: u64,
99    /// File mode (permissions).
100    pub mode: String,
101    /// Modification time.
102    pub mod_time: String,
103    /// Whether this is a directory.
104    pub is_dir: bool,
105}
106
107/// Terminal resize message.
108#[derive(Debug, Clone, Serialize)]
109pub struct ResizeMessage {
110    #[serde(rename = "type")]
111    pub msg_type: String,
112    pub cols: u16,
113    pub rows: u16,
114}
115
116impl ResizeMessage {
117    pub fn new(cols: u16, rows: u16) -> Self {
118        Self {
119            msg_type: "resize".into(),
120            cols,
121            rows,
122        }
123    }
124}