Repository: libsql/libsql
File: bindings/c/src/lib.rs (Rust)

Code before:
            set_err_msg(format!("Wrong URL: {e}"), out_err_msg);
            return 1;
        }
    };
    let primary_url = unsafe { std::ffi::CStr::from_ptr(config.primary_url) };
    let primary_url = match primary_url.to_str() {
        Ok(url) => url,
        Err(e) => {
            set_err_msg(format!("Wrong URL: {e}"), out_err_msg);
            return 2;
        }
    };
    let auth_token = unsafe { std::ffi::CStr::from_ptr(config.auth_token) };
    let auth_token = match auth_token.to_str() {
        Ok(token) => token,
        Err(e) => {
            set_err_msg(format!("Wrong Auth Token: {e}"), out_err_msg);
            return 3;
        }
    };
    let uri: http::Uri = match primary_url.try_into(){
        Ok(uri) => uri,
        Err(e) => {
            set_err_msg(format!("Wrong primary URL: {e}"), out_err_msg);
            return 100;
        }
    };
    if let Some(query) = uri.query() {
        if query.contains("offline") {
            let mut builder = Builder::new_synced_database(db_path, primary_url.to_owned(), auth_token.to_owned());
            if config.with_webpki != 0 {
                let https = hyper_rustls::HttpsConnectorBuilder::new()
                    .with_webpki_roots()
                    .https_or_http()
                    .enable_http1()
                    .build();
                builder = builder.connector(https);
            }
            match RT.block_on(builder.build()) {
                Ok(db) => {
                    let db = Box::leak(Box::new(libsql_database { db }));
                    *out_db = libsql_database_t::from(db);
                    return 0;
                }
                Err(e) => {
                    set_err_msg(
                        format!("Error opening offline db path {db_path}, primary url {primary_url}: {e}"),
                        out_err_msg,
                    );
                    return 101;
                }

Review comment (refactor):
I think it makes most sense to remove the dep of `http` here and move this logic into the core crate. Have it return a specific error and then match here and convert to the c binding error style.

Code after:
            set_err_msg(format!("Wrong URL: {e}"), out_err_msg);
            return 1;
        }
    };
    let primary_url = unsafe { std::ffi::CStr::from_ptr(config.primary_url) };
    let primary_url = match primary_url.to_str() {
        Ok(url) => url,
        Err(e) => {
            set_err_msg(format!("Wrong URL: {e}"), out_err_msg);
            return 2;
        }
    };
    let auth_token = unsafe { std::ffi::CStr::from_ptr(config.auth_token) };
    let auth_token = match auth_token.to_str() {
        Ok(token) => token,
        Err(e) => {
            set_err_msg(format!("Wrong Auth Token: {e}"), out_err_msg);
            return 3;
        }
    };
    let uri: http::Uri = match primary_url.try_into() {
        Ok(uri) => uri,
        Err(e) => {
            set_err_msg(format!("Wrong primary URL: {e}"), out_err_msg);
            return 100;
        }
    };
    if let Some(query) = uri.query() {
        if query.contains("offline") {
            let mut builder = Builder::new_synced_database(
                db_path,
                primary_url.to_owned(),
                auth_token.to_owned(),
            );
            if config.with_webpki != 0 {
                let https = hyper_rustls::HttpsConnectorBuilder::new()
                    .with_webpki_roots()
                    .https_or_http()
                    .enable_http1()
                    .build();
                builder = builder.connector(https);
            }
            match RT.block_on(builder.build()) {
                Ok(db) => {
                    let db = Box::leak(Box::new(libsql_database { db }));
                    *out_db = libsql_database_t::from(db);
                    return 0;
                }
                Err(e) => {
                    set_err_msg(
                        format!("Error opening offline db path {db_path}, primary url {primary_url}: {e}"),