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
//! OpenAPI exporter
//!
//! Provides functionality to export OpenAPI models in their native YAML or JSON format.
use crate::export::ExportError;
use crate::models::openapi::OpenAPIFormat;
use serde_json::Value as JsonValue;
/// OpenAPI Exporter
///
/// Exports OpenAPI models in their native YAML or JSON format.
#[derive(Debug, Default)]
pub struct OpenAPIExporter;
impl OpenAPIExporter {
/// Create a new OpenAPIExporter
pub fn new() -> Self {
Self
}
/// Export OpenAPI model content
///
/// # Arguments
///
/// * `content` - The OpenAPI content as a string (YAML or JSON).
/// * `source_format` - The format of the source content.
/// * `target_format` - Optional target format (if conversion needed).
///
/// # Returns
///
/// The content in the requested format.
///
/// # Example
///
/// ```rust
/// use data_modelling_core::export::openapi::OpenAPIExporter;
/// use data_modelling_core::models::openapi::OpenAPIFormat;
///
/// let exporter = OpenAPIExporter::new();
/// let yaml_content = r#"openapi: 3.1.0
/// info:
/// title: Test API
/// version: 1.0.0"#;
/// let exported = exporter.export(yaml_content, OpenAPIFormat::Yaml, Some(OpenAPIFormat::Json)).unwrap();
/// ```
pub fn export(
&self,
content: &str,
source_format: OpenAPIFormat,
target_format: Option<OpenAPIFormat>,
) -> Result<String, ExportError> {
// If no target format specified, return content as-is
let target = target_format.unwrap_or(source_format);
// If formats match, validate and return content as-is
if source_format == target {
// Validate content before returning (if feature enabled)
#[cfg(all(feature = "schema-validation", feature = "openapi"))]
{
use crate::validation::schema::validate_openapi_internal;
validate_openapi_internal(content).map_err(|e| {
ExportError::ValidationError(format!("OpenAPI validation failed: {}", e))
})?;
}
return Ok(content.to_string());
}
// Parse source content
let json_value: JsonValue = match source_format {
OpenAPIFormat::Yaml => serde_yaml::from_str(content).map_err(|e| {
ExportError::SerializationError(format!("Failed to parse YAML: {}", e))
})?,
OpenAPIFormat::Json => serde_json::from_str(content).map_err(|e| {
ExportError::SerializationError(format!("Failed to parse JSON: {}", e))
})?,
};
// Convert to target format
let result = match target {
OpenAPIFormat::Yaml => serde_yaml::to_string(&json_value).map_err(|e| {
ExportError::SerializationError(format!("Failed to serialize to YAML: {}", e))
})?,
OpenAPIFormat::Json => serde_json::to_string_pretty(&json_value).map_err(|e| {
ExportError::SerializationError(format!("Failed to serialize to JSON: {}", e))
})?,
};
// Validate exported content against OpenAPI schema (if feature enabled)
#[cfg(all(feature = "schema-validation", feature = "openapi"))]
{
use crate::validation::schema::validate_openapi_internal;
validate_openapi_internal(&result).map_err(|e| {
ExportError::ValidationError(format!("OpenAPI validation failed: {}", e))
})?;
}
Ok(result)
}
}