This is an automated email from the ASF dual-hosted git repository.

lidavidm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-adbc.git


The following commit(s) were added to refs/heads/main by this push:
     new 790e84084 feat(rust): make boolean conventions clearer (#4427)
790e84084 is described below

commit 790e84084bbc5a067ddf297a52242fc062d29aed
Author: Austin Bonander <[email protected]>
AuthorDate: Mon Jun 22 18:29:58 2026 -0700

    feat(rust): make boolean conventions clearer (#4427)
    
    Adds `From<bool> for OptionValue` and `TryFrom<OptionValue> for bool`
    (and `TryFrom<&OptionValue>`).
    
    Documents boolean conventions on `OptionValue` for
    clarity/discoverability.
    
    closes #4413
---
 rust/core/src/constants.rs |  3 +++
 rust/core/src/options.rs   | 57 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)

diff --git a/rust/core/src/constants.rs b/rust/core/src/constants.rs
index d9dd1c845..7735df076 100644
--- a/rust/core/src/constants.rs
+++ b/rust/core/src/constants.rs
@@ -75,6 +75,9 @@ pub const ADBC_OPTION_URI: &str = "uri";
 pub const ADBC_OPTION_USERNAME: &str = "username";
 pub const ADBC_OPTION_PASSWORD: &str = "password";
 
+pub const ADBC_OPTION_VALUE_ENABLED: &str = "true";
+pub const ADBC_OPTION_VALUE_DISABLED: &str = "false";
+
 pub const ADBC_CONNECTION_OPTION_AUTOCOMMIT: &str = 
"adbc.connection.autocommit";
 pub const ADBC_CONNECTION_OPTION_READ_ONLY: &str = "adbc.connection.readonly";
 pub const ADBC_CONNECTION_OPTION_CURRENT_CATALOG: &str = 
"adbc.connection.catalog";
diff --git a/rust/core/src/options.rs b/rust/core/src/options.rs
index 98bca581f..99fdb5907 100644
--- a/rust/core/src/options.rs
+++ b/rust/core/src/options.rs
@@ -18,6 +18,7 @@
 //! Various option and configuration types.
 use std::{os::raw::c_int, str::FromStr};
 
+use crate::constants::{ADBC_OPTION_VALUE_DISABLED, ADBC_OPTION_VALUE_ENABLED};
 use crate::{
     constants,
     error::{Error, Status},
@@ -26,6 +27,13 @@ use crate::{
 /// Option value.
 ///
 /// Can be created with various implementations of [From].
+///
+/// # Note: Booleans
+/// ADBC passes booleans as the strings `"true"` 
([`ADBC_OPTION_VALUE_ENABLED`])
+/// or `"false"` ([`ADBC_OPTION_VALUE_DISABLED`]).
+///
+/// To reflect this, instead of a special boolean variant, this type 
implements `From<bool>`
+/// and provides `TryFrom<OptionValue> for bool` which expects 
[`Self::String`].
 #[derive(Debug, Clone)]
 #[non_exhaustive]
 pub enum OptionValue {
@@ -83,6 +91,55 @@ impl<const N: usize> From<&[u8; N]> for OptionValue {
     }
 }
 
+impl From<bool> for OptionValue {
+    /// Convert a boolean to the ADBC string equivalent.
+    ///
+    /// Returns `"true"` ([`ADBC_OPTION_VALUE_ENABLED`]) for `true`
+    /// or `"false"` ([`ADBC_OPTION_VALUE_DISABLED`]) for `false`.
+    fn from(value: bool) -> Self {
+        if value {
+            ADBC_OPTION_VALUE_ENABLED.into()
+        } else {
+            ADBC_OPTION_VALUE_DISABLED.into()
+        }
+    }
+}
+
+impl TryFrom<OptionValue> for bool {
+    type Error = Error;
+
+    /// Expects either `"true"` ([`ADBC_OPTION_VALUE_ENABLED`]) for `true`
+    /// or `"false"` ([`ADBC_OPTION_VALUE_DISABLED`]) for `false`.
+    ///
+    /// Returns an error with [`Status::InvalidArguments`] if any other string 
or value type.
+    fn try_from(value: OptionValue) -> Result<Self, Self::Error> {
+        // Forward to the borrowed implementation
+        <bool as TryFrom<&OptionValue>>::try_from(&value)
+    }
+}
+
+impl TryFrom<&OptionValue> for bool {
+    type Error = Error;
+
+    /// Expects either `"true"` ([`ADBC_OPTION_VALUE_ENABLED`]) for `true`
+    /// or `"false"` ([`ADBC_OPTION_VALUE_DISABLED`]) for `false`.
+    ///
+    /// Returns an error with [`Status::InvalidArguments`] if any other string 
or value type.
+    fn try_from(value: &OptionValue) -> Result<Self, Self::Error> {
+        match value {
+            OptionValue::String(value) if value == ADBC_OPTION_VALUE_ENABLED 
=> Ok(true),
+            OptionValue::String(value) if value == ADBC_OPTION_VALUE_DISABLED 
=> Ok(false),
+            _ => Err(Error::with_message_and_status(
+                format!(
+                    "expected {ADBC_OPTION_VALUE_ENABLED:?} or 
{ADBC_OPTION_VALUE_DISABLED:?}, \
+                     got {value:?}"
+                ),
+                Status::InvalidArguments,
+            )),
+        }
+    }
+}
+
 /// ADBC revision versions.
 ///
 /// The [`Default`] implementation returns the latest version.

Reply via email to