Fix formatting and apply clippy suggestion

This commit is contained in:
Andrey "MOU" Larionov 2022-10-10 11:04:46 +02:00
parent 9dbc71cb6d
commit 343a677566
No known key found for this signature in database
GPG Key ID: 5FF293FC94C01D6A
4 changed files with 28 additions and 17 deletions

View File

@ -1,8 +1,8 @@
use std::io::Write;
use actix_http::header::TryIntoHeaderPair; use actix_http::header::TryIntoHeaderPair;
use bytes::Bytes; use bytes::Bytes;
use flate2::write::{GzEncoder, ZlibEncoder}; use flate2::write::{GzEncoder, ZlibEncoder};
use flate2::Compression; use flate2::Compression;
use std::io::Write;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub enum Encoder { pub enum Encoder {
@ -17,18 +17,24 @@ impl Encoder {
match self { match self {
Self::Gzip => { Self::Gzip => {
let mut encoder = GzEncoder::new(Vec::new(), Compression::default()); let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
encoder.write_all(&body.into()).expect("Failed to encode request body"); encoder
.write_all(&body.into())
.expect("Failed to encode request body");
encoder.finish().expect("Failed to encode request body") encoder.finish().expect("Failed to encode request body")
} }
Self::Deflate => { Self::Deflate => {
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default()); let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
encoder.write_all(&body.into()).expect("Failed to encode request body"); encoder
.write_all(&body.into())
.expect("Failed to encode request body");
encoder.finish().unwrap() encoder.finish().unwrap()
} }
Self::Plain => Vec::from(body.into()), Self::Plain => Vec::from(body.into()),
Self::Brotli => { Self::Brotli => {
let mut encoder = brotli::CompressorWriter::new(Vec::new(), 32 * 1024, 3, 22); let mut encoder = brotli::CompressorWriter::new(Vec::new(), 32 * 1024, 3, 22);
encoder.write_all(&body.into()).expect("Failed to encode request body"); encoder
.write_all(&body.into())
.expect("Failed to encode request body");
encoder.flush().expect("Failed to encode request body"); encoder.flush().expect("Failed to encode request body");
encoder.into_inner() encoder.into_inner()
} }
@ -45,6 +51,8 @@ impl Encoder {
} }
pub fn iterator() -> impl Iterator<Item = Self> { pub fn iterator() -> impl Iterator<Item = Self> {
[Self::Plain, Self::Gzip, Self::Deflate, Self::Brotli].iter().copied() [Self::Plain, Self::Gzip, Self::Deflate, Self::Brotli]
.iter()
.copied()
} }
} }

View File

@ -190,7 +190,9 @@ impl Index<'_> {
pub async fn update_settings(&self, settings: Value) -> (Value, StatusCode) { pub async fn update_settings(&self, settings: Value) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings", urlencode(self.uid.as_ref())); let url = format!("/indexes/{}/settings", urlencode(self.uid.as_ref()));
self.service.patch_encoded(url, settings, self.encoder).await self.service
.patch_encoded(url, settings, self.encoder)
.await
} }
pub async fn delete_settings(&self) -> (Value, StatusCode) { pub async fn delete_settings(&self) -> (Value, StatusCode) {
@ -230,7 +232,11 @@ impl Index<'_> {
pub async fn search_get(&self, query: Value) -> (Value, StatusCode) { pub async fn search_get(&self, query: Value) -> (Value, StatusCode) {
let params = yaup::to_string(&query).unwrap(); let params = yaup::to_string(&query).unwrap();
let url = format!("/indexes/{}/search?{}", urlencode(self.uid.as_ref()), params); let url = format!(
"/indexes/{}/search?{}",
urlencode(self.uid.as_ref()),
params
);
self.service.get(url).await self.service.get(url).await
} }

View File

@ -1,10 +1,10 @@
use crate::common::{GetAllDocumentsOptions, Server}; use crate::common::{GetAllDocumentsOptions, Server};
use actix_web::test; use actix_web::test;
use crate::common::encoder::Encoder;
use meilisearch_http::{analytics, create_app}; use meilisearch_http::{analytics, create_app};
use serde_json::{json, Value}; use serde_json::{json, Value};
use time::{format_description::well_known::Rfc3339, OffsetDateTime}; use time::{format_description::well_known::Rfc3339, OffsetDateTime};
use crate::common::encoder::Encoder;
/// This is the basic usage of our API and every other tests uses the content-type application/json /// This is the basic usage of our API and every other tests uses the content-type application/json
#[actix_rt::test] #[actix_rt::test]
@ -166,17 +166,16 @@ async fn add_single_document_with_every_encoding() {
)) ))
.await; .await;
// post // post
let mut task_uid = 0;
let document = serde_json::to_string(&document).unwrap(); let document = serde_json::to_string(&document).unwrap();
for encoder in Encoder::iterator() { for (task_uid, encoder) in Encoder::iterator().enumerate() {
let mut req = test::TestRequest::post() let mut req = test::TestRequest::post()
.uri("/indexes/dog/documents") .uri("/indexes/dog/documents")
.set_payload(encoder.encode(document.clone())) .set_payload(encoder.encode(document.clone()))
.insert_header(("content-type", "application/json")); .insert_header(("content-type", "application/json"));
req = match encoder.header() { req = match encoder.header() {
Some(header) => req.insert_header(header), Some(header) => req.insert_header(header),
None => req None => req,
}; };
let req = req.to_request(); let req = req.to_request();
let res = test::call_service(&app, req).await; let res = test::call_service(&app, req).await;
@ -185,9 +184,7 @@ async fn add_single_document_with_every_encoding() {
let response: Value = serde_json::from_slice(&body).unwrap_or_default(); let response: Value = serde_json::from_slice(&body).unwrap_or_default();
assert_eq!(status_code, 202); assert_eq!(status_code, 202);
assert_eq!(response["taskUid"], task_uid); assert_eq!(response["taskUid"], task_uid);
task_uid += 1;
} }
} }
/// any other content-type is must be refused /// any other content-type is must be refused

View File

@ -1,7 +1,7 @@
use crate::common::{GetAllDocumentsOptions, Server}; use crate::common::{GetAllDocumentsOptions, Server};
use serde_json::json;
use crate::common::encoder::Encoder; use crate::common::encoder::Encoder;
use serde_json::json;
#[actix_rt::test] #[actix_rt::test]
async fn error_document_update_create_index_bad_uid() { async fn error_document_update_create_index_bad_uid() {