apcore-toolkit 0.7.0

Shared scanner, schema extraction, and output toolkit for apcore framework adapters
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// Built-in verifiers for output writers.
//
// Each verifier implements the Verifier trait and checks a specific
// aspect of a written artifact.

use std::fs;
use std::panic::{catch_unwind, AssertUnwindSafe};

use crate::output::types::{Verifier, VerifyResult};

/// Verify that a Rust source file has valid syntax.
///
/// Uses the `syn` crate to parse the file as a Rust source file.
/// Analogous to `SyntaxVerifier` in Python (which uses `ast.parse`)
/// and TypeScript (which does a basic readability check).
pub struct SyntaxVerifier;

impl Verifier for SyntaxVerifier {
    fn verify(&self, path: &str, _module_id: &str) -> VerifyResult {
        if path.is_empty() {
            return VerifyResult::ok();
        }
        let content = match fs::read_to_string(path) {
            Ok(c) => c,
            Err(e) => return VerifyResult::fail(format!("Cannot read file: {e}")),
        };

        if content.trim().is_empty() {
            return VerifyResult::fail("File is empty".into());
        }

        match syn::parse_file(&content) {
            Ok(_) => VerifyResult::ok(),
            Err(e) => VerifyResult::fail(format!("Invalid Rust syntax: {e}")),
        }
    }
}

/// Verify that a YAML binding file is parseable and contains required fields.
pub struct YAMLVerifier;

impl Verifier for YAMLVerifier {
    fn verify(&self, path: &str, _module_id: &str) -> VerifyResult {
        if path.is_empty() {
            return VerifyResult::ok();
        }
        let content = match fs::read_to_string(path) {
            Ok(c) => c,
            Err(e) => return VerifyResult::fail(format!("Cannot read file: {e}")),
        };

        let parsed: serde_yaml_ng::Value = match serde_yaml_ng::from_str(&content) {
            Ok(v) => v,
            Err(e) => return VerifyResult::fail(format!("Invalid YAML: {e}")),
        };

        let bindings = match parsed.get("bindings") {
            Some(b) => b,
            None => return VerifyResult::fail("Missing or empty 'bindings' list".into()),
        };

        let bindings_seq = match bindings.as_sequence() {
            Some(s) if !s.is_empty() => s,
            _ => return VerifyResult::fail("Missing or empty 'bindings' list".into()),
        };

        for (i, entry) in bindings_seq.iter().enumerate() {
            for field in &["module_id", "target"] {
                match entry.get(*field) {
                    Some(serde_yaml_ng::Value::String(s)) if !s.trim().is_empty() => {}
                    _ => {
                        return VerifyResult::fail(format!(
                            "Entry {i}: missing or invalid '{field}' field"
                        ))
                    }
                }
            }
        }

        VerifyResult::ok()
    }
}

/// Verify that a file contains valid JSON, with optional schema validation.
pub struct JSONVerifier {
    // Schema validation is omitted (would require jsonschema crate).
    // Only checks that the file is valid JSON.
}

impl JSONVerifier {
    /// Create a new JSONVerifier.
    pub fn new() -> Self {
        Self {}
    }
}

impl Default for JSONVerifier {
    fn default() -> Self {
        Self::new()
    }
}

impl Verifier for JSONVerifier {
    fn verify(&self, path: &str, _module_id: &str) -> VerifyResult {
        if path.is_empty() {
            return VerifyResult::ok();
        }
        let content = match fs::read_to_string(path) {
            Ok(c) => c,
            Err(e) => return VerifyResult::fail(format!("Cannot read file: {e}")),
        };

        match serde_json::from_str::<serde_json::Value>(&content) {
            Ok(_) => VerifyResult::ok(),
            Err(e) => VerifyResult::fail(format!("Invalid JSON: {e}")),
        }
    }
}

/// Verify that a file starts with expected magic bytes.
pub struct MagicBytesVerifier {
    expected: Vec<u8>,
}

impl MagicBytesVerifier {
    /// Create a new MagicBytesVerifier with the expected byte sequence.
    pub fn new(expected: Vec<u8>) -> Self {
        Self { expected }
    }
}

impl Verifier for MagicBytesVerifier {
    fn verify(&self, path: &str, _module_id: &str) -> VerifyResult {
        if path.is_empty() {
            return VerifyResult::ok();
        }
        let content = match fs::read(path) {
            Ok(c) => c,
            Err(e) => return VerifyResult::fail(format!("Cannot read file: {e}")),
        };

        if content.len() < self.expected.len() {
            return VerifyResult::fail(format!(
                "File too short: expected at least {} bytes, got {}",
                self.expected.len(),
                content.len()
            ));
        }

        let header = &content[..self.expected.len()];
        if header != self.expected.as_slice() {
            return VerifyResult::fail(format!(
                "Magic bytes mismatch: expected {:?}, got {:?}",
                self.expected, header
            ));
        }

        VerifyResult::ok()
    }
}

