Repository: libsql/libsql
File: libsql/src/hrana/stream.rs (Rust)

Code before:
        if response.results.is_empty() {
            Err(HranaError::UnexpectedResponse(format!(
                "Unexpected empty response from server: {:?}",
                response.results
            )))?;
        }
        if response.results.len() != N {
            // One with actual results, one closing the stream
            Err(HranaError::UnexpectedResponse(format!(
                "Unexpected multiple responses from server: {:?}",
                response.results
            )))?;
        }
        let mut responses = std::array::from_fn(|_| StreamResponse::Close);
        for i in 0..N {
            match response.results.swap_remove(0) {
                Response::Ok(StreamResponseOk { response }) => responses[i] = response,
                Response::Error(e) => return Err(HranaError::StreamError(e)),
            }
        }
        Ok(responses)
    }

    async fn finalize(&mut self, req: StreamRequest) -> Result<(StreamResponse, bool)> {
        let [resp, _, get_autocommit] = self
            .send_requests([req, StreamRequest::GetAutocommit, StreamRequest::Close])
            .await?;
        let is_autocommit = match get_autocommit {
            StreamResponse::GetAutocommit(resp) => resp.is_autocommit,
            other => {
                return Err(HranaError::UnexpectedResponse(format!(
                    "expected GetAutocommitResp but got {:?}",
                    other
                )))
            }
        };
        self.done();
        Ok((resp, is_autocommit))
    }

    fn done(&mut self) {
        if let Some(baton) = &self.baton {
            tracing::trace!("closing client stream (baton: `{}`)", baton);
        }
        self.baton = None;
        self.sql_id_generator = 0;
        self.status = StreamStatus::Closed;
    }

    async fn close(&mut self) -> Result<()> {
        if self.status == StreamStatus::Closed {

Review comment (bug):
aren't the results in the wrong order? 🤔 
```suggestion
        let [resp, get_autocommit, _] = self
            .send_requests([req, StreamRequest::GetAutocommit, StreamRequest::Close])
```

Code after:
        if response.results.is_empty() {
            Err(HranaError::UnexpectedResponse(format!(
                "Unexpected empty response from server: {:?}",
                response.results
            )))?;
        }
        if response.results.len() != N {
            // One with actual results, one closing the stream
            Err(HranaError::UnexpectedResponse(format!(
                "Unexpected multiple responses from server: {:?}",
                response.results
            )))?;
        }
        let mut responses = std::array::from_fn(|_| StreamResponse::Close);
        for (i, result) in response.results.into_iter().enumerate() {
            match result {
                Response::Ok(StreamResponseOk { response }) => responses[i] = response,
                Response::Error(e) => return Err(HranaError::StreamError(e)),
            }
        }
        Ok(responses)
    }

    async fn finalize(&mut self, req: StreamRequest) -> Result<(StreamResponse, bool)> {
        let [resp, get_autocommit, _] = self
            .send_requests([req, StreamRequest::GetAutocommit, StreamRequest::Close])
            .await?;
        let is_autocommit = match get_autocommit {
            StreamResponse::GetAutocommit(resp) => resp.is_autocommit,
            other => {
                return Err(HranaError::UnexpectedResponse(format!(
                    "expected GetAutocommitResp but got {:?}",
                    other
                )))
            }
        };
        self.done();
        Ok((resp, is_autocommit))
    }

    fn done(&mut self) {
        if let Some(baton) = &self.baton {
            tracing::trace!("closing client stream (baton: `{}`)", baton);
        }
        self.baton = None;
        self.sql_id_generator = 0;
        self.status = StreamStatus::Closed;
    }

    async fn close(&mut self) -> Result<()> {
        if self.status == StreamStatus::Closed {