diff --git a/src/rust/amadeus-rs/lkt-lib/src/connexion.rs b/src/rust/amadeus-rs/lkt-lib/src/connexion.rs index e148df5d8ffd52fb8521a0111ddd2840f215b4b8..44325977a0431ebfd69caf86d4fccecb87fa8a2e 100644 --- a/src/rust/amadeus-rs/lkt-lib/src/connexion.rs +++ b/src/rust/amadeus-rs/lkt-lib/src/connexion.rs @@ -5,7 +5,7 @@ use crate::{ constants, query::LektorQuery, query::LektorQueryLineType, response::LektorFormatedResponse, LektorResponse, }; -use log::error; +use log::*; use std::{ io::{self, BufRead, BufReader, Write}, net::TcpStream, @@ -48,9 +48,8 @@ impl std::fmt::Debug for LektorConnexion { } impl LektorConnexion { - pub fn new(hostname: String, port: i16) -> io::Result<Self> { - let result = TcpStream::connect(format!("{}:{}", hostname, port)); - match result { + pub fn new(hostname: impl AsRef<str>, port: i16) -> io::Result<Self> { + match TcpStream::connect(format!("{}:{}", hostname.as_ref(), port)) { Ok(lektord) => { let mut ret: Self = Self { stream: lektord.try_clone().unwrap(), @@ -93,17 +92,15 @@ impl LektorConnexion { self.write_string(query.to_string())?; loop { match self.read_replies() { - Ok((res, None)) => { - previous_ret.extend(res); - return Ok(()); - } + Err(e) => return Err(e), + Ok((res, _)) if res.is_empty() => return Ok(()), + Ok((res, None)) => return Ok(previous_ret.extend(res)), Ok((res, Some(cont))) => { previous_ret.extend(res); self.write_string( LektorQuery::create_continuation(query.clone(), cont).to_string(), )?; } - Err(e) => return Err(e), } } } @@ -112,6 +109,7 @@ impl LektorConnexion { let error_return_value = io::Result::Err(io::Error::from(io::ErrorKind::Other)); let mut ret: Vec<String> = Vec::new(); let mut reply_line: String = String::new(); + let mut continuation = None; loop { reply_line.clear(); match self.reader.read_line(&mut reply_line) { @@ -125,13 +123,11 @@ impl LektorConnexion { } let msg = reply_line.trim(); match LektorQueryLineType::from_str(msg) { - Ok(LektorQueryLineType::Ok) => return Ok((ret, None)), + Ok(LektorQueryLineType::Ok) => return Ok((ret, continuation)), Ok(LektorQueryLineType::Ack) => return error_return_value, Ok(LektorQueryLineType::Data) => ret.push(msg.to_string()), Ok(LektorQueryLineType::ListOk) => continue, - Ok(LektorQueryLineType::Continuation(cont)) => { - return Ok((ret, Some(cont))) - } + Ok(LektorQueryLineType::Continuation(cont)) => continuation = Some(cont), Err(_) => { return Err(io::Error::new( io::ErrorKind::Other, @@ -144,7 +140,8 @@ impl LektorConnexion { } } - fn write_string(&mut self, buffer: String) -> io::Result<()> { + fn write_string(&mut self, buffer: impl AsRef<str>) -> io::Result<()> { + let buffer = buffer.as_ref(); if buffer.len() >= constants::LKT_MESSAGE_MAX { Err(io::Error::new( io::ErrorKind::InvalidData, diff --git a/src/rust/amadeus-rs/lkt-lib/src/query.rs b/src/rust/amadeus-rs/lkt-lib/src/query.rs index ef494bb8441ac51745dd94770e9a1f5c328d4969..9abc3e277784cdab5719a19a02552a6b0ccf688a 100644 --- a/src/rust/amadeus-rs/lkt-lib/src/query.rs +++ b/src/rust/amadeus-rs/lkt-lib/src/query.rs @@ -174,7 +174,7 @@ impl ToString for LektorQuery { InsertKara(uri) => format!("__insert {}\n", uri.to_string()), AddKara(uri) => format!("add {}\n", uri.to_string()), - Continuation(cont, query) => format!("{} {}", cont, query.to_owned().to_string()), + Continuation(cont, query) => format!("{cont} {}", query.to_owned().to_string()), } } } diff --git a/src/rust/amadeus-rs/lkt-lib/src/response.rs b/src/rust/amadeus-rs/lkt-lib/src/response.rs index 5f97291ef18d6f3e08e0397837e82d6332251586..e808a301e0b205383cf3d9538f00c1dc99db149a 100644 --- a/src/rust/amadeus-rs/lkt-lib/src/response.rs +++ b/src/rust/amadeus-rs/lkt-lib/src/response.rs @@ -66,8 +66,7 @@ impl TryFrom<Vec<String>> for LektorFormatedResponse { fn try_from(vec: Vec<String>) -> Result<Self, Self::Error> { let (mut content, mut raw_content) = (Vec::new(), Vec::new()); for line in vec { - let key_pair: Vec<&str> = line.splitn(2, ':').collect(); - match key_pair[..] { + match line.splitn(2, ':').collect::<Vec<&str>>()[..] { [key, value] => content.push((key.trim().to_lowercase(), value.trim().to_string())), _ => raw_content.push(line), }