Skip to main content

openapi_nexus/generators/
response_names.rs

1use heck::ToLowerCamelCase as _;
2
3/// Stable operation-response entry name used by language emitters.
4///
5/// This names the OpenAPI response entry itself. Body schema names are payload
6/// types and must not decide classification.
7pub fn response_entry_name(status: &str) -> String {
8    let upper = status.to_ascii_uppercase();
9    match status {
10        s if s.eq_ignore_ascii_case("default") => "Default".to_string(),
11        s if s.eq_ignore_ascii_case("4XX") => "ClientError".to_string(),
12        s if s.eq_ignore_ascii_case("5XX") => "ServerError".to_string(),
13        s if upper.ends_with("XX") => format!("Status{}xx", &s[..s.len() - 2]),
14        s => match s.parse::<u16>().ok() {
15            Some(400) => "BadRequest".to_string(),
16            Some(401) => "Unauthorized".to_string(),
17            Some(403) => "Forbidden".to_string(),
18            Some(404) => "NotFound".to_string(),
19            Some(409) => "Conflict".to_string(),
20            Some(422) => "Validation".to_string(),
21            Some(429) => "TooManyRequests".to_string(),
22            Some(500) => "InternalServerError".to_string(),
23            Some(502) => "BadGateway".to_string(),
24            Some(503) => "ServiceUnavailable".to_string(),
25            Some(504) => "GatewayTimeout".to_string(),
26            Some(code) => format!("Status{code}"),
27            None => "Unexpected".to_string(),
28        },
29    }
30}
31
32pub fn response_entry_kind(status: &str) -> String {
33    response_entry_name(status).to_lower_camel_case()
34}
35
36pub fn response_match_rank(status: &str) -> u8 {
37    if status.parse::<u16>().is_ok() {
38        0
39    } else if status.to_ascii_uppercase().ends_with("XX") {
40        1
41    } else if status.eq_ignore_ascii_case("default") {
42        2
43    } else {
44        3
45    }
46}