forgedb_validation/
status.rs1pub struct StatusCodeMapper;
5
6impl StatusCodeMapper {
7 pub fn for_validation_error(error_type: &str) -> u16 {
9 match error_type {
10 "required_field" => 400, "invalid_format" => 400, "invalid_type" => 400, "out_of_range" => 400, "not_found" => 404, "already_exists" => 409, "unique_violation" => 409, "foreign_key_violation" => 422, "internal_error" => 500, _ => 400, }
21 }
22
23 pub fn status_name(code: u16) -> &'static str {
25 match code {
26 200 => "OK",
27 201 => "Created",
28 204 => "No Content",
29 400 => "Bad Request",
30 401 => "Unauthorized",
31 403 => "Forbidden",
32 404 => "Not Found",
33 409 => "Conflict",
34 422 => "Unprocessable Entity",
35 500 => "Internal Server Error",
36 502 => "Bad Gateway",
37 503 => "Service Unavailable",
38 _ => "Unknown",
39 }
40 }
41
42 pub fn is_success(code: u16) -> bool {
44 code >= 200 && code < 300
45 }
46
47 pub fn is_client_error(code: u16) -> bool {
49 code >= 400 && code < 500
50 }
51
52 pub fn is_server_error(code: u16) -> bool {
54 code >= 500 && code < 600
55 }
56}
57