mbrobbel commented on code in PR #3099:
URL: https://github.com/apache/arrow-adbc/pull/3099#discussion_r2194367106


##########
rust/core/src/driver_manager.rs:
##########
@@ -316,6 +493,98 @@ impl Driver for ManagedDriver {
     }
 }
 
+#[cfg(windows)]
+fn load_driver_from_registry(
+    root: &windows_registry::Key,
+    driver_name: &OsStr,
+    entrypoint: Option<&[u8]>,
+) -> Result<DriverInfo> {
+    const ADBC_DRIVER_REGISTRY: &str = "SOFTWARE\\ADBC\\Drivers";
+    let drivers_key = root.open(ADBC_DRIVER_REGISTRY)?;
+
+    Ok(DriverInfo {
+        driver_name: drivers_key.get_string("name").unwrap_or_default(),
+        entrypoint: entrypoint
+            .or_else(|| drivers_key.get_string("entrypoint").map(|e| 
e.into_bytes())),
+        version: drivers_key.get_string("version").unwrap_or_default(),
+        source: drivers_key.get_string("source").unwrap_or_default(),
+        lib_path: 
PathBuf::from(drivers_key.get_string("driver").unwrap_or_default()),
+    })
+}
+
+fn get_optional_key(manifest: &Table, key: &str) -> String {
+    manifest
+        .get(key)
+        .and_then(Value::as_str)
+        .unwrap_or_default()
+        .to_string()
+}
+
+fn load_driver_manifest(info: &DriverInfo) -> Result<DriverInfo> {
+    let contents = 
fs::read_to_string(info.manifest_file.as_path()).map_err(|e| {
+        Error::with_message_and_status(
+            format!(
+                "Could not read manifest '{}': {e}",
+                info.manifest_file.display()
+            ),
+            Status::InvalidArguments,
+        )
+    })?;
+
+    let manifest = contents
+        .parse::<Table>()
+        .map_err(|e| Error::with_message_and_status(e.to_string(), 
Status::InvalidArguments))?;
+
+    let driver_name = get_optional_key(&manifest, "name");
+    let version = get_optional_key(&manifest, "version");
+    let source = get_optional_key(&manifest, "source");
+    let (os, arch, extra) = arch_triplet();
+
+    let mut lib_path = PathBuf::default();
+    if let Some(driver) = manifest.get("Driver").and_then(|v| v.get("shared")) 
{
+        if driver.is_str() {
+            lib_path = PathBuf::from(driver.as_str().unwrap_or_default());
+        } else if driver.is_table() {
+            lib_path = PathBuf::from(
+                driver
+                    .get(format!("{os}_{arch}{extra}"))
+                    .and_then(Value::as_str)
+                    .unwrap_or_default(),
+            );
+        }
+    }
+
+    if lib_path.as_os_str().is_empty() {
+        return Err(Error::with_message_and_status(
+            format!(
+                "Manifest '{}' missing appropriate path for current arch",
+                lib_path.display()
+            ),
+            Status::InvalidArguments,
+        ));
+    }
+
+    let entrypoint = manifest
+        .get("Driver")
+        .and_then(Value::as_table)
+        .and_then(|t| t.get("entrypoint"))
+        .and_then(Value::as_str)

Review Comment:
   Don't we want to error on an invalid type?



##########
rust/core/src/driver_manager.rs:
##########
@@ -316,6 +493,98 @@ impl Driver for ManagedDriver {
     }
 }
 
