anvil/eth/beacon/
response.rs1use axum::{
4 Json,
5 http::StatusCode,
6 response::{IntoResponse, Response},
7};
8use serde::{Deserialize, Serialize};
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct BeaconResponse<T> {
16 pub data: T,
18 #[serde(default, skip_serializing_if = "Option::is_none")]
22 pub execution_optimistic: Option<bool>,
23 #[serde(default, skip_serializing_if = "Option::is_none")]
27 pub finalized: Option<bool>,
28}
29
30impl<T> BeaconResponse<T> {
31 pub fn new(data: T) -> Self {
35 Self { data, execution_optimistic: None, finalized: None }
36 }
37
38 pub fn with_flags(data: T, execution_optimistic: bool, finalized: bool) -> Self {
40 Self { data, execution_optimistic: Some(execution_optimistic), finalized: Some(finalized) }
41 }
42}
43
44impl<T: Serialize> IntoResponse for BeaconResponse<T> {
45 fn into_response(self) -> Response {
46 (StatusCode::OK, Json(self)).into_response()
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_beacon_response_defaults() {
56 let response = BeaconResponse::new("test data");
57 assert_eq!(response.data, "test data");
58 assert!(response.execution_optimistic.is_none());
59 assert!(response.finalized.is_none());
60 }
61
62 #[test]
63 fn test_beacon_response_serialization() {
64 let response = BeaconResponse::with_flags(vec![1, 2, 3], false, false);
65 let json = serde_json::to_value(&response).unwrap();
66
67 assert_eq!(json["data"], serde_json::json!([1, 2, 3]));
68 assert_eq!(json["execution_optimistic"], false);
69 assert_eq!(json["finalized"], false);
70 }
71}