mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-10-31 18:17:13 +08:00 
			
		
		
		
	refactor(common_utils): move serde implementations and date-time utils to common_utils crate (#40)
				
					
				
			This commit is contained in:
		
							
								
								
									
										87
									
								
								crates/common_utils/src/custom_serde.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								crates/common_utils/src/custom_serde.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,87 @@ | ||||
| //! Custom serialization/deserialization implementations. | ||||
|  | ||||
| /// Use the well-known ISO 8601 format when serializing and deserializing an | ||||
| /// [`PrimitiveDateTime`][PrimitiveDateTime]. | ||||
| /// | ||||
| /// [PrimitiveDateTime]: ::time::PrimitiveDateTime | ||||
| pub mod iso8601 { | ||||
|     use std::num::NonZeroU8; | ||||
|  | ||||
|     use serde::{ser::Error as _, Deserializer, Serialize, Serializer}; | ||||
|     use time::{ | ||||
|         format_description::well_known::{ | ||||
|             iso8601::{Config, EncodedConfig, TimePrecision}, | ||||
|             Iso8601, | ||||
|         }, | ||||
|         serde::iso8601, | ||||
|         PrimitiveDateTime, UtcOffset, | ||||
|     }; | ||||
|  | ||||
|     const FORMAT_CONFIG: EncodedConfig = Config::DEFAULT | ||||
|         .set_time_precision(TimePrecision::Second { | ||||
|             decimal_digits: NonZeroU8::new(3), | ||||
|         }) | ||||
|         .encode(); | ||||
|  | ||||
|     /// Serialize a [`PrimitiveDateTime`] using the well-known ISO 8601 format. | ||||
|     pub fn serialize<S>(date_time: &PrimitiveDateTime, serializer: S) -> Result<S::Ok, S::Error> | ||||
|     where | ||||
|         S: Serializer, | ||||
|     { | ||||
|         date_time | ||||
|             .assume_utc() | ||||
|             .format(&Iso8601::<FORMAT_CONFIG>) | ||||
|             .map_err(S::Error::custom)? | ||||
|             .serialize(serializer) | ||||
|     } | ||||
|  | ||||
|     /// Deserialize an [`PrimitiveDateTime`] from its ISO 8601 representation. | ||||
|     pub fn deserialize<'a, D>(deserializer: D) -> Result<PrimitiveDateTime, D::Error> | ||||
|     where | ||||
|         D: Deserializer<'a>, | ||||
|     { | ||||
|         iso8601::deserialize(deserializer).map(|offset_date_time| { | ||||
|             let utc_date_time = offset_date_time.to_offset(UtcOffset::UTC); | ||||
|             PrimitiveDateTime::new(utc_date_time.date(), utc_date_time.time()) | ||||
|         }) | ||||
|     } | ||||
|  | ||||
|     /// Use the well-known ISO 8601 format when serializing and deserializing an | ||||
|     /// [`Option<PrimitiveDateTime>`][PrimitiveDateTime]. | ||||
|     /// | ||||
|     /// [PrimitiveDateTime]: ::time::PrimitiveDateTime | ||||
|     pub mod option { | ||||
|         use serde::Serialize; | ||||
|         use time::format_description::well_known::Iso8601; | ||||
|  | ||||
|         use super::*; | ||||
|  | ||||
|         /// Serialize an [`Option<PrimitiveDateTime>`] using the well-known ISO 8601 format. | ||||
|         pub fn serialize<S>( | ||||
|             date_time: &Option<PrimitiveDateTime>, | ||||
|             serializer: S, | ||||
|         ) -> Result<S::Ok, S::Error> | ||||
|         where | ||||
|             S: Serializer, | ||||
|         { | ||||
|             date_time | ||||
|                 .map(|date_time| date_time.assume_utc().format(&Iso8601::<FORMAT_CONFIG>)) | ||||
|                 .transpose() | ||||
|                 .map_err(S::Error::custom)? | ||||
|                 .serialize(serializer) | ||||
|         } | ||||
|  | ||||
|         /// Deserialize an [`Option<PrimitiveDateTime>`] from its ISO 8601 representation. | ||||
|         pub fn deserialize<'a, D>(deserializer: D) -> Result<Option<PrimitiveDateTime>, D::Error> | ||||
|         where | ||||
|             D: Deserializer<'a>, | ||||
|         { | ||||
|             iso8601::option::deserialize(deserializer).map(|option_offset_date_time| { | ||||
|                 option_offset_date_time.map(|offset_date_time| { | ||||
|                     let utc_date_time = offset_date_time.to_offset(UtcOffset::UTC); | ||||
|                     PrimitiveDateTime::new(utc_date_time.date(), utc_date_time.time()) | ||||
|                 }) | ||||
|             }) | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Sanchith Hegde
					Sanchith Hegde