pbbson 0.1.3

Utilities for pbjson to BSON conversion
use bson::{Bson, DateTime, Document};
use prost::Message;
use regex::Regex;
use serde::Serialize;
use tonic::Status;

pub fn pb_to_bson<T: Message + Serialize>(message: T) -> Result<Document, Status> {
    let bson_value = bson::to_bson(&message).map_err(|e| Status::invalid_argument(e.to_string()))?;
    let mut doc = match bson_value.as_document() {
        Some(doc) => doc.clone(),
        None => {
            return Err(Status::invalid_argument(
                "Protobuf message must be able to parse into a BSON Document",
            ));
        }
    };

    // Convert ISO8601 strings to BSON DateTime
    let re = Regex::new(r#"^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T"#).unwrap();
    for (k, v) in doc.clone().iter() {
        if let Bson::String(s) = v {
            if re.is_match(s) {
                if let Ok(dt) = DateTime::parse_rfc3339_str(s) {
                    doc.insert(k, dt);
                }
            }
        }
    }

    Ok(doc)
}