adbc_driver_manager 0.24.0

ADBC driver manager
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

use crate::ManagedDatabase;
use crate::search::find_filesystem_profile;
use adbc_core::{
    Optionable,
    error::{Error, Result, Status},
    options::{OptionDatabase, OptionValue},
};
use adbc_ffi::FFI_AdbcDriverInitFunc;
use regex::{Captures, Regex};
use std::path::PathBuf;
use std::{collections::HashMap, fs};
use std::{env, fmt, sync::OnceLock};
use toml::de::{DeTable, DeValue};

/// A connection profile that provides configuration for creating ADBC database connections.
///
/// Profiles contain the driver name, optional initialization function, and database options
/// that can be used to create a configured database connection without needing to specify
/// all connection details programmatically.
pub trait ConnectionProfile {
    /// Returns the driver name and an optional static initialization function.
    ///
    /// # Returns
    ///
    /// A tuple containing:
    /// - A string slice with the driver name (e.g., "adbc_driver_sqlite")
    /// - An optional reference to a statically-linked driver initialization function
    ///
    /// # Errors
    ///
    /// Returns an error if the profile is malformed or cannot be read.
    fn get_driver_name(&self) -> Result<(&str, Option<&FFI_AdbcDriverInitFunc>)>;

    /// Returns an iterator of database options to apply when creating a connection.
    ///
    /// # Returns
    ///
    /// An iterator yielding `(OptionDatabase, OptionValue)` tuples that should be
    /// applied to the database before initialization.
    ///
    /// # Errors
    ///
    /// Returns an error if the options cannot be retrieved or parsed.
    fn get_options(
        &self,
    ) -> Result<impl IntoIterator<Item = (<ManagedDatabase as Optionable>::Option, OptionValue)>>;
}

/// Provides access to connection profiles from a specific storage backend.
///
/// Implementations of this trait define how profiles are located and loaded,
/// such as from the filesystem, a configuration service, or other sources.
pub trait ConnectionProfileProvider {
    /// The concrete profile type returned by this provider.
    type Profile: ConnectionProfile;

    /// Retrieves a connection profile by name.
    ///
    /// # Arguments
    ///
    /// * `name` - The profile name or path to locate
    ///
    /// # Returns
    ///
    /// The loaded connection profile.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The profile cannot be found
    /// - The profile file is malformed
    /// - The profile version is unsupported
    fn get_profile(&self, name: &str) -> Result<Self::Profile>;
}

/// Provides connection profiles from TOML files on the filesystem.
///
/// This provider searches for profile files with a `.toml` extension in standard
/// configuration directories and any additional paths provided. Profile files must
/// conform to the ADBC profile specification version 1.
///
/// # Search Order
///
/// Profiles are searched in the following order:
/// 1. Additional paths provided via `new_with_search_paths`
/// 2. `ADBC_PROFILE_PATH` environment variable paths
/// 3. User configuration directory (`~/.config/adbc/profiles` on Linux,
///    `~/Library/Application Support/ADBC/Profiles` on macOS,
///    `%LOCALAPPDATA%\ADBC\Profiles` on Windows)
///
/// # Example
///
/// ```no_run
/// use adbc_driver_manager::profile::{
///     ConnectionProfileProvider, FilesystemProfileProvider
/// };
///
/// let provider = FilesystemProfileProvider::default();
/// let profile = provider.get_profile("my_database")?;
/// # Ok::<(), adbc_core::error::Error>(())
/// ```
#[derive(Clone, Default)]
pub struct FilesystemProfileProvider {
    additional_paths: Option<Vec<PathBuf>>,
}

impl FilesystemProfileProvider {
    /// Search the given paths (if any) for profiles.
    pub fn new_with_search_paths(additional_paths: Option<Vec<PathBuf>>) -> Self {
        Self { additional_paths }
    }
}

impl ConnectionProfileProvider for FilesystemProfileProvider {
    type Profile = FilesystemProfile;

