Skip to main content

apcore_toolkit/output/
mod.rs

1// Output writers for ScannedModule data.
2//
3// Provides writers for different output formats (YAML, registry, HTTP proxy).
4
5pub mod errors;
6pub mod registry_writer;
7pub mod types;
8pub mod verifiers;
9pub mod yaml_writer;
10
11#[cfg(feature = "http-proxy")]
12pub mod http_proxy_writer;
13
14use serde::{Deserialize, Serialize};
15
16/// Supported output format variants.
17///
18/// Used by `get_writer` to select the appropriate writer implementation.
19/// Each variant corresponds to a distinct writer struct with its own `write()`
20/// signature, so the factory returns the enum itself rather than a trait object.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
22pub enum OutputFormat {
23    /// Write `.binding.yaml` files to disk.
24    Yaml,
25    /// Register modules directly into an apcore Registry.
26    Registry,
27    /// Register modules as HTTP proxy modules (requires `http-proxy` feature).
28    HttpProxy,
29}
30
31/// Convenience factory that returns the `OutputFormat` variant for a given
32/// format string.
33///
34/// # Accepted values (case-insensitive)
35///
36/// | Input | Variant |
37/// |-------|---------|
38/// | `"yaml"` | `OutputFormat::Yaml` |
39/// | `"registry"` | `OutputFormat::Registry` |
40/// | `"http_proxy"` / `"http-proxy"` / `"httpproxy"` | `OutputFormat::HttpProxy` |
41///
42/// Returns `None` for unrecognised strings.
43///
44/// # Usage
45///
46/// ```rust
47/// use apcore_toolkit::output::get_writer;
48/// use apcore_toolkit::output::OutputFormat;
49///
50/// let fmt = get_writer("yaml").unwrap();
51/// assert_eq!(fmt, OutputFormat::Yaml);
52///
53/// // Then instantiate the concrete writer:
54/// match fmt {
55///     OutputFormat::Yaml => { /* use YAMLWriter */ }
56///     OutputFormat::Registry => { /* use RegistryWriter */ }
57///     OutputFormat::HttpProxy => { /* use HTTPProxyRegistryWriter */ }
58/// }
59/// ```
60pub fn get_writer(format: &str) -> Option<OutputFormat> {
61    match format.to_ascii_lowercase().as_str() {
62        "yaml" => Some(OutputFormat::Yaml),
63        "registry" => Some(OutputFormat::Registry),
64        "http_proxy" | "http-proxy" | "httpproxy" => Some(OutputFormat::HttpProxy),
65        _ => None,
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn test_get_writer_yaml() {
75        assert_eq!(get_writer("yaml"), Some(OutputFormat::Yaml));
76    }
77
78    #[test]
79    fn test_get_writer_registry() {
80        assert_eq!(get_writer("registry"), Some(OutputFormat::Registry));
81    }
82
83    #[test]
84    fn test_get_writer_http_proxy_variants() {
85        assert_eq!(get_writer("http_proxy"), Some(OutputFormat::HttpProxy));
86        assert_eq!(get_writer("http-proxy"), Some(OutputFormat::HttpProxy));
87        assert_eq!(get_writer("httpproxy"), Some(OutputFormat::HttpProxy));
88    }
89
90    #[test]
91    fn test_get_writer_case_insensitive() {
92        assert_eq!(get_writer("YAML"), Some(OutputFormat::Yaml));
93        assert_eq!(get_writer("Registry"), Some(OutputFormat::Registry));
94        assert_eq!(get_writer("HTTP_PROXY"), Some(OutputFormat::HttpProxy));
95    }
96
97    #[test]
98    fn test_get_writer_unknown() {
99        assert_eq!(get_writer("xml"), None);
100        assert_eq!(get_writer(""), None);
101    }
102
103    #[test]
104    fn test_output_format_serde_roundtrip() {
105        let fmt = OutputFormat::Yaml;
106        let json = serde_json::to_string(&fmt).unwrap();
107        let deserialized: OutputFormat = serde_json::from_str(&json).unwrap();
108        assert_eq!(deserialized, fmt);
109    }
110}