Skip to content

Commit

Permalink
Simplified FirestoreVector serialization code
Browse files Browse the repository at this point in the history
  • Loading branch information
abdolence committed Apr 12, 2024
1 parent 2ef983a commit e8f114c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 266 deletions.
4 changes: 3 additions & 1 deletion src/firestore_serde/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ impl serde::Serializer for FirestoreValueSerializer {
)
}
crate::firestore_serde::vector_serializers::FIRESTORE_VECTOR_TYPE_TAG_TYPE => {
crate::firestore_serde::vector_serializers::serialize_vector_for_firestore(value)
crate::firestore_serde::vector_serializers::serialize_vector_for_firestore(
self, value,
)
}
_ => value.serialize(self),
}
Expand Down
270 changes: 5 additions & 265 deletions src/firestore_serde/vector_serializers.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::errors::{FirestoreError, FirestoreSerializationError};
use crate::firestore_serde::serializer::SerializeVec;
use crate::errors::FirestoreError;
use crate::firestore_serde::serializer::FirestoreValueSerializer;
use crate::FirestoreValue;
use serde::de::{MapAccess, Visitor};
use serde::{Deserializer, Serialize, Serializer};
use serde::{Deserializer, Serialize};

pub(crate) const FIRESTORE_VECTOR_TYPE_TAG_TYPE: &str = "FirestoreVector";

Expand All @@ -25,270 +25,10 @@ where
}

pub fn serialize_vector_for_firestore<T: ?Sized + Serialize>(
firestore_value_serializer: FirestoreValueSerializer,
value: &T,
) -> Result<FirestoreValue, FirestoreError> {
struct VectorSerializer;

impl Serializer for VectorSerializer {
type Ok = FirestoreValue;
type Error = FirestoreError;
type SerializeSeq = crate::firestore_serde::serializer::SerializeVec;
type SerializeTuple = crate::firestore_serde::serializer::SerializeVec;
type SerializeTupleStruct = crate::firestore_serde::serializer::SerializeVec;
type SerializeTupleVariant = crate::firestore_serde::serializer::SerializeTupleVariant;
type SerializeMap = crate::firestore_serde::serializer::SerializeMap;
type SerializeStruct = crate::firestore_serde::serializer::SerializeMap;
type SerializeStructVariant = crate::firestore_serde::serializer::SerializeStructVariant;

fn serialize_bool(self, _v: bool) -> Result<Self::Ok, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type",
),
))
}

fn serialize_i8(self, _v: i8) -> Result<Self::Ok, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type",
),
))
}

fn serialize_i16(self, _v: i16) -> Result<Self::Ok, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type",
),
))
}

fn serialize_i32(self, _v: i32) -> Result<Self::Ok, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type",
),
))
}

fn serialize_i64(self, _v: i64) -> Result<Self::Ok, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type",
),
))
}

fn serialize_u8(self, _v: u8) -> Result<Self::Ok, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type",
),
))
}

fn serialize_u16(self, _v: u16) -> Result<Self::Ok, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type",
),
))
}

fn serialize_u32(self, _v: u32) -> Result<Self::Ok, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type",
),
))
}

fn serialize_u64(self, _v: u64) -> Result<Self::Ok, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type",
),
))
}

fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type",
),
))
}

fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type",
),
))
}

fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type",
),
))
}

fn serialize_str(self, _v: &str) -> Result<Self::Ok, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type",
),
))
}

fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type",
),
))
}

fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
Ok(FirestoreValue::from(
gcloud_sdk::google::firestore::v1::Value { value_type: None },
))
}

fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
{
value.serialize(self)
}

fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
Ok(FirestoreValue::from(
gcloud_sdk::google::firestore::v1::Value { value_type: None },
))
}

fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
self.serialize_unit()
}

fn serialize_unit_variant(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
) -> Result<Self::Ok, Self::Error> {
self.serialize_str(variant)
}

fn serialize_newtype_struct<T: ?Sized>(
self,
_name: &'static str,
value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
{
value.serialize(self)
}

fn serialize_newtype_variant<T: ?Sized>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
{
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type: newtype_variant",
),
))
}

fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
Ok(SerializeVec {
none_as_null: false,
vec: Vec::with_capacity(len.unwrap_or(0)),
})
}

fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type: tuple",
),
))
}

fn serialize_tuple_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleStruct, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type: tuple_struct",
),
))
}

fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type: tuple_variant",
),
))
}

fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type: map",
),
))
}

fn serialize_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeStruct, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type: struct",
),
))
}

fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Vector serializer doesn't support this type: struct_variant",
),
))
}
}

let value_with_array = value.serialize(VectorSerializer {})?;
let value_with_array = value.serialize(firestore_value_serializer)?;

Ok(FirestoreValue::from(
gcloud_sdk::google::firestore::v1::Value {
Expand Down

0 comments on commit e8f114c

Please sign in to comment.