pub mod rust;
use crate::core::openapi::OpenApiOperation;
use crate::core::templates::ServerTemplateKind;
use serde_json::Value as JsonValue;
pub trait EndpointContextBuilder {
fn build(&self, op: &OpenApiOperation) -> crate::core::error::Result<JsonValue>;
}
pub struct EndpointContext;
impl EndpointContext {
pub fn transform_endpoints(
template: ServerTemplateKind,
operations: Vec<OpenApiOperation>,
) -> crate::core::error::Result<Vec<JsonValue>> {
let builder = Self::get_builder(template);
let mut contexts = Vec::new();
for op in operations {
contexts.push(builder.build(&op)?);
}
contexts.sort_by(|a, b| {
let name_a = a.get("endpoint").and_then(|v| v.as_str()).unwrap_or("");
let name_b = b.get("endpoint").and_then(|v| v.as_str()).unwrap_or("");
name_a.cmp(name_b)
});
Ok(contexts)
}
pub fn get_builder(template: ServerTemplateKind) -> Box<dyn EndpointContextBuilder> {
match template {
ServerTemplateKind::RustAxum => Box::new(rust::RustEndpointContextBuilder),
_ => unimplemented!("Builder not implemented for template: {:?}", template),
}
}
}