camel-core 0.14.0

Core engine for rust-camel
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use std::fs;
use std::path::{Path, PathBuf};

fn collect_rust_files(dir: &Path, out: &mut Vec<PathBuf>) {
    let entries = fs::read_dir(dir).expect("failed to read directory");
    for entry in entries {
        let entry = entry.expect("failed to read directory entry");
        let path = entry.path();
        if path.is_dir() {
            collect_rust_files(&path, out);
        } else if path.extension().is_some_and(|ext| ext == "rs") {
            out.push(path);
        }
    }
}

fn assert_no_forbidden_imports(layer_dir: &str, forbidden: &[&str]) {
    let root = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("src")
        .join(layer_dir);
    let mut files = Vec::new();
    collect_rust_files(&root, &mut files);
    for file in files {
        let content = fs::read_to_string(&file).expect("failed to read source file");
        for pattern in forbidden {
            assert!(
                !content.contains(pattern),
                "hexagonal boundary violation in {}: found forbidden import pattern `{}`",
                file.display(),
                pattern
            );
        }
    }
}

fn assert_file_contains(path: &Path, required: &[&str]) {
    let content = fs::read_to_string(path).expect("failed to read source file");
    for pattern in required {
        assert!(
            content.contains(pattern),
            "expected pattern `{}` in {}",
            pattern,
            path.display()
        );
    }
}

fn assert_file_not_contains(path: &Path, forbidden: &[&str]) {
    let content = fs::read_to_string(path).expect("failed to read source file");
    for pattern in forbidden {
        assert!(
            !content.contains(pattern),
            "found forbidden pattern `{}` in {}",
            pattern,
            path.display()
        );
    }
}

fn assert_file_prefix_not_contains(path: &Path, split_at: &str, forbidden: &[&str]) {
    let content = fs::read_to_string(path).expect("failed to read source file");
    let prefix = match content.split_once(split_at) {
        Some((before, _)) => before,
        None => content.as_str(),
    };

    for pattern in forbidden {
        assert!(
            !prefix.contains(pattern),
            "found forbidden pattern `{}` in {} before `{}`",
            pattern,
            path.display(),
            split_at
        );
    }
}

fn assert_file_section_contains(path: &Path, section_start: &str, required: &[&str]) {
    let content = fs::read_to_string(path).expect("failed to read source file");
    let section = content
        .split(section_start)
        .nth(1)
        .unwrap_or_else(|| panic!("missing section `{section_start}` in {}", path.display()));
    let section = section.split("#[cfg(test)]").next().unwrap_or(section);
    for pattern in required {
        assert!(
            section.contains(pattern),
            "expected pattern `{}` in section `{}` of {}",
            pattern,
            section_start,
            path.display()
        );
    }
}

fn assert_file_section_not_contains(path: &Path, section_start: &str, forbidden: &[&str]) {
    let content = fs::read_to_string(path).expect("failed to read source file");
    let section = content
        .split(section_start)
        .nth(1)
        .unwrap_or_else(|| panic!("missing section `{section_start}` in {}", path.display()));
    let section = section.split("#[cfg(test)]").next().unwrap_or(section);
    for pattern in forbidden {
        assert!(
            !section.contains(pattern),
            "found forbidden pattern `{}` in section `{}` of {}",
            pattern,
            section_start,
            path.display()
        );
    }
}

#[test]
fn domain_layer_has_no_infrastructure_dependencies() {
    assert_no_forbidden_imports(
        "lifecycle/domain",
        &[
            "crate::lifecycle::adapters",
            "crate::lifecycle::application",
            "crate::context",
            "crate::reload",
            "crate::route_controller",
            "crate::supervising_route_controller",
        ],
    );
}

#[test]
fn application_layer_depends_on_ports_and_domain_only() {
    assert_no_forbidden_imports(
        "lifecycle/application",
        &[
            "crate::context",
            "crate::reload",
            "crate::route_controller",
            "crate::supervising_route_controller",
        ],
    );
}

