fix: proper datetime serialization

This commit is contained in:
2025-12-06 13:36:17 +08:00
parent 0e53ef4488
commit 66a2245733
4 changed files with 78 additions and 15 deletions

View File

@@ -1,4 +1,7 @@
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use chrono::{DateTime, TimeZone, Utc};
use serde::{Deserialize, Serialize, Serializer};
use serde_json::Value;
// Authentication models
@@ -48,8 +51,10 @@ pub struct RuleResponse {
pub action: String,
pub custom_response: Option<String>,
pub is_enabled: bool,
pub created_at: String,
pub updated_at: String,
#[serde(serialize_with = "serialize_dt")]
pub created_at: DateTime<Utc>,
#[serde(serialize_with = "serialize_dt")]
pub updated_at: DateTime<Utc>,
}
impl From<crate::db::models::InterceptionRule> for RuleResponse {
@@ -71,8 +76,10 @@ pub struct CommandResponse {
pub id: i64,
pub command: Value,
pub status: String,
pub received_at: String,
pub processed_at: Option<String>,
#[serde(serialize_with = "serialize_dt")]
pub received_at: DateTime<Utc>,
#[serde(serialize_with = "serialize_option_dt")]
pub processed_at: Option<DateTime<Utc>>,
pub notes: Option<String>,
}
@@ -99,3 +106,24 @@ impl ApiError {
Self { error: msg.into() }
}
}
fn serialize_dt<S, TZ>(dt: &DateTime<TZ>, serializer: S) -> Result<S::Ok, S::Error>
where
TZ: TimeZone,
TZ::Offset: Display,
S: Serializer,
{
let s = dt.format("%Y-%m-%d %H:%M:%S %z").to_string();
serializer.serialize_str(&s)
}
fn serialize_option_dt<S, TZ>(dt: &Option<DateTime<TZ>>, serializer: S) -> Result<S::Ok, S::Error>
where
TZ: TimeZone,
TZ::Offset: Display,
S: Serializer,
{
dt.as_ref()
.map(|dt| dt.format("%Y-%m-%d %H:%M:%S %z").to_string())
.serialize(serializer)
}

View File

@@ -1,4 +1,7 @@
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use chrono::{DateTime, TimeZone, Utc};
use serde::{Deserialize, Serialize, Serializer};
use serde_json::Value;
#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
@@ -8,8 +11,10 @@ pub struct InterceptionRule {
pub action: String,
pub custom_response: Option<String>,
pub is_enabled: bool,
pub created_at: String,
pub updated_at: String,
#[serde(serialize_with = "serialize_dt")]
pub created_at: DateTime<Utc>,
#[serde(serialize_with = "serialize_dt")]
pub updated_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
@@ -17,8 +22,11 @@ pub struct Command {
pub id: i64,
pub command_json: String,
pub status: String,
pub received_at: String,
pub processed_at: Option<String>,
#[serde(serialize_with = "serialize_dt")]
pub received_at: DateTime<Utc>,
#[serde(serialize_with = "serialize_option_dt")]
#[serde(default)]
pub processed_at: Option<DateTime<Utc>>,
pub notes: Option<String>,
}
@@ -27,7 +35,8 @@ pub struct Config {
pub key: String,
pub value: String,
pub description: Option<String>,
pub updated_at: String,
#[serde(serialize_with = "serialize_dt")]
pub updated_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
@@ -38,5 +47,27 @@ pub struct RequestLog {
pub response_body: Value,
pub request_interception_action: String,
pub response_interception_action: String,
pub created_at: String,
#[serde(serialize_with = "serialize_dt")]
pub created_at: DateTime<Utc>,
}
fn serialize_dt<S, TZ>(dt: &DateTime<TZ>, serializer: S) -> Result<S::Ok, S::Error>
where
TZ: TimeZone,
TZ::Offset: Display,
S: Serializer,
{
let s = dt.format("%Y-%m-%d %H:%M:%S %z").to_string();
serializer.serialize_str(&s)
}
fn serialize_option_dt<S, TZ>(dt: &Option<DateTime<TZ>>, serializer: S) -> Result<S::Ok, S::Error>
where
TZ: TimeZone,
TZ::Offset: Display,
S: Serializer,
{
dt.as_ref()
.map(|dt| dt.format("%Y-%m-%d %H:%M:%S %z").to_string())
.serialize(serializer)
}