lenso_process_protocol/
execution_target.rs1use serde::{Deserialize, Serialize, de::Error as _};
10
11use super::ProtocolError;
12
13pub const EXECUTION_TARGET_CAPABILITY_PROFILE: &str = "lenso.execution-target-capability-profile@1";
15
16#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize)]
22pub enum ExecutionTargetCapability {
23 #[serde(rename = "request")]
25 Request,
26 #[serde(rename = "stream")]
28 Stream,
29 #[serde(rename = "event")]
31 Event,
32 #[serde(rename = "websocket")]
34 WebSocket,
35 #[serde(rename = "host-imports")]
37 HostImports,
38 #[serde(rename = "native-process")]
40 NativeProcess,
41 #[serde(rename = "wasm-component")]
43 WasmComponent,
44 #[serde(rename = "remote")]
46 Remote,
47 #[serde(rename = "browser")]
49 Browser,
50 #[serde(rename = "workers")]
52 Workers,
53}
54
55impl ExecutionTargetCapability {
56 pub const ALL: [Self; 10] = [
58 Self::Browser,
59 Self::Event,
60 Self::HostImports,
61 Self::NativeProcess,
62 Self::Remote,
63 Self::Request,
64 Self::Stream,
65 Self::WasmComponent,
66 Self::WebSocket,
67 Self::Workers,
68 ];
69
70 #[must_use]
72 pub const fn as_str(self) -> &'static str {
73 match self {
74 Self::Request => "request",
75 Self::Stream => "stream",
76 Self::Event => "event",
77 Self::WebSocket => "websocket",
78 Self::HostImports => "host-imports",
79 Self::NativeProcess => "native-process",
80 Self::WasmComponent => "wasm-component",
81 Self::Remote => "remote",
82 Self::Browser => "browser",
83 Self::Workers => "workers",
84 }
85 }
86
87 #[must_use]
89 pub fn from_name(value: &str) -> Option<Self> {
90 match value {
91 "request" => Some(Self::Request),
92 "stream" => Some(Self::Stream),
93 "event" => Some(Self::Event),
94 "websocket" => Some(Self::WebSocket),
95 "host-imports" => Some(Self::HostImports),
96 "native-process" => Some(Self::NativeProcess),
97 "wasm-component" => Some(Self::WasmComponent),
98 "remote" => Some(Self::Remote),
99 "browser" => Some(Self::Browser),
100 "workers" => Some(Self::Workers),
101 _ => None,
102 }
103 }
104}
105
106impl<'de> Deserialize<'de> for ExecutionTargetCapability {
107 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
108 where
109 D: serde::Deserializer<'de>,
110 {
111 let value = String::deserialize(deserializer)?;
112 Self::from_name(&value)
113 .ok_or_else(|| D::Error::custom("unknown execution target capability"))
114 }
115}
116
117#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
123#[serde(deny_unknown_fields)]
124pub struct ExecutionTargetCapabilityProfile {
125 pub profile: String,
127 pub target_profile: String,
129 pub capabilities: Vec<ExecutionTargetCapability>,
132}
133
134impl ExecutionTargetCapabilityProfile {
135 pub fn validate(&self) -> Result<(), ProtocolError> {
138 if self.profile != EXECUTION_TARGET_CAPABILITY_PROFILE {
139 return Err(ProtocolError::new(
140 "unsupported execution target capability profile",
141 ));
142 }
143 validate_target_profile(&self.target_profile)?;
144 if self
145 .capabilities
146 .windows(2)
147 .any(|pair| pair[0].as_str() >= pair[1].as_str())
148 {
149 return Err(ProtocolError::new(
150 "capabilities must be strictly canonical-sorted and unique",
151 ));
152 }
153 Ok(())
154 }
155
156 #[must_use]
161 pub fn supports(&self, capability: ExecutionTargetCapability) -> bool {
162 self.validate().is_ok() && self.capabilities.contains(&capability)
163 }
164
165 #[must_use]
168 pub fn supports_named(&self, capability: &str) -> bool {
169 ExecutionTargetCapability::from_name(capability).is_some_and(|known| self.supports(known))
170 }
171
172 #[must_use]
177 pub fn missing_capabilities<'a>(
178 &self,
179 required: impl IntoIterator<Item = &'a str>,
180 ) -> Vec<String> {
181 required
182 .into_iter()
183 .filter(|capability| !self.supports_named(capability))
184 .map(ToOwned::to_owned)
185 .collect()
186 }
187}
188
189fn validate_target_profile(value: &str) -> Result<(), ProtocolError> {
190 if value.is_empty()
191 || value.len() > 128
192 || !value
193 .bytes()
194 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-' | b'@'))
195 {
196 return Err(ProtocolError::new(
197 "target_profile must be a 1..=128 byte portable token",
198 ));
199 }
200 Ok(())
201}