openzeppelin_relayer/models/
api_response.rs

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use serde::Serialize;
use utoipa::ToSchema;

#[derive(Debug, Serialize, PartialEq, Clone, ToSchema)]
pub struct PaginationMeta {
    pub current_page: u32,
    pub per_page: u32,
    pub total_items: u64,
}

#[derive(Serialize, ToSchema)]
pub struct ApiResponse<T> {
    pub success: bool,
    pub data: Option<T>,
    #[schema(nullable = false)]
    pub error: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schema(nullable = false)]
    pub pagination: Option<PaginationMeta>,
}

#[allow(dead_code)]
impl<T> ApiResponse<T> {
    pub fn new(data: Option<T>, error: Option<String>, pagination: Option<PaginationMeta>) -> Self {
        Self {
            success: error.is_none(),
            data,
            error,
            pagination,
        }
    }

    pub fn success(data: T) -> Self {
        Self {
            success: true,
            data: Some(data),
            error: None,
            pagination: None,
        }
    }

    pub fn error(message: impl Into<String>) -> Self {
        Self {
            success: false,
            data: None,
            error: Some(message.into()),
            pagination: None,
        }
    }

    pub fn no_data() -> Self {
        Self {
            success: true,
            data: None,
            error: None,
            pagination: None,
        }
    }

    pub fn paginated(data: T, meta: PaginationMeta) -> Self {
        Self {
            success: true,
            data: Some(data),
            error: None,
            pagination: Some(meta),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_new_with_data() {
        let data = "test data";
        let response = ApiResponse::new(Some(data), None, None);

        assert!(response.success);
        assert_eq!(response.data, Some(data));
        assert_eq!(response.error, None);
        assert_eq!(response.pagination, None);
    }

    #[test]
    fn test_new_with_error() {
        let error = "test error";
        let response: ApiResponse<()> = ApiResponse::new(None, Some(error.to_string()), None);

        assert!(!response.success);
        assert_eq!(response.data, None);
        assert_eq!(response.error, Some(error.to_string()));
        assert_eq!(response.pagination, None);
    }

    #[test]
    fn test_success() {
        let data = "test data";
        let response = ApiResponse::success(data);

        assert!(response.success);
        assert_eq!(response.data, Some(data));
        assert_eq!(response.error, None);
        assert_eq!(response.pagination, None);
    }

    #[test]
    fn test_error() {
        let error = "test error";
        let response: ApiResponse<()> = ApiResponse::error(error);

        assert!(!response.success);
        assert_eq!(response.data, None);
        assert_eq!(response.error, Some(error.to_string()));
        assert_eq!(response.pagination, None);
    }

    #[test]
    fn test_no_data() {
        let response: ApiResponse<String> = ApiResponse::no_data();

        assert!(response.success);
        assert_eq!(response.data, None);
        assert_eq!(response.error, None);
        assert_eq!(response.pagination, None);
    }

    #[test]
    fn test_paginated() {
        let data = "test data";
        let pagination = PaginationMeta {
            current_page: 1,
            per_page: 10,
            total_items: 100,
        };

        let response = ApiResponse::paginated(data, pagination.clone());

        assert!(response.success);
        assert_eq!(response.data, Some(data));
        assert_eq!(response.error, None);
        assert_eq!(response.pagination, Some(pagination));
    }
}