2019-10-03 17:49:13 +08:00
|
|
|
use std::hash::{Hash, Hasher};
|
|
|
|
|
|
|
|
use crate::DocumentId;
|
|
|
|
use serde::{ser, Serialize};
|
|
|
|
use serde_json::Value;
|
|
|
|
use siphasher::sip::SipHasher;
|
|
|
|
|
2019-10-18 19:05:28 +08:00
|
|
|
use super::{ConvertToString, SerializerError};
|
2019-10-03 17:49:13 +08:00
|
|
|
|
|
|
|
pub fn extract_document_id<D>(
|
|
|
|
identifier: &str,
|
|
|
|
document: &D,
|
|
|
|
) -> Result<Option<DocumentId>, SerializerError>
|
2019-10-18 19:05:28 +08:00
|
|
|
where
|
|
|
|
D: serde::Serialize,
|
2019-10-03 17:49:13 +08:00
|
|
|
{
|
|
|
|
let serializer = ExtractDocumentId { identifier };
|
|
|
|
document.serialize(serializer)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn value_to_string(value: &Value) -> Option<String> {
|
|
|
|
match value {
|
|
|
|
Value::Null => None,
|
|
|
|
Value::Bool(_) => None,
|
|
|
|
Value::Number(value) => Some(value.to_string()),
|
|
|
|
Value::String(value) => Some(value.to_string()),
|
|
|
|
Value::Array(_) => None,
|
|
|
|
Value::Object(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn compute_document_id<H: Hash>(t: H) -> DocumentId {
|
|
|
|
let mut s = SipHasher::new();
|
|
|
|
t.hash(&mut s);
|
|
|
|
let hash = s.finish();
|
|
|
|
DocumentId(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ExtractDocumentId<'a> {
|
|
|
|
identifier: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ser::Serializer for ExtractDocumentId<'a> {
|
|
|
|
type Ok = Option<DocumentId>;
|
|
|
|
type Error = SerializerError;
|
|
|
|
type SerializeSeq = ser::Impossible<Self::Ok, Self::Error>;
|
|
|
|
type SerializeTuple = ser::Impossible<Self::Ok, Self::Error>;
|
|
|
|
type SerializeTupleStruct = ser::Impossible<Self::Ok, Self::Error>;
|
|
|
|
type SerializeTupleVariant = ser::Impossible<Self::Ok, Self::Error>;
|
|
|
|
type SerializeMap = ExtractDocumentIdMapSerializer<'a>;
|
|
|
|
type SerializeStruct = ExtractDocumentIdStructSerializer<'a>;
|
|
|
|
type SerializeStructVariant = ser::Impossible<Self::Ok, Self::Error>;
|
|
|
|
|
|
|
|
forward_to_unserializable_type! {
|
|
|
|
bool => serialize_bool,
|
|
|
|
char => serialize_char,
|
|
|
|
|
|
|
|
i8 => serialize_i8,
|
|
|
|
i16 => serialize_i16,
|
|
|
|
i32 => serialize_i32,
|
|
|
|
i64 => serialize_i64,
|
|
|
|
|
|
|
|
u8 => serialize_u8,
|
|
|
|
u16 => serialize_u16,
|
|
|
|
u32 => serialize_u32,
|
|
|
|
u64 => serialize_u64,
|
|
|
|
|
|
|
|
f32 => serialize_f32,
|
|
|
|
f64 => serialize_f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_str(self, _value: &str) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Err(SerializerError::UnserializableType { type_name: "str" })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_bytes(self, _value: &[u8]) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Err(SerializerError::UnserializableType { type_name: "&[u8]" })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
|
2019-10-18 19:05:28 +08:00
|
|
|
Err(SerializerError::UnserializableType {
|
|
|
|
type_name: "Option",
|
|
|
|
})
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<Self::Ok, Self::Error>
|
2019-10-18 19:05:28 +08:00
|
|
|
where
|
|
|
|
T: Serialize,
|
2019-10-03 17:49:13 +08:00
|
|
|
{
|
2019-10-18 19:05:28 +08:00
|
|
|
Err(SerializerError::UnserializableType {
|
|
|
|
type_name: "Option",
|
|
|
|
})
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Err(SerializerError::UnserializableType { type_name: "()" })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
|
2019-10-18 19:05:28 +08:00
|
|
|
Err(SerializerError::UnserializableType {
|
|
|
|
type_name: "unit struct",
|
|
|
|
})
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_unit_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
2019-10-18 19:05:28 +08:00
|
|
|
_variant: &'static str,
|
|
|
|
) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Err(SerializerError::UnserializableType {
|
|
|
|
type_name: "unit variant",
|
|
|
|
})
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_newtype_struct<T: ?Sized>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
2019-10-18 19:05:28 +08:00
|
|
|
value: &T,
|
2019-10-03 17:49:13 +08:00
|
|
|
) -> Result<Self::Ok, Self::Error>
|
2019-10-18 19:05:28 +08:00
|
|
|
where
|
|
|
|
T: Serialize,
|
2019-10-03 17:49:13 +08:00
|
|
|
{
|
|
|
|
value.serialize(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_newtype_variant<T: ?Sized>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str,
|
2019-10-18 19:05:28 +08:00
|
|
|
_value: &T,
|
2019-10-03 17:49:13 +08:00
|
|
|
) -> Result<Self::Ok, Self::Error>
|
2019-10-18 19:05:28 +08:00
|
|
|
where
|
|
|
|
T: Serialize,
|
2019-10-03 17:49:13 +08:00
|
|
|
{
|
2019-10-18 19:05:28 +08:00
|
|
|
Err(SerializerError::UnserializableType {
|
|
|
|
type_name: "newtype variant",
|
|
|
|
})
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
|
2019-10-18 19:05:28 +08:00
|
|
|
Err(SerializerError::UnserializableType {
|
|
|
|
type_name: "sequence",
|
|
|
|
})
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
|
|
|
|
Err(SerializerError::UnserializableType { type_name: "tuple" })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_tuple_struct(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
2019-10-18 19:05:28 +08:00
|
|
|
_len: usize,
|
|
|
|
) -> Result<Self::SerializeTupleStruct, Self::Error> {
|
|
|
|
Err(SerializerError::UnserializableType {
|
|
|
|
type_name: "tuple struct",
|
|
|
|
})
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_tuple_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str,
|
2019-10-18 19:05:28 +08:00
|
|
|
_len: usize,
|
|
|
|
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
|
|
|
Err(SerializerError::UnserializableType {
|
|
|
|
type_name: "tuple variant",
|
|
|
|
})
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
|
|
|
let serializer = ExtractDocumentIdMapSerializer {
|
|
|
|
identifier: self.identifier,
|
|
|
|
document_id: None,
|
|
|
|
current_key_name: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(serializer)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_struct(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
2019-10-18 19:05:28 +08:00
|
|
|
_len: usize,
|
|
|
|
) -> Result<Self::SerializeStruct, Self::Error> {
|
2019-10-03 17:49:13 +08:00
|
|
|
let serializer = ExtractDocumentIdStructSerializer {
|
|
|
|
identifier: self.identifier,
|
|
|
|
document_id: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(serializer)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_struct_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str,
|
2019-10-18 19:05:28 +08:00
|
|
|
_len: usize,
|
|
|
|
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
|
|
|
Err(SerializerError::UnserializableType {
|
|
|
|
type_name: "struct variant",
|
|
|
|
})
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ExtractDocumentIdMapSerializer<'a> {
|
|
|
|
identifier: &'a str,
|
|
|
|
document_id: Option<DocumentId>,
|
|
|
|
current_key_name: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ser::SerializeMap for ExtractDocumentIdMapSerializer<'a> {
|
|
|
|
type Ok = Option<DocumentId>;
|
|
|
|
type Error = SerializerError;
|
|
|
|
|
|
|
|
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
|
2019-10-18 19:05:28 +08:00
|
|
|
where
|
|
|
|
T: Serialize,
|
2019-10-03 17:49:13 +08:00
|
|
|
{
|
|
|
|
let key = key.serialize(ConvertToString)?;
|
|
|
|
self.current_key_name = Some(key);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
2019-10-18 19:05:28 +08:00
|
|
|
where
|
|
|
|
T: Serialize,
|
2019-10-03 17:49:13 +08:00
|
|
|
{
|
|
|
|
let key = self.current_key_name.take().unwrap();
|
|
|
|
self.serialize_entry(&key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_entry<K: ?Sized, V: ?Sized>(
|
|
|
|
&mut self,
|
|
|
|
key: &K,
|
2019-10-18 19:05:28 +08:00
|
|
|
value: &V,
|
2019-10-03 17:49:13 +08:00
|
|
|
) -> Result<(), Self::Error>
|
2019-10-18 19:05:28 +08:00
|
|
|
where
|
|
|
|
K: Serialize,
|
|
|
|
V: Serialize,
|
2019-10-03 17:49:13 +08:00
|
|
|
{
|
|
|
|
let key = key.serialize(ConvertToString)?;
|
|
|
|
|
|
|
|
if self.identifier == key {
|
|
|
|
let value = serde_json::to_string(value).and_then(|s| serde_json::from_str(&s))?;
|
|
|
|
match value_to_string(&value).map(|s| compute_document_id(&s)) {
|
|
|
|
Some(document_id) => self.document_id = Some(document_id),
|
|
|
|
None => return Err(SerializerError::InvalidDocumentIdType),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(self.document_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ExtractDocumentIdStructSerializer<'a> {
|
|
|
|
identifier: &'a str,
|
|
|
|
document_id: Option<DocumentId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ser::SerializeStruct for ExtractDocumentIdStructSerializer<'a> {
|
|
|
|
type Ok = Option<DocumentId>;
|
|
|
|
type Error = SerializerError;
|
|
|
|
|
|
|
|
fn serialize_field<T: ?Sized>(
|
|
|
|
&mut self,
|
|
|
|
key: &'static str,
|
2019-10-18 19:05:28 +08:00
|
|
|
value: &T,
|
2019-10-03 17:49:13 +08:00
|
|
|
) -> Result<(), Self::Error>
|
2019-10-18 19:05:28 +08:00
|
|
|
where
|
|
|
|
T: Serialize,
|
2019-10-03 17:49:13 +08:00
|
|
|
{
|
|
|
|
if self.identifier == key {
|
|
|
|
let value = serde_json::to_string(value).and_then(|s| serde_json::from_str(&s))?;
|
|
|
|
match value_to_string(&value).map(compute_document_id) {
|
|
|
|
Some(document_id) => self.document_id = Some(document_id),
|
|
|
|
None => return Err(SerializerError::InvalidDocumentIdType),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(self.document_id)
|
|
|
|
}
|
|
|
|
}
|