Skip to main content

camel_api/
reserved_suffix.rs

1//! Reserved document-suffix predicates (ADR-0062 single suffix rule).
2//!
3//! This is the single definition of the reserved-suffix rule, defined here
4//! in `camel-api` (below the lint/runtime hex boundary) so that both
5//! `camel-dsl` (runtime route discovery) and `camel-lint` consume one
6//! definition. `camel_dsl::discovery` re-exports these predicates, so
7//! existing `camel_dsl::discovery::*` import paths keep resolving to the
8//! same functions.
9
10use std::path::Path;
11
12/// Returns true if the file name ends with the reserved `.test.yaml` or
13/// `.test.yml` suffix. Such files name camel test documents (the
14/// `camel test` family), not routes.
15pub fn is_test_document(path: &Path) -> bool {
16    path.file_name().is_some_and(|name| {
17        let name = name.to_string_lossy();
18        name.ends_with(".test.yaml") || name.ends_with(".test.yml")
19    })
20}
21
22/// Returns true if the file name ends with the reserved `.job.yaml` or
23/// `.job.yml` suffix. Such files name camel job documents (the
24/// `camel job` family), not routes.
25pub fn is_job_document(path: &Path) -> bool {
26    path.file_name().is_some_and(|name| {
27        let name = name.to_string_lossy();
28        name.ends_with(".job.yaml") || name.ends_with(".job.yml")
29    })
30}
31
32/// Returns true if the file name ends with any reserved document suffix:
33/// `.test.yaml`/`.test.yml` (owned by `camel test`) or
34/// `.job.yaml`/`.job.yml` (owned by `camel job`). Such files are never
35/// routes; route discovery skips them under wildcard globs and errors on
36/// literal naming.
37pub fn is_reserved_document(path: &Path) -> bool {
38    is_test_document(path) || is_job_document(path)
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn test_suffixes_match() {
47        assert!(is_test_document(Path::new("a.test.yaml")));
48        assert!(is_test_document(Path::new("a.test.yml")));
49        assert!(is_job_document(Path::new("a.job.yaml")));
50        assert!(is_job_document(Path::new("a.job.yml")));
51        assert!(is_reserved_document(Path::new("a.test.yaml")));
52        assert!(is_reserved_document(Path::new("a.test.yml")));
53        assert!(is_reserved_document(Path::new("a.job.yaml")));
54        assert!(is_reserved_document(Path::new("a.job.yml")));
55    }
56
57    #[test]
58    fn non_reserved_names_rejected() {
59        assert!(!is_test_document(Path::new("atest.yaml")));
60        assert!(!is_job_document(Path::new("ajob.yaml")));
61        assert!(!is_reserved_document(Path::new("atest.yaml")));
62        assert!(!is_reserved_document(Path::new("ajob.yaml")));
63        assert!(!is_test_document(Path::new("a.yaml")));
64        assert!(!is_job_document(Path::new("a.yaml")));
65        assert!(!is_reserved_document(Path::new("a.yaml")));
66        assert!(!is_test_document(Path::new("x.test.json")));
67        assert!(!is_job_document(Path::new("x.test.json")));
68        assert!(!is_reserved_document(Path::new("x.test.json")));
69        assert!(!is_test_document(Path::new("x.job.json")));
70        assert!(!is_job_document(Path::new("x.job.json")));
71        assert!(!is_reserved_document(Path::new("x.job.json")));
72    }
73}