Simplify documents! macro to reduce compile times

This commit is contained in:
Loïc Lecrenier 2022-08-10 09:32:03 +02:00 committed by Loïc Lecrenier
parent 98fc093823
commit 6fbf5dac68

View File

@ -152,30 +152,33 @@ impl fmt::Display for Error {
impl std::error::Error for Error {} impl std::error::Error for Error {}
/// Macro used to generate documents, with the same syntax as `serde_json::json`
#[cfg(test)] #[cfg(test)]
macro_rules! documents { pub fn objects_from_json_value(json: serde_json::Value) -> Vec<crate::Object> {
($data:tt) => {{ let documents = match json {
let documents = serde_json::json!($data);
let documents = match documents {
object @ serde_json::Value::Object(_) => vec![object], object @ serde_json::Value::Object(_) => vec![object],
serde_json::Value::Array(objects) => objects, serde_json::Value::Array(objects) => objects,
invalid => { invalid => {
panic!("an array of objects must be specified, {:#?} is not an array", invalid) panic!("an array of objects must be specified, {:#?} is not an array", invalid)
} }
}; };
let mut objects = vec![];
let mut builder = crate::documents::DocumentsBatchBuilder::new(Vec::new());
for document in documents { for document in documents {
let object = match document { let object = match document {
serde_json::Value::Object(object) => object, serde_json::Value::Object(object) => object,
invalid => panic!("an object must be specified, {:#?} is not an object", invalid), invalid => panic!("an object must be specified, {:#?} is not an object", invalid),
}; };
builder.append_json_object(&object).unwrap(); objects.push(object);
} }
objects
}
let vector = builder.into_inner().unwrap(); /// Macro used to generate documents, with the same syntax as `serde_json::json`
crate::documents::DocumentsBatchReader::from_reader(std::io::Cursor::new(vector)).unwrap() #[cfg(test)]
macro_rules! documents {
($data:tt) => {{
let documents = serde_json::json!($data);
let documents = $crate::documents::objects_from_json_value(documents);
$crate::documents::documents_batch_reader_from_objects(documents)
}}; }};
} }
@ -187,7 +190,8 @@ pub fn documents_batch_reader_from_objects(
for object in objects { for object in objects {
builder.append_json_object(&object).unwrap(); builder.append_json_object(&object).unwrap();
} }
DocumentsBatchReader::from_reader(std::io::Cursor::new(builder.into_inner().unwrap())).unwrap() let vector = builder.into_inner().unwrap();
DocumentsBatchReader::from_reader(std::io::Cursor::new(vector)).unwrap()
} }
#[cfg(test)] #[cfg(test)]