| 1 |
//! Collection of helper functions for serializing to and deserializing from BSON using Serde |
| 2 |
|
| 3 |
use std::{convert::TryFrom, result::Result}; |
| 4 |
|
| 5 |
use serde::{ser, Serialize, Serializer}; |
| 6 |
|
| 7 |
use crate::oid::ObjectId; |
| 8 |
|
| 9 |
#[doc(inline)] |
| 10 |
pub use bson_datetime_as_rfc3339_string::{ |
| 11 |
deserialize as deserialize_bson_datetime_from_rfc3339_string, |
| 12 |
serialize as serialize_bson_datetime_as_rfc3339_string, |
| 13 |
}; |
| 14 |
#[cfg(feature = "chrono-0_4")] |
| 15 |
#[doc(inline)] |
| 16 |
pub use chrono_datetime_as_bson_datetime::{ |
| 17 |
deserialize as deserialize_chrono_datetime_from_bson_datetime, |
| 18 |
serialize as serialize_chrono_datetime_as_bson_datetime, |
| 19 |
}; |
| 20 |
#[doc(inline)] |
| 21 |
pub use hex_string_as_object_id::{ |
| 22 |
deserialize as deserialize_hex_string_from_object_id, |
| 23 |
serialize as serialize_hex_string_as_object_id, |
| 24 |
}; |
| 25 |
#[doc(inline)] |
| 26 |
pub use i64_as_bson_datetime::{ |
| 27 |
deserialize as deserialize_i64_from_bson_datetime, |
| 28 |
serialize as serialize_i64_as_bson_datetime, |
| 29 |
}; |
| 30 |
#[doc(inline)] |
| 31 |
pub use rfc3339_string_as_bson_datetime::{ |
| 32 |
deserialize as deserialize_rfc3339_string_from_bson_datetime, |
| 33 |
serialize as serialize_rfc3339_string_as_bson_datetime, |
| 34 |
}; |
| 35 |
#[cfg(feature = "time-0_3")] |
| 36 |
#[doc(inline)] |
| 37 |
pub use time_0_3_offsetdatetime_as_bson_datetime::{ |
| 38 |
deserialize as deserialize_time_0_3_offsetdatetime_from_bson_datetime, |
| 39 |
serialize as serialize_time_0_3_offsetdatetime_as_bson_datetime, |
| 40 |
}; |
| 41 |
#[doc(inline)] |
| 42 |
pub use timestamp_as_u32::{ |
| 43 |
deserialize as deserialize_timestamp_from_u32, |
| 44 |
serialize as serialize_timestamp_as_u32, |
| 45 |
}; |
| 46 |
#[doc(inline)] |
| 47 |
pub use u32_as_f64::{deserialize as deserialize_u32_from_f64, serialize as serialize_u32_as_f64}; |
| 48 |
#[doc(inline)] |
| 49 |
pub use u32_as_timestamp::{ |
| 50 |
deserialize as deserialize_u32_from_timestamp, |
| 51 |
serialize as serialize_u32_as_timestamp, |
| 52 |
}; |
| 53 |
#[doc(inline)] |
| 54 |
pub use u64_as_f64::{deserialize as deserialize_u64_from_f64, serialize as serialize_u64_as_f64}; |
| 55 |
|
| 56 |
#[cfg(feature = "uuid-1")] |
| 57 |
#[doc(inline)] |
| 58 |
pub use uuid_1_as_binary::{ |
| 59 |
deserialize as deserialize_uuid_1_from_binary, |
| 60 |
serialize as serialize_uuid_1_as_binary, |
| 61 |
}; |
| 62 |
#[cfg(feature = "uuid-1")] |
| 63 |
#[doc(inline)] |
| 64 |
pub use uuid_1_as_c_sharp_legacy_binary::{ |
| 65 |
deserialize as deserialize_uuid_1_from_c_sharp_legacy_binary, |
| 66 |
serialize as serialize_uuid_1_as_c_sharp_legacy_binary, |
| 67 |
}; |
| 68 |
#[cfg(feature = "uuid-1")] |
| 69 |
#[doc(inline)] |
| 70 |
pub use uuid_1_as_java_legacy_binary::{ |
| 71 |
deserialize as deserialize_uuid_1_from_java_legacy_binary, |
| 72 |
serialize as serialize_uuid_1_as_java_legacy_binary, |
| 73 |
}; |
| 74 |
#[cfg(feature = "uuid-1")] |
| 75 |
#[doc(inline)] |
| 76 |
pub use uuid_1_as_python_legacy_binary::{ |
| 77 |
deserialize as deserialize_uuid_1_from_python_legacy_binary, |
| 78 |
serialize as serialize_uuid_1_as_python_legacy_binary, |
| 79 |
}; |
| 80 |
#[cfg(feature = "uuid-0_8")] |
| 81 |
#[doc(inline)] |
| 82 |
pub use uuid_as_binary::{ |
| 83 |
deserialize as deserialize_uuid_from_binary, |
| 84 |
serialize as serialize_uuid_as_binary, |
| 85 |
}; |
| 86 |
#[cfg(feature = "uuid-0_8")] |
| 87 |
#[doc(inline)] |
| 88 |
pub use uuid_as_c_sharp_legacy_binary::{ |
| 89 |
deserialize as deserialize_uuid_from_c_sharp_legacy_binary, |
| 90 |
serialize as serialize_uuid_as_c_sharp_legacy_binary, |
| 91 |
}; |
| 92 |
#[cfg(feature = "uuid-0_8")] |
| 93 |
#[doc(inline)] |
| 94 |
pub use uuid_as_java_legacy_binary::{ |
| 95 |
deserialize as deserialize_uuid_from_java_legacy_binary, |
| 96 |
serialize as serialize_uuid_as_java_legacy_binary, |
| 97 |
}; |
| 98 |
#[cfg(feature = "uuid-0_8")] |
| 99 |
#[doc(inline)] |
| 100 |
pub use uuid_as_python_legacy_binary::{ |
| 101 |
deserialize as deserialize_uuid_from_python_legacy_binary, |
| 102 |
serialize as serialize_uuid_as_python_legacy_binary, |
| 103 |
}; |
| 104 |
|
| 105 |
/// Attempts to serialize a u32 as an i32. Errors if an exact conversion is not possible. |
| 106 |
pub fn serialize_u32_as_i32<S: Serializer>(val: &u32, serializer: S) -> Result<S::Ok, S::Error> { |
| 107 |
match i32::try_from(*val) { |
| 108 |
Ok(val) => serializer.serialize_i32(val), |
| 109 |
Err(_) => Err(ser::Error::custom(format!("cannot convert {} to i32", val))), |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/// Serializes a u32 as an i64. |
| 114 |
pub fn serialize_u32_as_i64<S: Serializer>(val: &u32, serializer: S) -> Result<S::Ok, S::Error> { |
| 115 |
serializer.serialize_i64(*val as i64) |
| 116 |
} |
| 117 |
|
| 118 |
/// Attempts to serialize a u64 as an i32. Errors if an exact conversion is not possible. |
| 119 |
pub fn serialize_u64_as_i32<S: Serializer>(val: &u64, serializer: S) -> Result<S::Ok, S::Error> { |
| 120 |
match i32::try_from(*val) { |
| 121 |
Ok(val) => serializer.serialize_i32(val), |
| 122 |
Err(_) => Err(ser::Error::custom(format!("cannot convert {} to i32", val))), |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/// Attempts to serialize a u64 as an i64. Errors if an exact conversion is not possible. |
| 127 |
pub fn serialize_u64_as_i64<S: Serializer>(val: &u64, serializer: S) -> Result<S::Ok, S::Error> { |
| 128 |
match i64::try_from(*val) { |
| 129 |
Ok(val) => serializer.serialize_i64(val), |
| 130 |
Err(_) => Err(ser::Error::custom(format!("cannot convert {} to i64", val))), |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/// Serializes an [`ObjectId`] as a hex string. |
| 135 |
pub fn serialize_object_id_as_hex_string<S: Serializer>( |
| 136 |
val: &ObjectId, |
| 137 |
serializer: S, |
| 138 |
) -> Result<S::Ok, S::Error> { |
| 139 |
val.to_hex().serialize(serializer) |
| 140 |
} |
| 141 |
|
| 142 |
/// Contains functions to serialize a u32 as an f64 (BSON double) and deserialize a |
| 143 |
/// u32 from an f64 (BSON double). |
| 144 |
/// |
| 145 |
/// ```rust |
| 146 |
/// # use serde::{Serialize, Deserialize}; |
| 147 |
/// # use bson::serde_helpers::u32_as_f64; |
| 148 |
/// #[derive(Serialize, Deserialize)] |
| 149 |
/// struct FileInfo { |
| 150 |
/// #[serde(with = "u32_as_f64")] |
| 151 |
/// pub size_bytes: u32, |
| 152 |
/// } |
| 153 |
/// ``` |
| 154 |
pub mod u32_as_f64 { |
| 155 |
use serde::{de, Deserialize, Deserializer, Serializer}; |
| 156 |
|
| 157 |
/// Deserializes a u32 from an f64 (BSON double). Errors if an exact conversion is not possible. |
| 158 |
pub fn deserialize<'de, D>(deserializer: D) -> Result<u32, D::Error> |
| 159 |
where |
| 160 |
D: Deserializer<'de>, |
| 161 |
{ |
| 162 |
let f = f64::deserialize(deserializer)?; |
| 163 |
if (f - f as u32 as f64).abs() <= f64::EPSILON { |
| 164 |
Ok(f as u32) |
| 165 |
} else { |
| 166 |
Err(de::Error::custom(format!( |
| 167 |
"cannot convert f64 (BSON double) {} to u32", |
| 168 |
f |
| 169 |
))) |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/// Serializes a u32 as an f64 (BSON double). |
| 174 |
pub fn serialize<S: Serializer>(val: &u32, serializer: S) -> Result<S::Ok, S::Error> { |
| 175 |
serializer.serialize_f64(*val as f64) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/// Contains functions to serialize a u64 as an f64 (BSON double) and deserialize a |
| 180 |
/// u64 from an f64 (BSON double). |
| 181 |
/// |
| 182 |
/// ```rust |
| 183 |
/// # use serde::{Serialize, Deserialize}; |
| 184 |
/// # use bson::serde_helpers::u64_as_f64; |
| 185 |
/// #[derive(Serialize, Deserialize)] |
| 186 |
/// struct FileInfo { |
| 187 |
/// #[serde(with = "u64_as_f64")] |
| 188 |
/// pub size_bytes: u64, |
| 189 |
/// } |
| 190 |
/// ``` |
| 191 |
pub mod u64_as_f64 { |
| 192 |
use serde::{de, ser, Deserialize, Deserializer, Serializer}; |
| 193 |
|
| 194 |
/// Deserializes a u64 from an f64 (BSON double). Errors if an exact conversion is not possible. |
| 195 |
pub fn deserialize<'de, D>(deserializer: D) -> Result<u64, D::Error> |
| 196 |
where |
| 197 |
D: Deserializer<'de>, |
| 198 |
{ |
| 199 |
let f = f64::deserialize(deserializer)?; |
| 200 |
if (f - f as u64 as f64).abs() <= f64::EPSILON { |
| 201 |
Ok(f as u64) |
| 202 |
} else { |
| 203 |
Err(de::Error::custom(format!( |
| 204 |
"cannot convert f64 (BSON double) {} to u64", |
| 205 |
f |
| 206 |
))) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/// Serializes a u64 as an f64 (BSON double). Errors if an exact conversion is not possible. |
| 211 |
pub fn serialize<S: Serializer>(val: &u64, serializer: S) -> Result<S::Ok, S::Error> { |
| 212 |
if val < &u64::MAX && *val == *val as f64 as u64 { |
| 213 |
serializer.serialize_f64(*val as f64) |
| 214 |
} else { |
| 215 |
Err(ser::Error::custom(format!( |
| 216 |
"cannot convert u64 {} to f64 (BSON double)", |
| 217 |
val |
| 218 |
))) |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
/// Contains functions to serialize a [`time::OffsetDateTime`] as a [`crate::DateTime`] and |
| 224 |
/// deserialize a [`time::OffsetDateTime`] from a [`crate::DateTime`]. |
| 225 |
/// |
| 226 |
/// ```rust |
| 227 |
/// # #[cfg(feature = "time-0_3")] |
| 228 |
/// # { |
| 229 |
/// # use serde::{Serialize, Deserialize}; |
| 230 |
/// # use bson::serde_helpers::time_0_3_offsetdatetime_as_bson_datetime; |
| 231 |
/// #[derive(Serialize, Deserialize)] |
| 232 |
/// struct Event { |
| 233 |
/// #[serde(with = "time_0_3_offsetdatetime_as_bson_datetime")] |
| 234 |
/// pub date: time::OffsetDateTime, |
| 235 |
/// } |
| 236 |
/// # } |
| 237 |
/// ``` |
| 238 |
#[cfg(feature = "time-0_3")] |
| 239 |
#[cfg_attr(docsrs, doc(cfg(feature = "time-0_3")))] |
| 240 |
pub mod time_0_3_offsetdatetime_as_bson_datetime { |
| 241 |
use crate::DateTime; |
| 242 |
use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
| 243 |
use std::result::Result; |
| 244 |
|
| 245 |
/// Deserializes a [`time::OffsetDateTime`] from a [`crate::DateTime`]. |
| 246 |
#[cfg_attr(docsrs, doc(cfg(feature = "time-0_3")))] |
| 247 |
pub fn deserialize<'de, D>(deserializer: D) -> Result<time::OffsetDateTime, D::Error> |
| 248 |
where |
| 249 |
D: Deserializer<'de>, |
| 250 |
{ |
| 251 |
let datetime = DateTime::deserialize(deserializer)?; |
| 252 |
Ok(datetime.to_time_0_3()) |
| 253 |
} |
| 254 |
|
| 255 |
/// Serializes a [`time::OffsetDateTime`] as a [`crate::DateTime`]. |
| 256 |
#[cfg_attr(docsrs, doc(cfg(feature = "time-0_3")))] |
| 257 |
pub fn serialize<S: Serializer>( |
| 258 |
val: &time::OffsetDateTime, |
| 259 |
serializer: S, |
| 260 |
) -> Result<S::Ok, S::Error> { |
| 261 |
let datetime = DateTime::from_time_0_3(val.to_owned()); |
| 262 |
datetime.serialize(serializer) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
/// Contains functions to serialize a [`chrono::DateTime`] as a [`crate::DateTime`] and deserialize |
| 267 |
/// a [`chrono::DateTime`] from a [`crate::DateTime`]. |
| 268 |
/// |
| 269 |
/// ```rust |
| 270 |
/// # #[cfg(feature = "chrono-0_4")] |
| 271 |
/// # { |
| 272 |
/// # use serde::{Serialize, Deserialize}; |
| 273 |
/// # use bson::serde_helpers::chrono_datetime_as_bson_datetime; |
| 274 |
/// #[derive(Serialize, Deserialize)] |
| 275 |
/// struct Event { |
| 276 |
/// #[serde(with = "chrono_datetime_as_bson_datetime")] |
| 277 |
/// pub date: chrono::DateTime<chrono::Utc>, |
| 278 |
/// } |
| 279 |
/// # } |
| 280 |
/// ``` |
| 281 |
#[cfg(feature = "chrono-0_4")] |
| 282 |
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))] |
| 283 |
pub mod chrono_datetime_as_bson_datetime { |
| 284 |
use crate::DateTime; |
| 285 |
use chrono::Utc; |
| 286 |
use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
| 287 |
use std::result::Result; |
| 288 |
|
| 289 |
/// Deserializes a [`chrono::DateTime`] from a [`crate::DateTime`]. |
| 290 |
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))] |
| 291 |
pub fn deserialize<'de, D>(deserializer: D) -> Result<chrono::DateTime<Utc>, D::Error> |
| 292 |
where |
| 293 |
D: Deserializer<'de>, |
| 294 |
{ |
| 295 |
let datetime = DateTime::deserialize(deserializer)?; |
| 296 |
Ok(datetime.to_chrono()) |
| 297 |
} |
| 298 |
|
| 299 |
/// Serializes a [`chrono::DateTime`] as a [`crate::DateTime`]. |
| 300 |
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))] |
| 301 |
pub fn serialize<S: Serializer>( |
| 302 |
val: &chrono::DateTime<Utc>, |
| 303 |
serializer: S, |
| 304 |
) -> Result<S::Ok, S::Error> { |
| 305 |
let datetime = DateTime::from_chrono(val.to_owned()); |
| 306 |
datetime.serialize(serializer) |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
/// Contains functions to serialize an RFC 3339 (ISO 8601) formatted string as a [`crate::DateTime`] |
| 311 |
/// and deserialize an RFC 3339 (ISO 8601) formatted string from a [`crate::DateTime`]. |
| 312 |
/// |
| 313 |
/// ```rust |
| 314 |
/// # use serde::{Serialize, Deserialize}; |
| 315 |
/// # use bson::serde_helpers::rfc3339_string_as_bson_datetime; |
| 316 |
/// #[derive(Serialize, Deserialize)] |
| 317 |
/// struct Event { |
| 318 |
/// #[serde(with = "rfc3339_string_as_bson_datetime")] |
| 319 |
/// pub date: String, |
| 320 |
/// } |
| 321 |
/// ``` |
| 322 |
pub mod rfc3339_string_as_bson_datetime { |
| 323 |
use crate::{Bson, DateTime}; |
| 324 |
use serde::{de, ser, Deserialize, Deserializer, Serialize, Serializer}; |
| 325 |
use std::result::Result; |
| 326 |
|
| 327 |
/// Deserializes an ISO string from a DateTime. |
| 328 |
pub fn deserialize<'de, D>(deserializer: D) -> Result<String, D::Error> |
| 329 |
where |
| 330 |
D: Deserializer<'de>, |
| 331 |
{ |
| 332 |
let date = DateTime::deserialize(deserializer)?; |
| 333 |
date.try_to_rfc3339_string() |
| 334 |
.map_err(|e| de::Error::custom(format!("cannot format {} as RFC 3339: {}", date, e))) |
| 335 |
} |
| 336 |
|
| 337 |
/// Serializes an ISO string as a DateTime. |
| 338 |
pub fn serialize<S: Serializer>(val: &str, serializer: S) -> Result<S::Ok, S::Error> { |
| 339 |
let date = crate::DateTime::parse_rfc3339_str(val) |
| 340 |
.map_err(|_| ser::Error::custom(format!("cannot convert {} to DateTime", val)))?; |
| 341 |
Bson::DateTime(date).serialize(serializer) |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
/// Contains functions to serialize a [`crate::DateTime`] as an RFC 3339 (ISO 8601) formatted string |
| 346 |
/// and deserialize a [`crate::DateTime`] from an RFC 3339 (ISO 8601) formatted string. |
| 347 |
/// |
| 348 |
/// ```rust |
| 349 |
/// # use serde::{Serialize, Deserialize}; |
| 350 |
/// # use bson::serde_helpers::bson_datetime_as_rfc3339_string; |
| 351 |
/// #[derive(Serialize, Deserialize)] |
| 352 |
/// struct Event { |
| 353 |
/// #[serde(with = "bson_datetime_as_rfc3339_string")] |
| 354 |
/// pub date: bson::DateTime, |
| 355 |
/// } |
| 356 |
/// ``` |
| 357 |
pub mod bson_datetime_as_rfc3339_string { |
| 358 |
use crate::DateTime; |
| 359 |
use serde::{de, ser, Deserialize, Deserializer, Serializer}; |
| 360 |
use std::result::Result; |
| 361 |
|
| 362 |
/// Deserializes a [`crate::DateTime`] from an RFC 3339 formatted string. |
| 363 |
pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime, D::Error> |
| 364 |
where |
| 365 |
D: Deserializer<'de>, |
| 366 |
{ |
| 367 |
let iso = String::deserialize(deserializer)?; |
| 368 |
let date = crate::DateTime::parse_rfc3339_str(&iso).map_err(|_| { |
| 369 |
de::Error::custom(format!("cannot parse RFC 3339 datetime from \"{}\"", iso)) |
| 370 |
})?; |
| 371 |
Ok(date) |
| 372 |
} |
| 373 |
|
| 374 |
/// Serializes a [`crate::DateTime`] as an RFC 3339 (ISO 8601) formatted string. |
| 375 |
pub fn serialize<S: Serializer>(val: &DateTime, serializer: S) -> Result<S::Ok, S::Error> { |
| 376 |
let formatted = val |
| 377 |
.try_to_rfc3339_string() |
| 378 |
.map_err(|e| ser::Error::custom(format!("cannot format {} as RFC 3339: {}", val, e)))?; |
| 379 |
serializer.serialize_str(&formatted) |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
/// Contains functions to serialize a hex string as an ObjectId and deserialize a |
| 384 |
/// hex string from an ObjectId |
| 385 |
/// |
| 386 |
/// ```rust |
| 387 |
/// # use serde::{Serialize, Deserialize}; |
| 388 |
/// # use bson::serde_helpers::hex_string_as_object_id; |
| 389 |
/// #[derive(Serialize, Deserialize)] |
| 390 |
/// struct Item { |
| 391 |
/// #[serde(with = "hex_string_as_object_id")] |
| 392 |
/// pub id: String, |
| 393 |
/// } |
| 394 |
/// ``` |
| 395 |
pub mod hex_string_as_object_id { |
| 396 |
use crate::oid::ObjectId; |
| 397 |
use serde::{ser, Deserialize, Deserializer, Serialize, Serializer}; |
| 398 |
|
| 399 |
/// Deserializes a hex string from an ObjectId. |
| 400 |
pub fn deserialize<'de, D>(deserializer: D) -> Result<String, D::Error> |
| 401 |
where |
| 402 |
D: Deserializer<'de>, |
| 403 |
{ |
| 404 |
let object_id = ObjectId::deserialize(deserializer)?; |
| 405 |
Ok(object_id.to_hex()) |
| 406 |
} |
| 407 |
|
| 408 |
/// Serializes a hex string as an ObjectId. |
| 409 |
pub fn serialize<S: Serializer>(val: &str, serializer: S) -> Result<S::Ok, S::Error> { |
| 410 |
match ObjectId::parse_str(val) { |
| 411 |
Ok(oid) => oid.serialize(serializer), |
| 412 |
Err(_) => Err(ser::Error::custom(format!( |
| 413 |
"cannot convert {} to ObjectId", |
| 414 |
val |
| 415 |
))), |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
/// Contains functions to `serialize` a `i64` integer as [`DateTime`](crate::DateTime) and |
| 421 |
/// `deserialize` a `i64` integer from [`DateTime`](crate::DateTime). |
| 422 |
/// |
| 423 |
/// ### The i64 should represent seconds `(DateTime::timestamp_millis(..))`. |
| 424 |
/// |
| 425 |
/// ```rust |
| 426 |
/// # use serde::{Serialize, Deserialize}; |
| 427 |
/// # use bson::serde_helpers::i64_as_bson_datetime; |
| 428 |
/// #[derive(Serialize, Deserialize)] |
| 429 |
/// struct Item { |
| 430 |
/// #[serde(with = "i64_as_bson_datetime")] |
| 431 |
/// pub now: i64, |
| 432 |
/// } |
| 433 |
/// ``` |
| 434 |
pub mod i64_as_bson_datetime { |
| 435 |
use crate::DateTime; |
| 436 |
use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
| 437 |
|
| 438 |
/// Deserializes a i64 integer from a DateTime. |
| 439 |
pub fn deserialize<'de, D>(deserializer: D) -> Result<i64, D::Error> |
| 440 |
where |
| 441 |
D: Deserializer<'de>, |
| 442 |
{ |
| 443 |
let date: DateTime = DateTime::deserialize(deserializer)?; |
| 444 |
Ok(date.timestamp_millis()) |
| 445 |
} |
| 446 |
|
| 447 |
/// Serializes a i64 integer as a DateTime. |
| 448 |
pub fn serialize<S: Serializer>(val: &i64, serializer: S) -> Result<S::Ok, S::Error> { |
| 449 |
let date_time = DateTime::from_millis(*val); |
| 450 |
date_time.serialize(serializer) |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
#[allow(unused_macros)] |
| 455 |
macro_rules! as_binary_mod { |
| 456 |
($feat:meta, $uu:path) => { |
| 457 |
use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
| 458 |
use std::result::Result; |
| 459 |
use $uu; |
| 460 |
|
| 461 |
/// Serializes a Uuid as a Binary. |
| 462 |
#[cfg_attr(docsrs, doc($feat))] |
| 463 |
pub fn serialize<S: Serializer>(val: &Uuid, serializer: S) -> Result<S::Ok, S::Error> { |
| 464 |
crate::uuid::Uuid::from(*val).serialize(serializer) |
| 465 |
} |
| 466 |
|
| 467 |
/// Deserializes a Uuid from a Binary. |
| 468 |
#[cfg_attr(docsrs, doc($feat))] |
| 469 |
pub fn deserialize<'de, D>(deserializer: D) -> Result<Uuid, D::Error> |
| 470 |
where |
| 471 |
D: Deserializer<'de>, |
| 472 |
{ |
| 473 |
let bson_uuid = crate::uuid::Uuid::deserialize(deserializer)?; |
| 474 |
Ok(bson_uuid.into()) |
| 475 |
} |
| 476 |
}; |
| 477 |
} |
| 478 |
|
| 479 |
/// Contains functions to serialize a [`uuid_0_8::Uuid`] as a [`crate::Binary`] and deserialize a |
| 480 |
/// [`uuid_0_8::Uuid`] from a [`crate::Binary`]. |
| 481 |
/// |
| 482 |
/// ```rust |
| 483 |
/// # #[cfg(feature = "uuid-0_8")] |
| 484 |
/// # { |
| 485 |
/// use serde::{Serialize, Deserialize}; |
| 486 |
/// use uuid_0_8::Uuid; |
| 487 |
/// use bson::serde_helpers::uuid_as_binary; |
| 488 |
/// |
| 489 |
/// #[derive(Serialize, Deserialize)] |
| 490 |
/// struct Item { |
| 491 |
/// #[serde(with = "uuid_as_binary")] |
| 492 |
/// pub id: Uuid, |
| 493 |
/// } |
| 494 |
/// # } |
| 495 |
/// ``` |
| 496 |
#[cfg(feature = "uuid-0_8")] |
| 497 |
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))] |
| 498 |
pub mod uuid_as_binary { |
| 499 |
as_binary_mod!(cfg(feature = "uuid-0_8"), uuid_0_8::Uuid); |
| 500 |
} |
| 501 |
|
| 502 |
/// Contains functions to serialize a [`uuid::Uuid`] as a [`crate::Binary`] and deserialize a |
| 503 |
/// [`uuid::Uuid`] from a [`crate::Binary`]. |
| 504 |
/// |
| 505 |
/// ```rust |
| 506 |
/// # #[cfg(feature = "uuid-1")] |
| 507 |
/// # { |
| 508 |
/// use serde::{Serialize, Deserialize}; |
| 509 |
/// use uuid::Uuid; |
| 510 |
/// use bson::serde_helpers::uuid_1_as_binary; |
| 511 |
/// |
| 512 |
/// #[derive(Serialize, Deserialize)] |
| 513 |
/// struct Item { |
| 514 |
/// #[serde(with = "uuid_1_as_binary")] |
| 515 |
/// pub id: Uuid, |
| 516 |
/// } |
| 517 |
/// # } |
| 518 |
/// ``` |
| 519 |
#[cfg(feature = "uuid-1")] |
| 520 |
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-1")))] |
| 521 |
pub mod uuid_1_as_binary { |
| 522 |
as_binary_mod!(cfg(feature = "uuid-1"), uuid::Uuid); |
| 523 |
} |
| 524 |
|
| 525 |
#[allow(unused_macros)] |
| 526 |
macro_rules! as_legacy_binary_mod { |
| 527 |
($feat:meta, $uu:path, $rep:path) => { |
| 528 |
use crate::{uuid::UuidRepresentation, Binary}; |
| 529 |
use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; |
| 530 |
use std::result::Result; |
| 531 |
use $uu; |
| 532 |
|
| 533 |
/// Serializes a Uuid as a Binary in the legacy UUID format. |
| 534 |
#[cfg_attr(docsrs, doc($feat))] |
| 535 |
pub fn serialize<S: Serializer>(val: &Uuid, serializer: S) -> Result<S::Ok, S::Error> { |
| 536 |
let binary = Binary::from_uuid_with_representation(crate::uuid::Uuid::from(*val), $rep); |
| 537 |
binary.serialize(serializer) |
| 538 |
} |
| 539 |
|
| 540 |
/// Deserializes a Uuid from a Binary in the legacy UUID format. |
| 541 |
#[cfg_attr(docsrs, doc($feat))] |
| 542 |
pub fn deserialize<'de, D>(deserializer: D) -> Result<Uuid, D::Error> |
| 543 |
where |
| 544 |
D: Deserializer<'de>, |
| 545 |
{ |
| 546 |
let binary = Binary::deserialize(deserializer)?; |
| 547 |
let uuid = binary |
| 548 |
.to_uuid_with_representation($rep) |
| 549 |
.map_err(de::Error::custom)?; |
| 550 |
Ok(uuid.into()) |
| 551 |
} |
| 552 |
}; |
| 553 |
} |
| 554 |
|
| 555 |
/// Contains functions to serialize a [`uuid_0_8::Uuid`] to a [`crate::Binary`] in the legacy |
| 556 |
/// Java driver UUID format and deserialize [`uuid_0_8::Uuid`] from a [`crate::Binary`] in the |
| 557 |
/// legacy Java driver format. |
| 558 |
/// |
| 559 |
/// ```rust |
| 560 |
/// #[cfg(feature = "uuid-0_8")] |
| 561 |
/// # { |
| 562 |
/// use serde::{Serialize, Deserialize}; |
| 563 |
/// use uuid_0_8::Uuid; |
| 564 |
/// use bson::serde_helpers::uuid_as_java_legacy_binary; |
| 565 |
/// |
| 566 |
/// #[derive(Serialize, Deserialize)] |
| 567 |
/// struct Item { |
| 568 |
/// #[serde(with = "uuid_as_java_legacy_binary")] |
| 569 |
/// pub id: Uuid, |
| 570 |
/// } |
| 571 |
/// # } |
| 572 |
/// ``` |
| 573 |
#[cfg(feature = "uuid-0_8")] |
| 574 |
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))] |
| 575 |
pub mod uuid_as_java_legacy_binary { |
| 576 |
as_legacy_binary_mod!( |
| 577 |
cfg(feature = "uuid-0_8"), |
| 578 |
uuid_0_8::Uuid, |
| 579 |
UuidRepresentation::JavaLegacy |
| 580 |
); |
| 581 |
} |
| 582 |
|
| 583 |
/// Contains functions to serialize a [`uuid::Uuid`] to a [`crate::Binary`] in the legacy |
| 584 |
/// Java driver UUID format and deserialize [`uuid::Uuid`] from a [`crate::Binary`] in the legacy |
| 585 |
/// Java driver format. |
| 586 |
/// |
| 587 |
/// ```rust |
| 588 |
/// #[cfg(feature = "uuid-1")] |
| 589 |
/// # { |
| 590 |
/// use serde::{Serialize, Deserialize}; |
| 591 |
/// use uuid::Uuid; |
| 592 |
/// use bson::serde_helpers::uuid_1_as_java_legacy_binary; |
| 593 |
/// |
| 594 |
/// #[derive(Serialize, Deserialize)] |
| 595 |
/// struct Item { |
| 596 |
/// #[serde(with = "uuid_1_as_java_legacy_binary")] |
| 597 |
/// pub id: Uuid, |
| 598 |
/// } |
| 599 |
/// # } |
| 600 |
/// ``` |
| 601 |
#[cfg(feature = "uuid-1")] |
| 602 |
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-1")))] |
| 603 |
pub mod uuid_1_as_java_legacy_binary { |
| 604 |
as_legacy_binary_mod!( |
| 605 |
cfg(feature = "uuid-1"), |
| 606 |
uuid::Uuid, |
| 607 |
UuidRepresentation::JavaLegacy |
| 608 |
); |
| 609 |
} |
| 610 |
|
| 611 |
/// Contains functions to serialize a [`uuid_0_8::Uuid`] to a [`crate::Binary`] in the legacy Python |
| 612 |
/// driver UUID format and deserialize [`uuid_0_8::Uuid`] from a [`crate::Binary`] in the legacy |
| 613 |
/// Python driver format. |
| 614 |
/// |
| 615 |
/// ```rust |
| 616 |
/// # #[cfg(feature = "uuid-0_8")] |
| 617 |
/// # { |
| 618 |
/// use serde::{Serialize, Deserialize}; |
| 619 |
/// use uuid_0_8::Uuid; |
| 620 |
/// use bson::serde_helpers::uuid_as_python_legacy_binary; |
| 621 |
/// |
| 622 |
/// #[derive(Serialize, Deserialize)] |
| 623 |
/// struct Item { |
| 624 |
/// #[serde(with = "uuid_as_python_legacy_binary")] |
| 625 |
/// pub id: Uuid, |
| 626 |
/// } |
| 627 |
/// # } |
| 628 |
/// ``` |
| 629 |
#[cfg(feature = "uuid-0_8")] |
| 630 |
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))] |
| 631 |
pub mod uuid_as_python_legacy_binary { |
| 632 |
as_legacy_binary_mod!( |
| 633 |
cfg(feature = "uuid-0_8"), |
| 634 |
uuid_0_8::Uuid, |
| 635 |
UuidRepresentation::PythonLegacy |
| 636 |
); |
| 637 |
} |
| 638 |
|
| 639 |
/// Contains functions to serialize a [`uuid::Uuid`] to a [`crate::Binary`] in the legacy Python |
| 640 |
/// driver UUID format and deserialize [`uuid::Uuid`] from a [`crate::Binary`] in the legacy Python |
| 641 |
/// driver format. |
| 642 |
/// |
| 643 |
/// ```rust |
| 644 |
/// # #[cfg(feature = "uuid-1")] |
| 645 |
/// # { |
| 646 |
/// use serde::{Serialize, Deserialize}; |
| 647 |
/// use uuid::Uuid; |
| 648 |
/// use bson::serde_helpers::uuid_1_as_python_legacy_binary; |
| 649 |
/// |
| 650 |
/// #[derive(Serialize, Deserialize)] |
| 651 |
/// struct Item { |
| 652 |
/// #[serde(with = "uuid_1_as_python_legacy_binary")] |
| 653 |
/// pub id: Uuid, |
| 654 |
/// } |
| 655 |
/// # } |
| 656 |
/// ``` |
| 657 |
#[cfg(feature = "uuid-1")] |
| 658 |
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-1")))] |
| 659 |
pub mod uuid_1_as_python_legacy_binary { |
| 660 |
as_legacy_binary_mod!( |
| 661 |
cfg(feature = "uuid-1"), |
| 662 |
uuid::Uuid, |
| 663 |
UuidRepresentation::PythonLegacy |
| 664 |
); |
| 665 |
} |
| 666 |
|
| 667 |
/// Contains functions to serialize a [`uuid_0_8::Uuid`] to a [`crate::Binary`] in the legacy C# |
| 668 |
/// driver UUID format and deserialize [`uuid_0_8::Uuid`] from a [`crate::Binary`] in the legacy C# |
| 669 |
/// driver format. |
| 670 |
/// |
| 671 |
/// ```rust |
| 672 |
/// # #[cfg(feature = "uuid-0_8")] |
| 673 |
/// # { |
| 674 |
/// use serde::{Serialize, Deserialize}; |
| 675 |
/// use uuid_0_8::Uuid; |
| 676 |
/// use bson::serde_helpers::uuid_as_c_sharp_legacy_binary; |
| 677 |
/// |
| 678 |
/// #[derive(Serialize, Deserialize)] |
| 679 |
/// struct Item { |
| 680 |
/// #[serde(with = "uuid_as_c_sharp_legacy_binary")] |
| 681 |
/// pub id: Uuid, |
| 682 |
/// } |
| 683 |
/// # } |
| 684 |
/// ``` |
| 685 |
#[cfg(feature = "uuid-0_8")] |
| 686 |
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))] |
| 687 |
pub mod uuid_as_c_sharp_legacy_binary { |
| 688 |
as_legacy_binary_mod!( |
| 689 |
cfg(feature = "uuid-0_8"), |
| 690 |
uuid_0_8::Uuid, |
| 691 |
UuidRepresentation::CSharpLegacy |
| 692 |
); |
| 693 |
} |
| 694 |
|
| 695 |
/// Contains functions to serialize a [`uuid::Uuid`] to a [`crate::Binary`] in the legacy C# driver |
| 696 |
/// UUID format and deserialize [`uuid::Uuid`] from a [`crate::Binary`] in the legacy C# driver |
| 697 |
/// format. |
| 698 |
/// |
| 699 |
/// ```rust |
| 700 |
/// # #[cfg(feature = "uuid-1")] |
| 701 |
/// # { |
| 702 |
/// use serde::{Serialize, Deserialize}; |
| 703 |
/// use uuid::Uuid; |
| 704 |
/// use bson::serde_helpers::uuid_1_as_c_sharp_legacy_binary; |
| 705 |
/// |
| 706 |
/// #[derive(Serialize, Deserialize)] |
| 707 |
/// struct Item { |
| 708 |
/// #[serde(with = "uuid_1_as_c_sharp_legacy_binary")] |
| 709 |
/// pub id: Uuid, |
| 710 |
/// } |
| 711 |
/// # } |
| 712 |
/// ``` |
| 713 |
#[cfg(feature = "uuid-1")] |
| 714 |
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-1")))] |
| 715 |
pub mod uuid_1_as_c_sharp_legacy_binary { |
| 716 |
as_legacy_binary_mod!( |
| 717 |
cfg(feature = "uuid-1"), |
| 718 |
uuid::Uuid, |
| 719 |
UuidRepresentation::CSharpLegacy |
| 720 |
); |
| 721 |
} |
| 722 |
|
| 723 |
/// Contains functions to serialize a u32 as a bson::Timestamp and deserialize a u32 from a |
| 724 |
/// bson::Timestamp. The u32 should represent seconds since the Unix epoch. |
| 725 |
/// |
| 726 |
/// ```rust |
| 727 |
/// # use serde::{Serialize, Deserialize}; |
| 728 |
/// # use bson::serde_helpers::u32_as_timestamp; |
| 729 |
/// #[derive(Serialize, Deserialize)] |
| 730 |
/// struct Event { |
| 731 |
/// #[serde(with = "u32_as_timestamp")] |
| 732 |
/// pub time: u32, |
| 733 |
/// } |
| 734 |
/// ``` |
| 735 |
pub mod u32_as_timestamp { |
| 736 |
use crate::{Bson, Timestamp}; |
| 737 |
use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
| 738 |
use std::result::Result; |
| 739 |
|
| 740 |
/// Serializes a u32 as a bson::Timestamp. |
| 741 |
pub fn serialize<S: Serializer>(val: &u32, serializer: S) -> Result<S::Ok, S::Error> { |
| 742 |
let timestamp = Bson::Timestamp(Timestamp { |
| 743 |
time: *val, |
| 744 |
increment: 0, |
| 745 |
}); |
| 746 |
timestamp.serialize(serializer) |
| 747 |
} |
| 748 |
|
| 749 |
/// Deserializes a u32 from a bson::Timestamp. |
| 750 |
pub fn deserialize<'de, D>(deserializer: D) -> Result<u32, D::Error> |
| 751 |
where |
| 752 |
D: Deserializer<'de>, |
| 753 |
{ |
| 754 |
let timestamp = Timestamp::deserialize(deserializer)?; |
| 755 |
Ok(timestamp.time) |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
/// Contains functions to serialize a bson::Timestamp as a u32 and deserialize a bson::Timestamp |
| 760 |
/// from a u32. The u32 should represent seconds since the Unix epoch. Serialization will return an |
| 761 |
/// error if the Timestamp has a non-zero increment. |
| 762 |
/// |
| 763 |
/// ```rust |
| 764 |
/// # use serde::{Serialize, Deserialize}; |
| 765 |
/// # use bson::{serde_helpers::timestamp_as_u32, Timestamp}; |
| 766 |
/// #[derive(Serialize, Deserialize)] |
| 767 |
/// struct Item { |
| 768 |
/// #[serde(with = "timestamp_as_u32")] |
| 769 |
/// pub timestamp: Timestamp, |
| 770 |
/// } |
| 771 |
/// ``` |
| 772 |
pub mod timestamp_as_u32 { |
| 773 |
use crate::Timestamp; |
| 774 |
use serde::{ser, Deserialize, Deserializer, Serializer}; |
| 775 |
use std::result::Result; |
| 776 |
|
| 777 |
/// Serializes a bson::Timestamp as a u32. Returns an error if the conversion is lossy (i.e. the |
| 778 |
/// Timestamp has a non-zero increment). |
| 779 |
pub fn serialize<S: Serializer>(val: &Timestamp, serializer: S) -> Result<S::Ok, S::Error> { |
| 780 |
if val.increment != 0 { |
| 781 |
return Err(ser::Error::custom( |
| 782 |
"Cannot convert Timestamp with a non-zero increment to u32", |
| 783 |
)); |
| 784 |
} |
| 785 |
serializer.serialize_u32(val.time) |
| 786 |
} |
| 787 |
|
| 788 |
/// Deserializes a bson::Timestamp from a u32. |
| 789 |
pub fn deserialize<'de, D>(deserializer: D) -> Result<Timestamp, D::Error> |
| 790 |
where |
| 791 |
D: Deserializer<'de>, |
| 792 |
{ |
| 793 |
let time = u32::deserialize(deserializer)?; |
| 794 |
Ok(Timestamp { time, increment: 0 }) |
| 795 |
} |
| 796 |
} |