Repository: libsql/libsql
File: libsql/src/database/builder.rs (Rust)

Code before:

            let connector = if let Some(connector) = connector {
                connector
            } else {
                let https = super::connector()?;
                use tower::ServiceExt;

                let svc = https
                    .map_err(|e| e.into())
                    .map_response(|s| Box::new(s) as Box<dyn crate::util::Socket>);

                crate::util::ConnectorService::new(svc)
            };

            let client = hyper::client::Client::builder()
                .build::<_, hyper::Body>(connector.clone());

            let req = http::Request::get(format!("{url}/sync/0/0/0"))
                .header("Authorization", format!("Bearer {}", auth_token))
                .body(hyper::Body::empty())
                .unwrap();

            let res = client
                .request(req)
                .await
                .map_err(|err| crate::Error::Sync(err.into()))?;

            match res.status() {
                hyper::StatusCode::OK => return Builder::new_synced_database(path, url, auth_token)
                    .remote_writes(true)
                    .read_your_writes(read_your_writes)
                    .build()
                    .await,
                _ => {}
            }

            let path = path.to_str().ok_or(crate::Error::InvalidUTF8Path)?.to_owned();

            let db = if !skip_safety_assert {
                crate::local::Database::open_http_sync_internal(
                    connector,
                    path,
                    url,
                    auth_token,
                    version,
                    read_your_writes,
                    encryption_config.clone(),
                    sync_interval,
                    http_request_callback,
                    namespace,
                )

Review comment (refactor):
Can you please extract this into a function? Also, let's add a "sync protocol" config option with three options: `v1` (gRPC), `v2` (HTTP), and `auto` (use this probing).

Code after:
                    Remote {
                        url,
                        auth_token,
                        connector,
                        version,
                    },
                encryption_config,
                read_your_writes,
                sync_interval,
                http_request_callback,
                namespace,
                skip_safety_assert,
                #[cfg(feature = "sync")]
                sync_protocol,
            } = self.inner;

            let connector = if let Some(connector) = connector {
                connector
            } else {
                let https = super::connector()?;
                use tower::ServiceExt;

                let svc = https
                    .map_err(|e| e.into())
                    .map_response(|s| Box::new(s) as Box<dyn crate::util::Socket>);

                crate::util::ConnectorService::new(svc)
            };

            #[cfg(feature = "sync")]
            {
                use super::SyncProtocol;
                match sync_protocol {
                    p @ (SyncProtocol::Auto | SyncProtocol::V2) => {
                        let client = hyper::client::Client::builder()
                            .build::<_, hyper::Body>(connector.clone());

                        let req = http::Request::get(format!("{url}/sync/0/0/0"))
                            .header("Authorization", format!("Bearer {}", auth_token))
                            .body(hyper::Body::empty())
                            .unwrap();

                        let res = client
                            .request(req)
                            .await
                            .map_err(|err| crate::Error::Sync(err.into()))?;

                        if matches!(p, SyncProtocol::V2) {
                            if !res.status().is_success() {
                                let status = res.status();
                                let body_bytes = hyper::body::to_bytes(res.into_body())