+#[cfg(windows)]
+fn load_driver_from_registry(
+    root: &windows_registry::Key,
+    driver_name: &OsStr,
+    entrypoint: Option<&[u8]>,
+) -> Result<DriverInfo> {
+    const ADBC_DRIVER_REGISTRY: &str = "SOFTWARE\\ADBC\\Drivers";
+    let drivers_key = root.open(ADBC_DRIVER_REGISTRY)?;
+
+    Ok(DriverInfo {
+        driver_name: drivers_key.get_string("name").unwrap_or_default(),
+        entrypoint: entrypoint
+            .or_else(|| drivers_key.get_string("entrypoint").map(|e| 
e.into_bytes())),
+        version: drivers_key.get_string("version").unwrap_or_default(),
+        source: drivers_key.get_string("source").unwrap_or_default(),
+        lib_path: 
PathBuf::from(drivers_key.get_string("driver").unwrap_or_default()),
+    })
+}
+
+fn get_optional_key(manifest: &Table, key: &str) -> String {
+    manifest
+        .get(key)
+        .and_then(Value::as_str)
+        .unwrap_or_default()
+        .to_string()
+}
+
+fn load_driver_manifest(info: &DriverInfo) -> Result<DriverInfo> {

Review Comment:
   Maybe this should be a method of `DriverInfo`?



##########
rust/core/src/driver_manager.rs:
##########
@@ -316,6 +493,98 @@ impl Driver for ManagedDriver {
     }
 }
 
