Mountain/ApplicationState/DTO/
TerminalStateDTO.rs

1//! # TerminalStateDTO
2//!
3//! Defines the Data Transfer Object for storing the state of a single active
4//! integrated terminal instance.
5
6#![allow(non_snake_case, non_camel_case_types)]
7
8use std::{collections::HashMap, path::PathBuf, sync::Arc};
9
10use serde_json::Value;
11use tokio::{
12	sync::{Mutex as TokioMutex, mpsc as TokioMPSC},
13	task::JoinHandle,
14};
15
16/// Holds the complete state and runtime resources for a single pseudo-terminal
17/// (PTY) instance. This includes configuration, process identifiers, and
18/// handles for I/O tasks.
19#[derive(Debug, Clone)]
20pub struct TerminalStateDTO {
21	// --- Identifiers ---
22	pub Identifier:u64,
23
24	pub Name:String,
25
26	pub OSProcessIdentifier:Option<u32>,
27
28	// --- Configuration ---
29	pub ShellPath:String,
30
31	pub ShellArguments:Vec<String>,
32
33	pub CurrentWorkingDirectory:Option<PathBuf>,
34
35	pub EnvironmentVariables:Option<HashMap<String, Option<String>>>,
36
37	pub IsPTY:bool,
38
39	// --- Runtime Handles ---
40	pub PTYInputTransmitter:Option<TokioMPSC::Sender<String>>,
41
42	pub ReaderTaskHandle:Option<Arc<TokioMutex<Option<JoinHandle<()>>>>>,
43
44	pub ProcessWaitHandle:Option<Arc<TokioMutex<Option<JoinHandle<()>>>>>,
45}
46
47impl TerminalStateDTO {
48	/// Creates a new `TerminalStateDTO` by parsing terminal options from a
49	/// `serde_json::Value`.
50	pub fn Create(Identifier:u64, Name:String, OptionsValue:&Value, DefaultShellPath:String) -> Self {
51		let ShellPath = OptionsValue
52			.get("shellPath")
53			.and_then(Value::as_str)
54			.unwrap_or(&DefaultShellPath)
55			.to_string();
56
57		let ShellArguments = match OptionsValue.get("shellArgs") {
58			Some(Value::Array(Array)) => Array.iter().filter_map(Value::as_str).map(String::from).collect(),
59
60			_ => Vec::new(),
61		};
62
63		let CWD = OptionsValue.get("cwd").and_then(Value::as_str).map(PathBuf::from);
64
65		// A more complete implementation would parse the `env` object.
66		let EnvVars = None;
67
68		Self {
69			Identifier,
70
71			Name,
72
73			ShellPath,
74
75			ShellArguments,
76
77			CurrentWorkingDirectory:CWD,
78
79			EnvironmentVariables:EnvVars,
80
81			OSProcessIdentifier:None,
82
83			// Assume all terminals are PTYs for now
84			IsPTY:true,
85
86			PTYInputTransmitter:None,
87
88			ReaderTaskHandle:None,
89
90			ProcessWaitHandle:None,
91		}
92	}
93}