    fn get_profile(&self, name: &str) -> Result<Self::Profile> {
        let profile_path = find_filesystem_profile(name, &self.additional_paths)?;
        FilesystemProfile::from_path(profile_path)
    }
}

/// Recursively processes TOML table entries into database options.
///
/// This function flattens nested TOML tables into dot-separated option keys
/// and converts TOML values into appropriate `OptionValue` types.
///
/// # Arguments
///
/// * `opts` - Map to populate with parsed options
/// * `prefix` - Current key prefix for nested tables (e.g., "connection." for nested options)
/// * `table` - TOML table to process
///
/// # Supported Types
///
/// - String values → `OptionValue::String`
/// - Integer values → `OptionValue::Int`
/// - Float values → `OptionValue::Double`
/// - Boolean values → `OptionValue::String` (converted to "true" or "false")
/// - Nested tables → Recursively processed with dot-separated keys
///
/// # Errors
///
/// Returns an error if:
/// - An integer value cannot be parsed as `i64`
/// - A float value cannot be parsed as `f64`
/// - An unsupported TOML type is encountered (e.g., arrays, inline tables)
fn process_options(
    opts: &mut HashMap<OptionDatabase, OptionValue>,
    prefix: &str,
    table: &DeTable,
) -> Result<()> {
    for (key, value) in table.iter() {
        let full_key = format!("{}{}", prefix, key.get_ref());
        match value.get_ref() {
            DeValue::String(s) => {
                opts.insert(full_key.as_str().into(), OptionValue::String(s.to_string()));
            }
            DeValue::Integer(i) => {
                let val: i64 = i.as_str().parse().map_err(|e| {
                    Error::with_message_and_status(
                        format!("invalid integer value for key '{}': {e}", full_key),
                        Status::InvalidArguments,
                    )
                })?;
                opts.insert(full_key.as_str().into(), OptionValue::Int(val));
            }
            DeValue::Float(f) => {
                let val: f64 = f.as_str().parse().map_err(|e| {
                    Error::with_message_and_status(
                        format!("invalid float value for key '{}': {e}", full_key),
                        Status::InvalidArguments,
                    )
                })?;
                opts.insert(full_key.as_str().into(), OptionValue::Double(val));
            }
            DeValue::Boolean(b) => {
                opts.insert(full_key.as_str().into(), OptionValue::String(b.to_string()));
            }
            DeValue::Table(t) => {
                let nested_prefix = format!("{}.", full_key);
                process_options(opts, &nested_prefix, t)?;
            }
            _ => {
                return Err(Error::with_message_and_status(
                    format!("unsupported option type for key '{}'", full_key),
                    Status::InvalidArguments,
                ));
            }
        }
    }
    Ok(())
}

/// A connection profile loaded from a TOML file on the filesystem.
///
/// This profile contains:
/// - The path to the profile file
/// - The driver name specified in the profile
/// - A map of database options parsed from the profile
///
/// # Profile Format
///
/// Profile files must be valid TOML with the following structure:
///
/// ```toml
/// profile_version = 1
/// driver = "driver_name"
///
/// [Options]
/// option_key = "option_value"
/// nested.key = "nested_value"
/// ```
///
/// Currently, only profile_version 1 profiles are supported.
#[derive(Debug)]
pub struct FilesystemProfile {
    profile_path: PathBuf,
    driver: String,
    opts: HashMap<OptionDatabase, OptionValue>,
}

