Repository: libsql/libsql
File: libsql-server/src/hrana/ws/conn.rs (Rust)

Code before:
async fn handle_client_msg(conn: &mut Conn, client_msg: proto::ClientMsg) -> Result<bool> {
    tracing::trace!("Received client msg: {:?}", client_msg);
    match client_msg {
        proto::ClientMsg::None => bail!(ProtocolError::NoneClientMsg),
        proto::ClientMsg::Hello(msg) => handle_hello_msg(conn, msg.jwt).await,
        proto::ClientMsg::Request(msg) => match msg.request {
            Some(request) => handle_request_msg(conn, msg.request_id, request).await,
            None => bail!(ProtocolError::NoneRequest),
        },
    }
}

async fn handle_hello_msg(conn: &mut Conn, jwt: Option<String>) -> Result<bool> {
    let hello_res = match conn.session.as_mut() {
        None => {
            conn.session = session::handle_hello(&conn.server, jwt, conn.namespace.clone())
                .await
                .map(|auth| session::Session::new(auth, conn.version))
                .map(|s| Some(s))?;
                Ok(())
        }
        Some(session) => {
            if session.version < Version::Hrana2 {
                bail!(ProtocolError::NotSupported {what: "Repeated hello message", min_version: Version::Hrana2,})
            }
            session.auth = session::handle_hello(&conn.server, jwt, conn.namespace.clone()).await?;
            Ok(())
        }
    };

    match hello_res {
        Ok(_) => {
            send_msg(conn, &proto::ServerMsg::HelloOk(proto::HelloOkMsg {})).await?;
            Ok(true)
        }
        Err(err) => match downcast_error(err) {
            Ok(error) => {
                send_msg(
                    conn,
                    &proto::ServerMsg::HelloError(proto::HelloErrorMsg { error }),
                )
                .await?;
                Ok(false)
            }
            Err(err) => Err(err),
        },
    }
}

async fn handle_request_msg(
    conn: &mut Conn,

Review comment (suggestion):
Same question about the `?`. Previously this match would never return from `handle_hello_msg`.

Code after:
async fn handle_client_msg(conn: &mut Conn, client_msg: proto::ClientMsg) -> Result<bool> {
    tracing::trace!("Received client msg: {:?}", client_msg);
    match client_msg {
        proto::ClientMsg::None => bail!(ProtocolError::NoneClientMsg),
        proto::ClientMsg::Hello(msg) => handle_hello_msg(conn, msg.jwt).await,
        proto::ClientMsg::Request(msg) => match msg.request {
            Some(request) => handle_request_msg(conn, msg.request_id, request).await,
            None => bail!(ProtocolError::NoneRequest),
        },
    }
}

async fn handle_hello_msg(conn: &mut Conn, jwt: Option<String>) -> Result<bool> {
    let auth = session::handle_hello(&conn.server, jwt, conn.namespace.clone()).await;

    let hello_res = auth
        .map(|a| {
            if let Some(sess) = conn.session.as_mut() {
                sess.update_auth(a)
            } else {
                conn.session = Some(session::Session::new(a, conn.version));
                Ok(())
            }
        })
        .and_then(|o| o);

    match hello_res {
        Ok(_) => {
            send_msg(conn, &proto::ServerMsg::HelloOk(proto::HelloOkMsg {})).await?;
            Ok(true)
        }
        Err(err) => match downcast_error(err) {
            Ok(error) => {
                send_msg(
                    conn,
                    &proto::ServerMsg::HelloError(proto::HelloErrorMsg { error }),
                )
                .await?;
                Ok(false)
            }
            Err(err) => Err(err),
        },
    }
}

async fn handle_request_msg(
    conn: &mut Conn,
    request_id: i32,
    request: proto::Request,
) -> Result<bool> {
    let Some(session) = conn.session.as_mut() else {