/// Verify that a module is registered and retrievable from a registry.
pub struct RegistryVerifier<'a> {
    registry: &'a apcore::Registry,
}

impl<'a> RegistryVerifier<'a> {
    /// Create a new RegistryVerifier checking against the given registry.
    pub fn new(registry: &'a apcore::Registry) -> Self {
        Self { registry }
    }
}

impl Verifier for RegistryVerifier<'_> {
    fn verify(&self, _path: &str, module_id: &str) -> VerifyResult {
        if self.registry.has(module_id) {
            VerifyResult::ok()
        } else {
            VerifyResult::fail(format!(
                "Module '{module_id}' not found in registry after registration"
            ))
        }
    }
}

/// Run verifiers in order; stop on first failure.
///
/// Each verifier call is wrapped in `catch_unwind` so that a panicking
/// verifier does not crash the caller. A panic is reported as a
/// `VerifyResult::fail` with the spec-mandated literal prefix
/// `"Verifier crashed:"` so cross-language callers can match the same
/// prefix produced by the Python and TypeScript SDKs.
///
/// Cross-language note: Python and TypeScript additionally append the
/// concrete verifier class name to the crash message for diagnosability
/// (e.g. `"Verifier crashed: <msg> (verifier: ExplodingVerifier)"`). Rust
/// uses `&dyn Verifier` trait objects which erase concrete type
/// information at runtime, so the verifier name cannot be recovered
/// without a `name()` method on the trait. To keep the trait minimal and
/// the public API stable, Rust crash messages omit the identity suffix —
/// this is an intentional, language-idiomatic asymmetry, not a defect.
pub fn run_verifier_chain(
    verifiers: &[&dyn Verifier],
    path: &str,
    module_id: &str,
) -> VerifyResult {
    for verifier in verifiers {
        let verifier = AssertUnwindSafe(verifier);
        let path = path.to_string();
        let module_id = module_id.to_string();
        let outcome = catch_unwind(move || verifier.verify(&path, &module_id));
        match outcome {
            Ok(result) if !result.ok => return result,
            Ok(_) => {} // passed, continue
            Err(panic_info) => {
                let msg = if let Some(s) = panic_info.downcast_ref::<&str>() {
                    (*s).to_string()
                } else if let Some(s) = panic_info.downcast_ref::<String>() {
                    s.clone()
                } else {
                    "unknown panic".to_string()
                };
                return VerifyResult::fail(format!("Verifier crashed: {msg}"));
            }
        }
    }
    VerifyResult::ok()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    // ---- empty-path contract (RegistryWriter compatibility) ----

    #[test]
    fn test_syntax_verifier_empty_path_passes() {
        assert!(SyntaxVerifier.verify("", "mod").ok);
    }

    #[test]
    fn test_yaml_verifier_empty_path_passes() {
        assert!(YAMLVerifier.verify("", "mod").ok);
    }

    #[test]
    fn test_json_verifier_empty_path_passes() {
        assert!(JSONVerifier::new().verify("", "mod").ok);
    }

    #[test]
    fn test_magic_bytes_verifier_empty_path_passes() {
        let v = MagicBytesVerifier::new(b"PNG".to_vec());
        assert!(v.verify("", "mod").ok);
    }

    #[test]
    fn test_yaml_verifier_valid() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "bindings:\n  - module_id: test\n    target: app:func").unwrap();
        let result = YAMLVerifier.verify(f.path().to_str().unwrap(), "test");
        assert!(result.ok);
    }

    #[test]
    fn test_yaml_verifier_invalid_yaml() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "{{invalid: yaml: [}}").unwrap();
        let result = YAMLVerifier.verify(f.path().to_str().unwrap(), "test");
        assert!(!result.ok);
        assert!(result.error.unwrap().contains("Invalid YAML"));
    }

    #[test]
    fn test_yaml_verifier_missing_bindings() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "other_key: value").unwrap();
        let result = YAMLVerifier.verify(f.path().to_str().unwrap(), "test");
        assert!(!result.ok);
    }

    #[test]
    fn test_yaml_verifier_multi_binding_second_entry_missing_target() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(
            f,
            "bindings:\n  - module_id: first\n    target: app:fn1\n  - module_id: second"
        )
        .unwrap();
        let result = YAMLVerifier.verify(f.path().to_str().unwrap(), "test");
        assert!(!result.ok, "should fail when second entry lacks 'target'");
        assert!(
            result.error.unwrap().contains("target"),
            "error should mention missing field"
        );
    }

    #[test]
    fn test_yaml_verifier_multi_binding_all_valid() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(
            f,
            "bindings:\n  - module_id: first\n    target: app:fn1\n  - module_id: second\n    target: app:fn2"
        )
        .unwrap();
        let result = YAMLVerifier.verify(f.path().to_str().unwrap(), "test");
        assert!(result.ok, "should pass when all entries are valid");
    }

    #[test]
    fn test_yaml_verifier_missing_required_field() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "bindings:\n  - module_id: test").unwrap();
        let result = YAMLVerifier.verify(f.path().to_str().unwrap(), "test");
        assert!(!result.ok);
        assert!(result.error.unwrap().contains("target"));
    }

    #[test]
    fn test_yaml_verifier_whitespace_only_module_id() {
        // D11-007: whitespace-only module_id must be rejected, not accepted.
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "bindings:\n  - module_id: \"   \"\n    target: app:func").unwrap();
        let result = YAMLVerifier.verify(f.path().to_str().unwrap(), "test");
        assert!(!result.ok, "whitespace-only module_id should be rejected");
        assert!(
            result.error.unwrap().contains("module_id"),
            "error should mention the invalid field"
        );
    }

    #[test]
    fn test_yaml_verifier_integer_module_id() {
        // D11-007: integer module_id (non-string) must be rejected.
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "bindings:\n  - module_id: 42\n    target: app:func").unwrap();
        let result = YAMLVerifier.verify(f.path().to_str().unwrap(), "test");
        assert!(!result.ok, "integer module_id should be rejected");
        assert!(
            result.error.unwrap().contains("module_id"),
            "error should mention the invalid field"
        );
    }

    #[test]
    fn test_json_verifier_valid() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, r#"{{"key": "value"}}"#).unwrap();
        let result = JSONVerifier::new().verify(f.path().to_str().unwrap(), "test");
        assert!(result.ok);
    }

    #[test]
    fn test_json_verifier_invalid() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "not json").unwrap();
        let result = JSONVerifier::new().verify(f.path().to_str().unwrap(), "test");
        assert!(!result.ok);
    }

    #[test]
    fn test_magic_bytes_verifier_match() {
        let mut f = NamedTempFile::new().unwrap();
        f.write_all(b"\x89PNG\r\n\x1a\nrest of file").unwrap();
        let verifier = MagicBytesVerifier::new(b"\x89PNG\r\n\x1a\n".to_vec());
        let result = verifier.verify(f.path().to_str().unwrap(), "test");
        assert!(result.ok);
    }

    #[test]
    fn test_magic_bytes_verifier_mismatch() {
        let mut f = NamedTempFile::new().unwrap();
        f.write_all(b"NOT PNG").unwrap();
        let verifier = MagicBytesVerifier::new(b"\x89PNG".to_vec());
        let result = verifier.verify(f.path().to_str().unwrap(), "test");
        assert!(!result.ok);
        assert!(result.error.unwrap().contains("mismatch"));
    }

    #[test]
    fn test_run_verifier_chain_all_pass() {
        let v1 = JSONVerifier::new();
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, r#"{{"ok": true}}"#).unwrap();
        let verifiers: Vec<&dyn Verifier> = vec![&v1];
        let result = run_verifier_chain(&verifiers, f.path().to_str().unwrap(), "test");
        assert!(result.ok);
    }

    #[test]
    fn test_run_verifier_chain_stops_on_failure() {
        let v1 = JSONVerifier::new();
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "not json").unwrap();
        let verifiers: Vec<&dyn Verifier> = vec![&v1];
        let result = run_verifier_chain(&verifiers, f.path().to_str().unwrap(), "test");
        assert!(!result.ok);
    }

    #[test]
    fn test_run_verifier_chain_empty() {
        let verifiers: Vec<&dyn Verifier> = vec![];
        let result = run_verifier_chain(&verifiers, "", "test");
        assert!(result.ok);
    }

    #[test]
    fn test_yaml_verifier_nonexistent_file() {
        let result = YAMLVerifier.verify("/tmp/nonexistent_file_abc123.yaml", "test");
        assert!(!result.ok);
        assert!(result.error.unwrap().contains("Cannot read file"));
    }

    #[test]
    fn test_json_verifier_nonexistent_file() {
        let result = JSONVerifier::new().verify("/tmp/nonexistent_file_abc123.json", "test");
        assert!(!result.ok);
        assert!(result.error.unwrap().contains("Cannot read file"));
    }

    #[test]
    fn test_magic_bytes_verifier_file_too_short() {
        let mut f = NamedTempFile::new().unwrap();
        f.write_all(b"AB").unwrap();
        let verifier = MagicBytesVerifier::new(b"ABCDEF".to_vec());
        let result = verifier.verify(f.path().to_str().unwrap(), "test");
        assert!(!result.ok);
        let err = result.error.unwrap();
        assert!(err.contains("File too short"), "got: {err}");
        assert!(err.contains("6"), "should mention expected length");
        assert!(err.contains("2"), "should mention actual length");
    }

    /// A verifier that panics, used to test catch_unwind in the chain.
    struct PanickingVerifier;

    impl Verifier for PanickingVerifier {
        fn verify(&self, _path: &str, _module_id: &str) -> VerifyResult {
            panic!("verifier exploded");
        }
    }

    #[test]
    fn test_run_verifier_chain_panic_caught() {
        let bad = PanickingVerifier;
        let verifiers: Vec<&dyn Verifier> = vec![&bad];
        let result = run_verifier_chain(&verifiers, "/fake", "test");
        assert!(!result.ok);
        let err = result.error.unwrap();
        assert!(
            err.contains("Verifier crashed"),
            "expected crash message, got: {err}"
        );
        assert!(
            err.contains("verifier exploded"),
            "expected panic message, got: {err}"
        );
    }

    /// A verifier that always returns an error, simulating a "crash" without panicking.
    struct AlwaysFailVerifier {
        message: String,
    }

    impl AlwaysFailVerifier {
        fn new(message: &str) -> Self {
            Self {
                message: message.to_string(),
            }
        }
    }

    impl Verifier for AlwaysFailVerifier {
        fn verify(&self, _path: &str, _module_id: &str) -> VerifyResult {
            VerifyResult::fail(self.message.clone())
        }
    }

    /// A verifier that always passes.
    struct AlwaysPassVerifier;

    impl Verifier for AlwaysPassVerifier {
        fn verify(&self, _path: &str, _module_id: &str) -> VerifyResult {
            VerifyResult::ok()
        }
    }

    #[test]
    fn test_run_verifier_chain_crash_caught() {
        let bad = AlwaysFailVerifier::new("simulated crash");
        let verifiers: Vec<&dyn Verifier> = vec![&bad];
        let result = run_verifier_chain(&verifiers, "/fake", "test");
        assert!(!result.ok);
        assert_eq!(result.error.as_deref(), Some("simulated crash"));
    }

    #[test]
    fn test_run_verifier_chain_first_failure_stops() {
        // The first verifier fails, so the second should never matter.
        let fail_v = AlwaysFailVerifier::new("first failed");
        let pass_v = AlwaysPassVerifier;
        let verifiers: Vec<&dyn Verifier> = vec![&fail_v, &pass_v];
        let result = run_verifier_chain(&verifiers, "/fake", "test");
        assert!(!result.ok);
        assert_eq!(result.error.as_deref(), Some("first failed"));
    }

    #[test]
    fn test_syntax_verifier_valid_rust() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "fn main() {{\n    println!(\"hello\");\n}}").unwrap();
        let result = SyntaxVerifier.verify(f.path().to_str().unwrap(), "test");
        assert!(result.ok, "expected ok, got: {:?}", result.error);
    }

    #[test]
    fn test_syntax_verifier_invalid_rust() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "fn main() {{{{{{").unwrap();
        let result = SyntaxVerifier.verify(f.path().to_str().unwrap(), "test");
        assert!(!result.ok);
        assert!(result.error.unwrap().contains("Invalid Rust syntax"));
    }

    #[test]
    fn test_syntax_verifier_empty_file() {
        let mut f = NamedTempFile::new().unwrap();
        write!(f, "").unwrap();
        let result = SyntaxVerifier.verify(f.path().to_str().unwrap(), "test");
        assert!(!result.ok);
        assert!(result.error.unwrap().contains("File is empty"));
    }

    #[test]
    fn test_syntax_verifier_nonexistent_file() {
        let result = SyntaxVerifier.verify("/tmp/nonexistent_rs_file_abc123.rs", "test");
        assert!(!result.ok);
        assert!(result.error.unwrap().contains("Cannot read file"));
    }

    #[test]
    fn test_syntax_verifier_whitespace_only() {
        let mut f = NamedTempFile::new().unwrap();
        write!(f, "   \n\n  \t  ").unwrap();
        let result = SyntaxVerifier.verify(f.path().to_str().unwrap(), "test");
        assert!(!result.ok);
        assert!(result.error.unwrap().contains("File is empty"));
    }

    #[test]
    fn test_syntax_verifier_complex_valid() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(
            f,
            "use std::collections::HashMap;\n\
             \n\
             pub struct Foo {{\n\
                 pub name: String,\n\
                 pub values: HashMap<String, i32>,\n\
             }}\n\
             \n\
             impl Foo {{\n\
                 pub fn new(name: String) -> Self {{\n\
                     Self {{ name, values: HashMap::new() }}\n\
                 }}\n\
             }}"
        )
        .unwrap();
        let result = SyntaxVerifier.verify(f.path().to_str().unwrap(), "test");
        assert!(result.ok, "expected ok, got: {:?}", result.error);
    }
}