impl FilesystemProfile {
    /// Loads a profile from the specified filesystem path.
    ///
    /// # Arguments
    ///
    /// * `profile_path` - Path to the TOML profile file
    ///
    /// # Returns
    ///
    /// A loaded `FilesystemProfile` with parsed configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The file cannot be read
    /// - The TOML is malformed
    /// - The profile version is not "1"
    /// - The `options` table is missing or invalid
    /// - Any option values cannot be parsed
    fn from_path(profile_path: PathBuf) -> Result<Self> {
        let contents = fs::read_to_string(&profile_path).map_err(|e| {
            Error::with_message_and_status(
                format!("could not read profile '{}': {e}", profile_path.display()),
                Status::InvalidArguments,
            )
        })?;

        let profile = DeTable::parse(&contents)
            .map_err(|e| Error::with_message_and_status(e.to_string(), Status::InvalidArguments))?;

        let raw_profile_version = profile.get_ref().get("profile_version").ok_or_else(|| {
            Error::with_message_and_status(
                "missing 'profile_version' in profile".to_string(),
                Status::InvalidArguments,
            )
        })?;

        let profile_version = raw_profile_version
            .as_ref()
            .as_integer()
            .and_then(|i| i64::from_str_radix(i.as_str(), i.radix()).ok())
            .ok_or_else(|| {
                Error::with_message_and_status(
                    format!(
                        "invalid 'profile_version' in profile: {:?}",
                        raw_profile_version.as_ref()
                    ),
                    Status::InvalidArguments,
                )
            })?;

        if profile_version != 1 {
            return Err(Error::with_message_and_status(
                format!(
                    "unsupported profile version '{}', expected '1'",
                    profile_version
                ),
                Status::InvalidArguments,
            ));
        }

        let driver = profile
            .get_ref()
            .get("driver")
            .and_then(|v| v.get_ref().as_str())
            .ok_or_else(|| {
                Error::with_message_and_status(
                    "missing or invalid 'driver' field in profile".to_string(),
                    Status::InvalidArguments,
                )
            })?
            .to_string();

        let options_table = profile
            .get_ref()
            .get("Options")
            .and_then(|v| v.get_ref().as_table())
            .ok_or_else(|| {
                Error::with_message_and_status(
                    "missing or invalid 'Options' table in profile".to_string(),
                    Status::InvalidArguments,
                )
            })?;

        let mut opts = HashMap::new();
        process_options(&mut opts, "", options_table)?;

        Ok(FilesystemProfile {
            profile_path,
            driver,
            opts,
        })
    }
}

impl ConnectionProfile for FilesystemProfile {
    fn get_driver_name(&self) -> Result<(&str, Option<&FFI_AdbcDriverInitFunc>)> {
        Ok((&self.driver, None))
    }

    fn get_options(
        &self,
    ) -> Result<impl IntoIterator<Item = (<ManagedDatabase as Optionable>::Option, OptionValue)>>
    {
        Ok(self.opts.clone().into_iter())
    }
}

impl fmt::Display for FilesystemProfile {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "FilesystemProfile({})", self.profile_path.display())
    }
}

fn profile_value_regex() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| Regex::new(r"\{\{\s*([^{}]*?)\s*\}\}").unwrap())
}

