Skip to main content

oapi_codegen/naming/
operations.rs

1//! Deriving the Rust names of the artifacts generated for one OpenAPI operation.
2//!
3//! Centralised so the server emitter and (later) the client emitter derive the
4//! same names from the same operation identifier, and cannot drift apart.
5
6use crate::naming::Case;
7use crate::naming::RustIdent;
8use crate::naming::to_ident;
9
10pub fn operation_method_name(raw: &str) -> RustIdent {
11    return to_ident(raw, Case::Snake);
12}
13
14/// The generated response-enum name for an operation, `<Op><Suffix>` (the
15/// suffix defaults to `Response`. Override it via `response-type-suffix` to
16/// resolve a clash with a component schema of the same name).
17pub fn response_enum_name(op: &RustIdent, suffix: &str) -> RustIdent {
18    return to_ident(&format!("{}_{}", op.logical(), suffix), Case::Pascal);
19}
20
21pub fn query_struct_name(op: &RustIdent) -> RustIdent {
22    return to_ident(&format!("{}_query", op.logical()), Case::Pascal);
23}
24
25pub fn headers_struct_name(op: &RustIdent) -> RustIdent {
26    return to_ident(&format!("{}_headers", op.logical()), Case::Pascal);
27}
28
29/// The generated cookie-struct name for an operation (`<Op>Cookies`).
30pub fn cookies_struct_name(op: &RustIdent) -> RustIdent {
31    return to_ident(&format!("{}_cookies", op.logical()), Case::Pascal);
32}
33
34/// The generated multipart-extractor struct name for an operation
35/// (`<Op>Multipart`).
36pub fn multipart_struct_name(op: &RustIdent) -> RustIdent {
37    return to_ident(&format!("{}_multipart", op.logical()), Case::Pascal);
38}
39
40/// The generated dispatch-enum name for an operation whose request body offers
41/// several content types (`<Op>RequestBody`).
42pub fn request_body_enum_name(op: &RustIdent) -> RustIdent {
43    return to_ident(&format!("{}_request_body", op.logical()), Case::Pascal);
44}
45
46/// The generated body-enum name for a response variant that offers several
47/// content types (`<Response><Variant>Body`).
48pub fn response_body_enum_name(response_enum: &RustIdent, variant: &RustIdent) -> RustIdent {
49    return to_ident(
50        &format!("{}_{}_body", response_enum.logical(), variant.logical()),
51        Case::Pascal,
52    );
53}
54
55pub fn axum_handler_name(op: &RustIdent) -> RustIdent {
56    return to_ident(&format!("{}_handler", op.logical()), Case::Snake);
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    fn op(name: &str) -> RustIdent {
64        return to_ident(name, Case::Snake);
65    }
66
67    #[test]
68    fn derives_operation_artifact_names() {
69        let list_pets = op("list_pets");
70        assert_eq!(operation_method_name("listPets").logical(), "list_pets");
71        assert_eq!(response_enum_name(&list_pets, "response").logical(), "ListPetsResponse");
72        assert_eq!(query_struct_name(&list_pets).logical(), "ListPetsQuery");
73        assert_eq!(headers_struct_name(&list_pets).logical(), "ListPetsHeaders");
74        assert_eq!(cookies_struct_name(&list_pets).logical(), "ListPetsCookies");
75        assert_eq!(multipart_struct_name(&list_pets).logical(), "ListPetsMultipart");
76        assert_eq!(request_body_enum_name(&list_pets).logical(), "ListPetsRequestBody");
77        assert_eq!(response_enum_name(&list_pets, "Resp").logical(), "ListPetsResp");
78        let response = response_enum_name(&list_pets, "response");
79        let ok = to_ident("ok", Case::Pascal);
80        assert_eq!(
81            response_body_enum_name(&response, &ok).logical(),
82            "ListPetsResponseOkBody"
83        );
84        assert_eq!(axum_handler_name(&list_pets).logical(), "list_pets_handler");
85    }
86}