#[test]
fn ports_layer_only_uses_approved_application_imports() {
    assert_no_forbidden_imports(
        "lifecycle/ports",
        &[
            "crate::lifecycle::adapters",
            "crate::context",
            "crate::reload",
            "crate::route_controller",
            "crate::supervising_route_controller",
            "crate::lifecycle::application::commands",
            "crate::lifecycle::application::runtime_bus",
            "crate::lifecycle::application::queries",
            // `internal_commands` module was removed; keep guarding concrete app internals that still exist.
            "crate::lifecycle::application::supervision_service",
        ],
    );
}

#[test]
fn domain_and_ports_do_not_import_runtime_contract_types_from_camel_api_directly() {
    let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
    let route_runtime = root.join("lifecycle/domain/route_runtime.rs");
    let runtime_ports = root.join("lifecycle/ports/runtime_ports.rs");

    assert_file_not_contains(
        &route_runtime,
        &["use camel_api::{CamelError, RuntimeEvent};"],
    );
    assert_file_not_contains(
        &runtime_ports,
        &[
            "use camel_api::{CamelError, RuntimeEvent};",
            "use crate::route::RouteDefinition;",
        ],
    );
    assert_file_contains(
        &runtime_ports,
        &[
            "use crate::lifecycle::application::route_definition::RouteDefinition",
            "use crate::lifecycle::domain::{RouteRuntimeAggregate, RuntimeEvent}",
        ],
    );
    assert_file_not_contains(
        &runtime_ports,
        &["use crate::lifecycle::domain::{RouteDefinition"],
    );
}

#[test]
fn runtime_bus_and_command_handlers_are_controller_agnostic() {
    let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
    let runtime_bus = root.join("lifecycle/application/runtime_bus.rs");
    let commands = root.join("lifecycle/application/commands.rs");

    assert_file_not_contains(
        &runtime_bus,
        &[
            "RouteControllerInternal",
            "with_controller(",
            "route_status(",
        ],
    );
    assert_file_not_contains(
        &commands,
        &[
            "RouteControllerInternal",
            "with_controller(",
            "route_status(",
        ],
    );
}

#[test]
fn runtime_side_effects_flow_through_execution_port() {
    let commands = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("src")
        .join("lifecycle/application/commands.rs");

    assert_file_contains(
        &commands,
        &[
            "RuntimeExecutionPort",
            "pub execution: Option<Arc<dyn RuntimeExecutionPort>>",
            "if let Some(execution) = &deps.execution",
        ],
    );
}

#[test]
fn reload_runtime_path_does_not_use_controller_local_status_heuristics() {
    let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
    let reload_watcher = root.join("hot_reload/adapters/reload_watcher.rs");
    let reload = root.join("hot_reload/application/reload.rs");

    assert_file_contains(&reload_watcher, &["runtime_route_ids()"]);
    assert_file_contains(&reload, &["runtime_route_status("]);
    assert_file_contains(
        &reload,
        &["pub(crate) fn compute_reload_actions_from_runtime_snapshot"],
    );
    assert_file_contains(&reload, &["pub async fn execute_reload_actions"]);
    assert_file_prefix_not_contains(&reload, "#[cfg(test)]", &[".route_status("]);
}

#[test]
fn supervision_loop_decisions_are_runtime_query_driven() {
    let supervising = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("src")
        .join("lifecycle")
        .join("adapters")
        .join("controller_actor.rs");

    assert_file_section_contains(
        &supervising,
        "pub fn spawn_supervision_task(",
        &[
            "controller: RouteControllerHandle",
            "controller.restart_route(route_id.clone()).await",
        ],
    );
    assert_file_section_not_contains(
        &supervising,
        "pub fn spawn_supervision_task(",
        &[
            "RuntimeQuery::GetRouteStatus",
            "RuntimeCommand::FailRoute",
            "RuntimeCommand::ReloadRoute",
            ".route_status(",
        ],
    );
}

#[test]
fn runtime_execution_handle_has_no_raw_controller_escape_hatch() {
    let context = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("src")
        .join("context.rs");
    assert_file_not_contains(&context, &["fn raw(&self)"]);
    assert_file_contains(
        &context,
        &["ProducerContext::new().with_runtime(self.runtime())"],
    );
    assert_file_not_contains(&context, &["ProducerContext::new(route_controller)"]);
}