pub fn process_profile_value(value: &str) -> Result<OptionValue> {
    let re = profile_value_regex();

    let replacer = |caps: &Captures| -> Result<String> {
        let content = caps.get(1).unwrap().as_str();
        if !content.starts_with("env_var(") || !content.ends_with(")") {
            return Err(Error::with_message_and_status(
                format!(
                    "invalid profile replacement expression '{{{{ {} }}}}'",
                    content
                ),
                Status::InvalidArguments,
            ));
        }

        let env_var_name = content[8..content.len() - 1].trim();
        if env_var_name.is_empty() {
            return Err(Error::with_message_and_status(
                format!(
                    "empty environment variable name in profile replacement expression '{{{{ {} }}}}'",
                    content
                ),
                Status::InvalidArguments,
            ));
        }

        match env::var(env_var_name) {
            Ok(val) => Ok(val),
            Err(env::VarError::NotPresent) => Ok("".to_string()),
            Err(e) => Err(Error::with_message_and_status(
                format!(
                    "error retrieving environment variable '{}' for profile replacement expression '{{{{ {} }}}}': {}",
                    env_var_name, content, e
                ),
                Status::InvalidArguments,
            )),
        }
    };

    let mut new = String::with_capacity(value.len());
    let mut last_match = 0;
    for caps in re.captures_iter(value) {
        let m = caps.get(0).unwrap();
        new.push_str(&value[last_match..m.start()]);
        new.push_str(&replacer(&caps)?);
        last_match = m.end();
    }
    new.push_str(&value[last_match..]);
    Ok(OptionValue::String(new))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use toml::de::DeTable;

    #[test]
    fn test_process_options_basic_types() {
        let test_cases = vec![
            (
                "string value",
                r#"key = "value""#,
                vec![("key", OptionValue::String("value".to_string()))],
            ),
            (
                "integer value",
                r#"port = 5432"#,
                vec![("port", OptionValue::Int(5432))],
            ),
            (
                "float value",
                r#"timeout = 30.5"#,
                vec![("timeout", OptionValue::Double(30.5))],
            ),
            (
                "boolean values",
                r#"enabled = true
disabled = false"#,
                vec![
                    ("enabled", OptionValue::String("true".to_string())),
                    ("disabled", OptionValue::String("false".to_string())),
                ],
            ),
            (
                "multiple types",
                r#"str = "text"
num = 42
flt = 1.5
flag = true"#,
                vec![
                    ("str", OptionValue::String("text".to_string())),
                    ("num", OptionValue::Int(42)),
                    ("flt", OptionValue::Double(1.5)),
                    ("flag", OptionValue::String("true".to_string())),
                ],
            ),
        ];

        for (name, toml_str, expected_opts) in test_cases {
            let table = DeTable::parse(toml_str).unwrap();
            let mut opts = HashMap::new();
            process_options(&mut opts, "", table.get_ref())
                .unwrap_or_else(|_| panic!("Failed to process options for test case: {}", name));

            assert_eq!(
                opts.len(),
                expected_opts.len(),
                "Test case '{}': expected {} options, got {}",
                name,
                expected_opts.len(),
                opts.len()
            );

            for (key_str, expected_value) in expected_opts {
                let key = OptionDatabase::from(key_str);
                let actual_value = opts
                    .get(&key)
                    .unwrap_or_else(|| panic!("Test case '{}': missing key '{}'", name, key_str));

                match (actual_value, &expected_value) {
                    (OptionValue::String(a), OptionValue::String(e)) => {
                        assert_eq!(
                            a, e,
                            "Test case '{}': string mismatch for key '{}'",
                            name, key_str
                        );
                    }
                    (OptionValue::Int(a), OptionValue::Int(e)) => {
                        assert_eq!(
                            a, e,
                            "Test case '{}': int mismatch for key '{}'",
                            name, key_str
                        );
                    }
                    (OptionValue::Double(a), OptionValue::Double(e)) => {
                        assert!(
                            (a - e).abs() < 1e-10,
                            "Test case '{}': float mismatch for key '{}'",
                            name,
                            key_str
                        );
                    }
                    _ => panic!("Test case '{}': type mismatch for key '{}'", name, key_str),
                }
            }
        }
    }

    #[test]
    fn test_process_options_nested_table() {
        let toml_str = r#"
key = "value"
[nested]
subkey = "subvalue"
number = 42
"#;
        let table = DeTable::parse(toml_str).unwrap();
        let mut opts = HashMap::new();
        process_options(&mut opts, "", table.get_ref()).unwrap();

        assert_eq!(opts.len(), 3);

        let key1 = OptionDatabase::from("key");
        match opts.get(&key1) {
            Some(OptionValue::String(s)) => assert_eq!(s, "value"),
            _ => panic!("Expected string value"),
        }

        let key2 = OptionDatabase::from("nested.subkey");
        match opts.get(&key2) {
            Some(OptionValue::String(s)) => assert_eq!(s, "subvalue"),
            _ => panic!("Expected string value for nested.subkey"),
        }

        let key3 = OptionDatabase::from("nested.number");
        match opts.get(&key3) {
            Some(OptionValue::Int(i)) => assert_eq!(*i, 42),
            _ => panic!("Expected int value for nested.number"),
        }
    }

    #[test]
    fn test_process_options_deeply_nested() {
        let toml_str = r#"
[level1]
[level1.level2]
[level1.level2.level3]
deep = "value"
"#;
        let table = DeTable::parse(toml_str).unwrap();
        let mut opts = HashMap::new();
        process_options(&mut opts, "", table.get_ref()).unwrap();

        assert_eq!(opts.len(), 1);

        let key = OptionDatabase::from("level1.level2.level3.deep");
        match opts.get(&key) {
            Some(OptionValue::String(s)) => assert_eq!(s, "value"),
            _ => panic!("Expected string value"),
        }
    }

    #[test]
    fn test_process_options_error_cases() {
        let test_cases = vec![
            (
                "invalid integer (too large)",
                r#"bad_int = 999999999999999999999999999999"#,
                "invalid integer value",
            ),
            (
                "unsupported type (array)",
                r#"array = [1, 2, 3]"#,
                "unsupported option type",
            ),
        ];

        for (name, toml_str, expected_error_msg) in test_cases {
            let table = DeTable::parse(toml_str).unwrap();
            let mut opts = HashMap::new();
            let result = process_options(&mut opts, "", table.get_ref());

            assert!(
                result.is_err(),
                "Test case '{}': expected error but got Ok",
                name
            );
            let err = result.unwrap_err();
            assert_eq!(
                err.status,
                Status::InvalidArguments,
                "Test case '{}': wrong status",
                name
            );
            assert!(
                err.message.contains(expected_error_msg),
                "Test case '{}': expected '{}' in error message, got '{}'",
                name,
                expected_error_msg,
                err.message
            );
        }
    }

    #[test]
    fn test_filesystem_profile_from_path_errors() {
        let test_cases = vec![
            (
                "missing file",
                "/nonexistent/path/to/profile.toml",
                None,
                "could not read profile",
            ),
            (
                "invalid version (too high)",
                "invalid_version_high.toml",
                Some(
                    r#"
profile_version = 99
driver = "test_driver"

[Options]
key = "value"
"#,
                ),
                "unsupported profile version '99', expected '1'",
            ),
            (
                "version 0",
                "version_zero.toml",
                Some(
                    r#"
profile_version = 0
driver = "test_driver"

[Options]
key = "value"
"#,
                ),
                "unsupported profile version '0', expected '1'",
            ),
            (
                "version 2",
                "version_two.toml",
                Some(
                    r#"
profile_version = 2
driver = "test_driver"

[Options]
key = "value"
"#,
                ),
                "unsupported profile version '2', expected '1'",
            ),
        ];

        for (name, filename, content_opt, expected_error_msg) in test_cases {
            let profile_path = if let Some(content) = content_opt {
                let tmp_dir = tempfile::Builder::new()
                    .prefix("adbc_profile_test")
                    .tempdir()
                    .unwrap();
                let path = tmp_dir.path().join(filename);
                std::fs::write(&path, content).unwrap();
                // Keep tmp_dir alive until after the test
                let result = FilesystemProfile::from_path(path.clone());

                assert!(
                    result.is_err(),
                    "Test case '{}': expected error but got Ok",
                    name
                );
                let err = result.unwrap_err();
                assert_eq!(
                    err.status,
                    Status::InvalidArguments,
                    "Test case '{}': wrong status",
                    name
                );
                assert!(
                    err.message.contains(expected_error_msg),
                    "Test case '{}': expected '{}' in error message, got '{}'",
                    name,
                    expected_error_msg,
                    err.message
                );

                tmp_dir.close().unwrap();
                continue;
            } else {
                PathBuf::from(filename)
            };

            let result = FilesystemProfile::from_path(profile_path);

            assert!(
                result.is_err(),
                "Test case '{}': expected error but got Ok",
                name
            );
            let err = result.unwrap_err();
            assert_eq!(
                err.status,
                Status::InvalidArguments,
                "Test case '{}': wrong status",
                name
            );
            assert!(
                err.message.contains(expected_error_msg),
                "Test case '{}': expected '{}' in error message, got '{}'",
                name,
                expected_error_msg,
                err.message
            );
        }
    }

    #[test]
    fn test_process_profile_value() {
        // (name, env_vars_to_set, input, expected_ok / expected_err_fragment)
        struct TestCase<'a>(
            &'a str,
            Vec<(&'a str, &'a str)>,
            &'a str,
            std::result::Result<&'a str, &'a str>,
        );

        let test_cases: Vec<TestCase> = vec![
            TestCase("empty string", vec![], "", Ok("")),
            TestCase(
                "plain string no templates",
                vec![],
                "just a plain string",
                Ok("just a plain string"),
            ),
            TestCase(
                "not actually a substitution",
                vec![],
                "{{ env_var(NONEXISTENT)",
                Ok("{{ env_var(NONEXISTENT)"),
            ),
            TestCase(
                "not actually a substitution (2)",
                vec![],
                "{{ env_var(NONEXISTENT) }",
                Ok("{{ env_var(NONEXISTENT) }"),
            ),
            TestCase(
                "not actually a substitution (3)",
                vec![],
                "{ env_var(NONEXISTENT) }",
                Ok("{ env_var(NONEXISTENT) }"),
            ),
            TestCase(
                "string with special chars but no templates",
                vec![],
                "host=localhost port=5432",
                Ok("host=localhost port=5432"),
            ),
            TestCase(
                "env var present",
                vec![("ADBC_TEST_PPV_HOST", "myhost.example.com")],
                "{{ env_var(ADBC_TEST_PPV_HOST) }}",
                Ok("myhost.example.com"),
            ),
            TestCase(
                "env var not set returns empty string",
                vec![],
                "{{ env_var(ADBC_TEST_PPV_NONEXISTENT_XYZ) }}",
                Ok(""),
            ),
            TestCase(
                "env var not set interpolates the empty string",
                vec![],
                "foo{{ env_var(ADBC_TEST_PPV_NONEXISTENT_XYZ) }}bar",
                Ok("foobar"),
            ),
            TestCase(
                "env var not set interpolates the empty string (2)",
                vec![],
                "foo{{ env_var(ADBC_TEST_PPV_NONEXISTENT_XYZ) }}",
                Ok("foo"),
            ),
            TestCase(
                "env var not set interpolates the empty string (3)",
                vec![],
                "{{ env_var(ADBC_TEST_PPV_NONEXISTENT_XYZ) }}bar",
                Ok("bar"),
            ),
            TestCase(
                "env var not set interpolates the empty string (4)",
                vec![],
                "foo{{ env_var(ADBC_TEST_PPV_NONEXISTENT_XYZ) }}bar{{ env_var(ADBC_TEST_PPV_NONEXISTENT_XYZ2) }}baz",
                Ok("foobarbaz"),
            ),
            TestCase(
                "env var not set interpolates the empty string (5)",
                vec![],
                "{{ env_var(ADBC_TEST_PPV_NONEXISTENT_XYZ) }}foobarbaz{{ env_var(ADBC_TEST_PPV_NONEXISTENT_XYZ2) }}",
                Ok("foobarbaz"),
            ),
            TestCase(
                "mixed literal text and env var",
                vec![("ADBC_TEST_PPV_PORT", "5432")],
                "host=localhost port={{ env_var(ADBC_TEST_PPV_PORT) }}",
                Ok("host=localhost port=5432"),
            ),
            TestCase(
                "multiple env var replacements",
                vec![
                    ("ADBC_TEST_PPV_USER", "alice"),
                    ("ADBC_TEST_PPV_PASS", "secret"),
                ],
                "{{ env_var(ADBC_TEST_PPV_USER) }}:{{ env_var(ADBC_TEST_PPV_PASS) }}",
                Ok("alice:secret"),
            ),
            TestCase(
                "extra whitespace inside braces",
                vec![("ADBC_TEST_PPV_DB", "mydb")],
                "{{  env_var(ADBC_TEST_PPV_DB)  }}",
                Ok("mydb"),
            ),
            TestCase(
                "no whitespace inside braces",
                vec![("ADBC_TEST_PPV_DB", "mydb")],
                "{{env_var(ADBC_TEST_PPV_DB)}}",
                Ok("mydb"),
            ),
            TestCase(
                "invalid expression not env_var",
                vec![],
                "{{ something_invalid }}",
                Err("invalid profile replacement expression"),
            ),
            TestCase(
                "empty env var name",
                vec![],
                "{{ env_var() }}",
                Err("empty environment variable name"),
            ),
            TestCase(
                "empty env var name with whitespace",
                vec![],
                "{{ env_var(   ) }}",
                Err("empty environment variable name"),
            ),
        ];

        for TestCase(name, env_vars, input, expected) in test_cases {
            for (k, v) in &env_vars {
                unsafe {
                    std::env::set_var(k, v);
                }
            }

            let result = process_profile_value(input);

            match expected {
                Ok(expected_str) => match result.unwrap_or_else(|e| {
                    panic!("Test case '{}': expected Ok but got Err: {:?}", name, e)
                }) {
                    OptionValue::String(s) => {
                        assert_eq!(s, expected_str, "Test case '{}': string mismatch", name)
                    }
                    other => panic!(
                        "Test case '{}': expected OptionValue::String, got {:?}",
                        name, other
                    ),
                },
                Err(err_fragment) => {
                    assert!(
                        result.is_err(),
                        "Test case '{}': expected Err but got Ok",
                        name
                    );
                    let err = result.unwrap_err();
                    assert_eq!(
                        err.status,
                        Status::InvalidArguments,
                        "Test case '{}': wrong status",
                        name
                    );
                    assert!(
                        err.message.contains(err_fragment),
                        "Test case '{}': expected {:?} in error message, got {:?}",
                        name,
                        err_fragment,
                        err.message
                    );
                }
            }

            for (k, _) in &env_vars {
                unsafe {
                    std::env::remove_var(k);
                }
            }
        }
    }

    #[test]
    fn test_filesystem_profile_provider() {
        let profile_content = r#"
profile_version = 1
driver = "test_driver"

[Options]
test_key = "test_value"
"#;

        let test_cases = vec![
            ("absolute path", "absolute_test.toml", None, true),
            (
                "search path with name only",
                "search_test",
                Some(vec![]),
                true,
            ),
            (
                "search path with .toml extension",
                "search_test.toml",
                Some(vec![]),
                true,
            ),
        ];

        for (name, profile_name, search_paths_opt, should_succeed) in test_cases {
            let tmp_dir = tempfile::Builder::new()
                .prefix("adbc_profile_test")
                .tempdir()
                .unwrap();

            let profile_path = tmp_dir.path().join(if profile_name.ends_with(".toml") {
                profile_name.to_string()
            } else {
                format!("{}.toml", profile_name)
            });
            std::fs::write(&profile_path, profile_content).unwrap();

            let search_paths = search_paths_opt.map(|mut paths| {
                paths.push(tmp_dir.path().to_path_buf());
                paths
            });
            let provider = FilesystemProfileProvider::new_with_search_paths(search_paths);

            let profile_arg = if name.contains("absolute") {
                profile_path.to_str().unwrap().to_string()
            } else {
                profile_name.to_string()
            };

            let result = provider.get_profile(&profile_arg);

            if should_succeed {
                let profile =
                    result.unwrap_or_else(|e| panic!("Test case '{}' failed: {:?}", name, e));
                let (driver, _) = profile.get_driver_name().unwrap();
                assert_eq!(
                    driver, "test_driver",
                    "Test case '{}': driver mismatch",
                    name
                );
            } else {
                assert!(result.is_err(), "Test case '{}': expected error", name);
            }

            tmp_dir.close().unwrap();
        }
    }
}