1pub const PLUGIN_WIT: &str = include_str!("../wit/camel-plugin.wit");
3
4pub const BEAN_WIT: &str = include_str!("../wit/camel-bean.wit");
6
7pub const FULL_WIT: &str = include_str!("../wit/camel-all.wit");
9
10pub const APPLICATION_JSON: &str = "application/json";
18
19pub const TEXT_PLAIN: &str = "text/plain";
21
22pub const APPLICATION_OCTET_STREAM: &str = "application/octet-stream";
24
25pub const TEXT_HTML: &str = "text/html";
27
28pub const APPLICATION_XML: &str = "application/xml";
30
31pub const APPLICATION_FORM_URLENCODED: &str = "application/x-www-form-urlencoded";
33
34pub fn wit_dir() -> &'static std::path::Path {
54 std::path::Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/wit"))
55}
56
57use std::sync::atomic::{AtomicUsize, Ordering};
58
59use camel_api::CamelError;
60
61const DEFAULT_MAX_RESOURCES: usize = 1000;
63
64#[derive(Debug)]
72pub struct WitHost {
73 max_resources: usize,
74 allocation_count: AtomicUsize,
75}
76
77impl WitHost {
78 pub fn new() -> Self {
80 Self::with_max_resources(DEFAULT_MAX_RESOURCES)
81 }
82
83 pub fn with_max_resources(max: usize) -> Self {
85 Self {
86 max_resources: max,
87 allocation_count: AtomicUsize::new(0),
88 }
89 }
90
91 pub fn allocate(&self, _name: &str) -> Result<(), CamelError> {
97 let mut current = self.allocation_count.load(Ordering::Relaxed);
98 loop {
99 if current >= self.max_resources {
100 return Err(CamelError::ProcessorError("resource limit exceeded".into()));
101 }
102 match self.allocation_count.compare_exchange_weak(
103 current,
104 current + 1,
105 Ordering::AcqRel,
106 Ordering::Relaxed,
107 ) {
108 Ok(_) => return Ok(()),
109 Err(actual) => current = actual,
110 }
111 }
112 }
113
114 pub fn deallocate(&self, _name: &str) {
116 self.allocation_count.fetch_sub(1, Ordering::AcqRel);
117 }
118
119 pub fn resource_count(&self) -> usize {
121 self.allocation_count.load(Ordering::Acquire)
122 }
123
124 pub fn max_resources(&self) -> usize {
126 self.max_resources
127 }
128}
129
130impl Default for WitHost {
131 fn default() -> Self {
132 Self::new()
133 }
134}
135
136#[cfg(test)]
137mod tests {
138 use super::*;
139
140 #[test]
141 fn test_wit_host_rejects_beyond_max_resources() {
142 let host = WitHost::with_max_resources(3);
143 host.allocate("a").unwrap();
144 host.allocate("b").unwrap();
145 host.allocate("c").unwrap();
146 let result = host.allocate("d"); assert!(result.is_err());
148 }
149
150 #[test]
151 fn test_wit_host_default_limit_is_1000() {
152 let host = WitHost::new();
153 assert_eq!(host.max_resources(), 1000);
154 }
155
156 #[test]
157 fn test_wit_host_allows_up_to_limit() {
158 let host = WitHost::with_max_resources(2);
159 assert!(host.allocate("x").is_ok());
160 assert!(host.allocate("y").is_ok());
161 assert!(host.allocate("z").is_err());
162 }
163
164 #[test]
165 fn test_wit_host_deallocate_frees_slot() {
166 let host = WitHost::with_max_resources(1);
167 host.allocate("a").unwrap();
168 assert!(host.allocate("b").is_err());
169 host.deallocate("a");
170 assert!(host.allocate("b").is_ok());
171 }
172
173 #[test]
174 fn test_wit_host_resource_count_tracks_allocations() {
175 let host = WitHost::new();
176 assert_eq!(host.resource_count(), 0);
177 host.allocate("a").unwrap();
178 host.allocate("b").unwrap();
179 assert_eq!(host.resource_count(), 2);
180 host.deallocate("a");
181 assert_eq!(host.resource_count(), 1);
182 }
183
184 #[test]
185 fn test_wit_host_error_is_processor_error() {
186 let host = WitHost::with_max_resources(1);
187 host.allocate("a").unwrap();
188 let err = host.allocate("b").unwrap_err();
189 assert!(matches!(err, CamelError::ProcessorError(_)));
190 assert!(err.to_string().contains("resource limit exceeded"));
191 }
192
193 #[test]
196 fn test_wit_dir_exists() {
197 let dir = wit_dir();
198 assert!(
199 dir.exists(),
200 "wit_dir() should point to an existing directory"
201 );
202 assert!(dir.is_dir(), "wit_dir() should be a directory");
203 }
204
205 #[test]
206 fn test_wit_dir_contains_expected_files() {
207 let dir = wit_dir();
208 assert!(
209 dir.join("camel-plugin.wit").exists(),
210 "camel-plugin.wit missing"
211 );
212 assert!(
213 dir.join("camel-bean.wit").exists(),
214 "camel-bean.wit missing"
215 );
216 assert!(dir.join("camel-all.wit").exists(), "camel-all.wit missing");
217 }
218
219 #[test]
220 fn test_plugin_wit_is_non_empty() {
221 assert!(!PLUGIN_WIT.is_empty(), "PLUGIN_WIT should not be empty");
222 }
223
224 #[test]
225 fn test_bean_wit_is_non_empty() {
226 assert!(!BEAN_WIT.is_empty(), "BEAN_WIT should not be empty");
227 }
228
229 #[test]
230 fn test_full_wit_is_non_empty() {
231 assert!(!FULL_WIT.is_empty(), "FULL_WIT should not be empty");
232 }
233
234 #[test]
235 fn test_wit_constants_contain_package_declaration() {
236 assert!(PLUGIN_WIT.contains("package camel:plugin"));
237 assert!(BEAN_WIT.contains("package camel:plugin"));
238 assert!(FULL_WIT.contains("package camel:plugin"));
239 }
240
241 #[test]
242 fn test_wit_exchange_has_route_and_message_id_fields() {
243 assert!(
245 FULL_WIT.contains("route-id"),
246 "wasm-exchange should contain route-id field"
247 );
248 assert!(
249 FULL_WIT.contains("message-id"),
250 "wasm-exchange should contain message-id field"
251 );
252 assert!(
253 PLUGIN_WIT.contains("route-id"),
254 "plugin WIT should contain route-id field"
255 );
256 assert!(
257 PLUGIN_WIT.contains("message-id"),
258 "plugin WIT should contain message-id field"
259 );
260 }
261
262 #[test]
263 fn test_content_type_constants_compile() {
264 assert_eq!(APPLICATION_JSON, "application/json");
266 assert_eq!(TEXT_PLAIN, "text/plain");
267 assert_eq!(APPLICATION_OCTET_STREAM, "application/octet-stream");
268 assert_eq!(TEXT_HTML, "text/html");
269 assert_eq!(APPLICATION_XML, "application/xml");
270 assert_eq!(
271 APPLICATION_FORM_URLENCODED,
272 "application/x-www-form-urlencoded"
273 );
274 }
275}