mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-10-18 11:02:54 +00:00
feat: Add Redis serialization trait implementations for SubscriberAccount and UsageEvent types
- Add redis::FromRedisValue/ToRedisArgs/ToSingleRedisArg imports - Implement FromRedisValue for SubscriberAccount with JSON deserialization and ParsingError mapping - Implement ToRedisArgs for SubscriberAccount with JSON serialization - Implement ToSingleRedisArg for SubscriberAccount - Implement FromRedisValue for UsageEvent with JSON deserialization and ParsingError mapping - Implement ToRedisArgs for UsageEvent with JSON serial
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use redis::{FromRedisValue, ToRedisArgs, ToSingleRedisArg};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SubscriberAccount {
|
||||
@@ -95,3 +96,48 @@ pub struct Action {
|
||||
pub action_type: String,
|
||||
pub parameters: std::collections::HashMap<String, String>,
|
||||
}
|
||||
|
||||
// Redis trait implementations for serialization
|
||||
impl FromRedisValue for SubscriberAccount {
|
||||
fn from_redis_value(v: redis::Value) -> Result<Self, redis::ParsingError> {
|
||||
let json: String = redis::from_redis_value(v)?;
|
||||
let account: SubscriberAccount = serde_json::from_str(&json)
|
||||
.map_err(|e| redis::ParsingError::from(e.to_string()))?;
|
||||
Ok(account)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToRedisArgs for SubscriberAccount {
|
||||
fn write_redis_args<W>(&self, out: &mut W)
|
||||
where
|
||||
W: redis::RedisWrite + ?Sized,
|
||||
{
|
||||
let json = serde_json::to_string(self)
|
||||
.expect("Failed to serialize SubscriberAccount");
|
||||
json.write_redis_args(out)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSingleRedisArg for SubscriberAccount {}
|
||||
|
||||
impl FromRedisValue for UsageEvent {
|
||||
fn from_redis_value(v: redis::Value) -> Result<Self, redis::ParsingError> {
|
||||
let json: String = redis::from_redis_value(v)?;
|
||||
let event: UsageEvent = serde_json::from_str(&json)
|
||||
.map_err(|e| redis::ParsingError::from(e.to_string()))?;
|
||||
Ok(event)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToRedisArgs for UsageEvent {
|
||||
fn write_redis_args<W>(&self, out: &mut W)
|
||||
where
|
||||
W: redis::RedisWrite + ?Sized,
|
||||
{
|
||||
let json = serde_json::to_string(self)
|
||||
.expect("Failed to serialize UsageEvent");
|
||||
json.write_redis_args(out)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSingleRedisArg for UsageEvent {}
|
||||
|
||||
Reference in New Issue
Block a user