#[test]
fn public_route_controller_trait_exposes_no_lifecycle_read_model() {
    let route_controller_api = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("..")
        .join("camel-api")
        .join("src")
        .join("route_controller.rs");
    assert_file_not_contains(&route_controller_api, &["fn route_status("]);

    let core_lib = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("src")
        .join("lib.rs");
    assert_file_not_contains(
        &core_lib,
        &["pub use route_controller::{DefaultRouteController, RouteControllerInternal};"],
    );
}

#[test]
fn runtime_execution_adapter_uses_semantic_executor_naming() {
    let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
    let adapters_mod = root.join("lifecycle/adapters/mod.rs");
    let context = root.join("context.rs");

    assert_file_contains(&adapters_mod, &["RuntimeExecutionAdapter"]);
    assert_file_contains(&context, &["RuntimeExecutionAdapter::new("]);
    assert_file_not_contains(&context, &["RouteControllerExecutionAdapter::new("]);
}

#[test]
fn domain_has_no_tower_or_framework_types() {
    let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("src/lifecycle/domain");
    let mut files = Vec::new();
    collect_rust_files(&root, &mut files);

    let forbidden = &[
        "tower::",
        "BoxProcessor",
        "compose_pipeline",
        "TracingProcessor",
        "use camel_component_api::",
    ];

    for file in &files {
        let content = std::fs::read_to_string(file).expect("failed to read source file");
        for pattern in forbidden {
            assert!(
                !content.contains(pattern),
                "domain boundary violation in {}: found forbidden pattern `{}`",
                file.display(),
                pattern
            );
        }
    }
}

#[test]
fn ports_imports_route_definition_from_application_not_adapters() {
    let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
    let ports_file = root.join("lifecycle/ports/runtime_ports.rs");

    assert_file_contains(
        &ports_file,
        &["use crate::lifecycle::application::route_definition::RouteDefinition"],
    );

    assert_file_not_contains(&ports_file, &["crate::lifecycle::adapters::"]);
}

#[test]
fn domain_does_not_import_application_or_adapters() {
    assert_no_forbidden_imports(
        "lifecycle/domain",
        &[
            "crate::lifecycle::application::",
            "crate::lifecycle::adapters::",
        ],
    );
}

#[test]
fn lifecycle_domain_does_not_import_hot_reload() {
    assert_no_forbidden_imports("lifecycle/domain", &["crate::hot_reload", "hot_reload::"]);
}

#[test]
fn lifecycle_domain_does_not_import_lifecycle_application_or_adapters() {
    assert_no_forbidden_imports(
        "lifecycle/domain",
        &[
            "crate::lifecycle::application",
            "crate::lifecycle::adapters",
        ],
    );
}

#[test]
fn lifecycle_application_does_not_import_hot_reload() {
    assert_no_forbidden_imports(
        "lifecycle/application",
        &["crate::hot_reload", "hot_reload::"],
    );
}

#[test]
fn hot_reload_does_not_import_lifecycle_domain_directly() {
    // hot_reload may use lifecycle application/adapters types directly (by design),
    // but must never bypass the layering by importing raw domain types.
    assert_no_forbidden_imports("hot_reload", &["crate::lifecycle::domain"]);
}

#[test]
fn lifecycle_ports_registration_port_is_pub_crate_only() {
    let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("src/lifecycle/ports/registration_port.rs");
    let content = std::fs::read_to_string(&path).expect("failed to read registration_port.rs");
    assert!(
        content.contains("pub(crate) trait RouteRegistrationPort"),
        "RouteRegistrationPort must be pub(crate), not pub"
    );
    assert!(
        !content.contains("pub trait RouteRegistrationPort"),
        "RouteRegistrationPort must NOT be pub (only pub(crate))"
    );
}

#[test]
fn lifecycle_does_not_use_old_flat_config_path() {
    assert_no_forbidden_imports(
        "lifecycle",
        &["crate::config::", "crate::tracer::", "crate::registry::"],
    );
}

#[test]
fn hot_reload_does_not_use_old_flat_config_path() {
    assert_no_forbidden_imports(
        "hot_reload",
        &["crate::config::", "crate::tracer::", "crate::registry::"],
    );
}