+#[cfg(windows)]
+fn load_driver_from_registry(
+    root: &windows_registry::Key,
+    driver_name: &OsStr,
+    entrypoint: Option<&[u8]>,
+) -> Result<DriverInfo> {
+    const ADBC_DRIVER_REGISTRY: &str = "SOFTWARE\\ADBC\\Drivers";
+    let drivers_key = root.open(ADBC_DRIVER_REGISTRY)?;
+
+    Ok(DriverInfo {
+        driver_name: drivers_key.get_string("name").unwrap_or_default(),
+        entrypoint: entrypoint
+            .or_else(|| drivers_key.get_string("entrypoint").map(|e| 
e.into_bytes())),
+        version: drivers_key.get_string("version").unwrap_or_default(),
+        source: drivers_key.get_string("source").unwrap_or_default(),
+        lib_path: 
PathBuf::from(drivers_key.get_string("driver").unwrap_or_default()),
+    })
+}
+
+fn get_optional_key(manifest: &Table, key: &str) -> String {
+    manifest
+        .get(key)
+        .and_then(Value::as_str)
+        .unwrap_or_default()
+        .to_string()
+}
+
+fn load_driver_manifest(info: &DriverInfo) -> Result<DriverInfo> {
+    let contents = 
fs::read_to_string(info.manifest_file.as_path()).map_err(|e| {
+        Error::with_message_and_status(
+            format!(
+                "Could not read manifest '{}': {e}",
+                info.manifest_file.display()
+            ),
+            Status::InvalidArguments,
+        )
+    })?;
+
+    let manifest = contents
+        .parse::<Table>()
+        .map_err(|e| Error::with_message_and_status(e.to_string(), 
Status::InvalidArguments))?;
+
+    let driver_name = get_optional_key(&manifest, "name");
+    let version = get_optional_key(&manifest, "version");
+    let source = get_optional_key(&manifest, "source");
+    let (os, arch, extra) = arch_triplet();
+
+    let mut lib_path = PathBuf::default();
+    if let Some(driver) = manifest.get("Driver").and_then(|v| v.get("shared")) 
{
+        if driver.is_str() {
+            lib_path = PathBuf::from(driver.as_str().unwrap_or_default());
+        } else if driver.is_table() {
+            lib_path = PathBuf::from(
+                driver
+                    .get(format!("{os}_{arch}{extra}"))
+                    .and_then(Value::as_str)
+                    .unwrap_or_default(),
+            );
+        }
+    }
+
+    if lib_path.as_os_str().is_empty() {
+        return Err(Error::with_message_and_status(
+            format!(
+                "Manifest '{}' missing appropriate path for current arch",

Review Comment:
   Maybe we can include a hint (e.g. the path) to fix it?



##########
rust/core/src/driver_manager.rs:
##########
@@ -184,6 +205,41 @@ impl ManagedDriver {
         Ok(ManagedDriver { inner })
     }
 
+    /// Load a driver either by name, filename, path, or via locating a toml 
manifest file.
+    /// The LoadFlags control what directories are searched to locate a 
manifest.
+    pub fn load_from_name(
+        name: impl AsRef<OsStr>,
+        entrypoint: Option<&[u8]>,
+        version: AdbcVersion,
+        load_flags: LoadFlags,
+    ) -> Result<Self> {
+        let driver_path = Path::new(name.as_ref());
+        let allow_relative = load_flags & LOAD_FLAG_ALLOW_RELATIVE_PATHS != 0;
+        if let Some(ext) = driver_path.extension() {
+            if !allow_relative && driver_path.is_relative() {
+                return Err(Error::with_message_and_status(
+                    "Relative paths are not allowed",
+                    Status::InvalidArguments,
+                ));
+            }
+
+            if ext == "toml" {
+                Self::load_from_manifest(driver_path, entrypoint, version)
+            } else {
+                Self::load_dynamic_from_filename(driver_path, entrypoint, 
version)
+            }
+        } else if driver_path.is_absolute() {
+            let toml_path = driver_path.with_extension("toml");
+            if toml_path.is_file() {
+                return Self::load_from_manifest(&toml_path, entrypoint, 
version);
+            }
+
+            Self::load_dynamic_from_filename(driver_path, entrypoint, version)
+        } else {
+            Self::find_driver(driver_path, entrypoint, version, load_flags)
+        }

Review Comment:
   Did you consider adding different methods for these different load methods? 
The logic here is not clear to me from the doc string.



##########
rust/core/src/driver_manager.rs:
##########
@@ -161,8 +170,20 @@ struct ManagedDriverInner {
     _library: Option<libloading::Library>,
 }
 
+#[derive(Default)]
+#[allow(dead_code)]

Review Comment:
   ```suggestion
   ```



##########
rust/core/Cargo.toml:
##########
@@ -16,31 +16,41 @@
 # under the License.
 
 [package]
-name = "adbc_core"
+authors.workspace = true
+categories.workspace = true
 description = "Public abstract API, driver manager and driver exporter"
-version.workspace = true
+documentation.workspace = true
 edition.workspace = true
-rust-version.workspace = true
-authors.workspace = true
+homepage.workspace = true
+keywords.workspace = true
 license.workspace = true
+name = "adbc_core"
 readme = "../README.md"
-documentation.workspace = true
-homepage.workspace = true
 repository.workspace = true
-keywords.workspace = true
-categories.workspace = true
+rust-version.workspace = true
+version.workspace = true
 
 [features]
 default = []
-driver_manager = ["dep:libloading"]
+driver_manager = ["dep:toml", "dep:libloading", "dep:windows-sys", 
"dep:windows-registry"]
+driver_manager_test_lib = ["driver_manager"]
+driver_manager_test_manifest_user = ["driver_manager_test_lib"]
 
 [dependencies]
 arrow-array.workspace = true
 arrow-schema.workspace = true
-libloading = { version = "0.8", optional = true }
+libloading = {version = "0.8", optional = true}
+toml = {version = "0.8.23", default-features = false, features = [
+    "parse", "display",
+], optional = true}

Review Comment:
   0.9 was released yesterday: 
https://github.com/toml-rs/toml/blob/main/crates/toml/CHANGELOG.md#090---2025-07-08
   
   ```suggestion
   toml = { version = "0.9.0", default-features = false, features = [
       "parse", "display", "std",
   ], optional = true }
   ```



##########
rust/core/src/driver_manager.rs:
##########
@@ -184,6 +205,41 @@ impl ManagedDriver {
         Ok(ManagedDriver { inner })
     }
 
+    /// Load a driver either by name, filename, path, or via locating a toml 
manifest file.
+    /// The LoadFlags control what directories are searched to locate a 
manifest.
+    pub fn load_from_name(
+        name: impl AsRef<OsStr>,
+        entrypoint: Option<&[u8]>,
+        version: AdbcVersion,

Review Comment:
   Maybe you can add something to the function docs about these args?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to