bse 0.1.4

Basis Set Exchange (BSE) library in Rust for quantum chemistry applications.
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
//! Command-line interface for the Basis Set Exchange in Rust.
//!
//! This CLI is functionally equivalent to the Python BSE CLI.

use bse::cli::common::resolve_cli_format;
use bse::cli::handlers::*;
use bse::is_dir_format;
use bse::{get_bse_source_default, parse_source_from_str, BseDataSource};
use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::Shell;
use std::path::PathBuf;

/// Basis Set Exchange CLI - retrieve, manipulate, and convert Gaussian-type
/// orbital basis sets for computational chemistry.
#[derive(Parser)]
#[command(name = "bse-rs")]
#[command(version, about, long_about = None)]
struct Cli {
    /// Override which data directory to use
    #[arg(short = 'd', long = "data-dir", global = true, value_name = "PATH")]
    data_dir: Option<PathBuf>,

    /// Data source: 'local', 'remote' (requires remote feature), or 'auto'.
    /// Default is from BSE_REMOTE env var, or 'local' if unset.
    #[arg(long = "source", global = true, value_name = "SOURCE")]
    source: Option<String>,

    /// Output to given file rather than stdout
    #[arg(short = 'o', long = "output", global = true, value_name = "PATH")]
    output: Option<PathBuf>,

    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Output a list of basis set formats that can be written
    ListWriterFormats(ListFormatsArgs),

    /// Output a list of basis set formats that can be read
    ListReaderFormats(ListFormatsArgs),

    /// Output a list of all available reference formats and descriptions
    ListRefFormats(ListFormatsArgs),

    /// Output a list of all available roles and descriptions
    ListRoles(ListFormatsArgs),

    /// Output the default data directory of this package
    GetDataDir,

    /// Output a list of all available basis sets and descriptions
    ListBasisSets(ListBasisSetsArgs),

    /// Output a list of all available basis set families
    ListFamilies,

    /// Lookup a companion/auxiliary basis by primary basis and role
    LookupByRole(LookupByRoleArgs),

    /// Output a formatted basis set
    GetBasis(GetBasisArgs),

    /// Output references for a basis set
    GetRefs(GetRefsArgs),

    /// Output general info and metadata for a basis set
    GetInfo(GetInfoArgs),

    /// Output the notes for a basis set
    GetNotes(GetNotesArgs),

    /// Output the family of a basis set
    GetFamily(GetFamilyArgs),

    /// Output a list of all available versions of a basis set
    GetVersions(GetVersionsArgs),

    /// Get the notes of a family of basis sets
    GetFamilyNotes(GetFamilyNotesArgs),

    /// Convert basis set files from one format to another
    ConvertBasis(ConvertBasisArgs),

    /// Form AutoAux auxiliary basis
    AutoauxBasis(AutoauxBasisArgs),

    /// Form AutoABS auxiliary basis
    AutoabsBasis(AutoauxBasisArgs),

    /// Generate or install shell completion scripts
    Completion(CompletionArgs),
}

// ============================================================================
// Argument structs for each subcommand
// ============================================================================

/// Simple listing arguments (just -n for no description)
#[derive(clap::Args)]
struct ListFormatsArgs {
    /// Print only the names without descriptions
    #[arg(short = 'n', long = "no-description")]
    no_description: bool,
}

/// List basis sets with optional filters
#[derive(clap::Args)]
struct ListBasisSetsArgs {
    /// Print only the basis set names without descriptions
    #[arg(short = 'n', long = "no-description")]
    no_description: bool,

    /// Limit the basis set list to only the specified family
    #[arg(short = 'f', long = "family")]
    family: Option<String>,

    /// Limit the basis set list to only the specified role
    #[arg(short = 'r', long = "role")]
    role: Option<String>,

    /// Limit the basis set list to only basis sets whose name contains the
    /// specified substring
    #[arg(short = 's', long = "substr")]
    substr: Option<String>,

    /// Limit the basis set list to only basis sets that contain all the given
    /// elements
    #[arg(short = 'e', long = "elements")]
    elements: Option<String>,
}

/// Lookup by role arguments
#[derive(clap::Args)]
struct LookupByRoleArgs {
    /// Name of the primary basis we want the auxiliary basis for
    basis: String,

    /// Role of the auxiliary basis to look for
    role: String,
}

/// Get basis set with all manipulation options
#[derive(clap::Args)]
struct GetBasisArgs {
    /// Name of the basis set to output
    basis: String,

    /// Which format to output the basis set as
    fmt: String,

    /// Which elements of the basis set to output. Default is all defined in the
    /// given basis
    #[arg(long = "elements")]
    elements: Option<String>,

    /// Which version of the basis set to output. Default is the latest version
    #[arg(long = "basis-version")]
    version: Option<String>,

    /// Do not output the header at the top
    #[arg(long = "noheader")]
    noheader: bool,

    /// Remove general contractions
    #[arg(long = "unc-gen")]
    unc_gen: bool,

    /// Remove combined sp, spd, ... contractions
    #[arg(long = "unc-spdf")]
    unc_spdf: bool,

    /// Remove segmented contractions
    #[arg(long = "unc-seg")]
    unc_seg: bool,

    /// Remove free primitives
    #[arg(long = "rm-free")]
    rm_free: bool,

    /// Optimize general contractions
    #[arg(long = "opt-gen")]
    opt_gen: bool,

    /// Make the basis set as generally-contracted as possible
    #[arg(long = "make-gen")]
    make_gen: bool,

    /// Augment with n steep functions
    #[arg(long = "aug-steep", default_value = "0")]
    aug_steep: i32,

    /// Augment with n diffuse functions
    #[arg(long = "aug-diffuse", default_value = "0")]
    aug_diffuse: i32,

    /// Instead of the orbital basis, get an automatically formed auxiliary
    /// basis (0=orbital, 1=autoaux, 2=autoabs)
    #[arg(long = "get-aux", default_value = "0")]
    get_aux: i32,
}

/// Get references arguments
#[derive(clap::Args)]
struct GetRefsArgs {
    /// Name of the basis set to output the references for
    basis: String,

    /// Which format to output the references as
    reffmt: String,

    /// Which elements to output the references for. Default is all defined in
    /// the given basis
    #[arg(long = "elements")]
    elements: Option<String>,

    /// Which version of the basis set to get the references for
    #[arg(long = "basis-version")]
    version: Option<String>,
}

/// Get info arguments
#[derive(clap::Args)]
struct GetInfoArgs {
    /// Name of the basis set to output the info for
    basis: String,
}

/// Get notes arguments
#[derive(clap::Args)]
struct GetNotesArgs {
    /// Name of the basis set to output the notes for
    basis: String,
}

/// Get family arguments
#[derive(clap::Args)]
struct GetFamilyArgs {
    /// Name of the basis set to output the family for
    basis: String,
}

/// Get versions arguments
#[derive(clap::Args)]
struct GetVersionsArgs {
    /// Name of the basis set to list the versions of
    basis: String,

    /// Print only the version numbers without descriptions
    #[arg(short = 'n', long = "no-description")]
    no_description: bool,
}

/// Get family notes arguments
#[derive(clap::Args)]
struct GetFamilyNotesArgs {
    /// The basis set family to get the notes of
    family: String,
}

/// Convert basis arguments
#[derive(clap::Args)]
struct ConvertBasisArgs {
    /// Basis set file to convert
    input_file: PathBuf,

    /// Converted basis set file
    output_file: PathBuf,

    /// Input format (default: autodetected from input filename)
    #[arg(long = "in-fmt")]
    in_fmt: Option<String>,

    /// Output format (default: autodetected from output filename)
    #[arg(long = "out-fmt")]
    out_fmt: Option<String>,

    /// Make the basis set as generally-contracted as possible
    #[arg(long = "make-gen")]
    make_gen: bool,
}

/// AutoAux/AutoABS basis arguments
#[derive(clap::Args)]
struct AutoauxBasisArgs {
    /// Orbital basis to load
    input_file: PathBuf,

    /// Output basis file
    output_file: PathBuf,

    /// Input format (default: autodetected from input filename)
    #[arg(long = "in-fmt")]
    in_fmt: Option<String>,

    /// Output format (default: autodetected from output filename)
    #[arg(long = "out-fmt")]
    out_fmt: Option<String>,
}

/// Shell completion arguments
#[derive(clap::Args)]
struct CompletionArgs {
    /// Shell to generate completion for (default: auto-detect from environment)
    #[arg(value_enum)]
    shell: Option<Shell>,

    /// Install completion script to the appropriate location for your shell
    #[arg(short = 'i', long = "install")]
    install: bool,
}

// ============================================================================
// Completion helpers
// ============================================================================

/// Detect the current shell from environment variables.
fn detect_shell() -> Option<Shell> {
    // Check $SHELL environment variable
    if let Ok(shell) = std::env::var("SHELL") {
        if shell.contains("zsh") {
            return Some(Shell::Zsh);
        }
        if shell.contains("bash") {
            return Some(Shell::Bash);
        }
        if shell.contains("fish") {
            return Some(Shell::Fish);
        }
        if shell.contains("elvish") {
            return Some(Shell::Elvish);
        }
    }

    // Check PowerShell on Windows
    if std::env::var("PSModulePath").is_ok() {
        return Some(Shell::PowerShell);
    }

    None
}

/// Get the default completion file path for a shell.
fn get_completion_path(shell: Shell) -> Option<PathBuf> {
    let home = std::env::var("HOME").ok()?;
    let home = PathBuf::from(home);

    match shell {
        Shell::Bash => {
            // Bash uses XDG_DATA_HOME or ~/.local/share/bash-completion/completions
            let xdg = std::env::var("XDG_DATA_HOME").ok();
            let base = xdg.map(PathBuf::from).unwrap_or_else(|| home.join(".local/share"));
            Some(base.join("bash-completion/completions/bse-rs"))
        },
        Shell::Zsh => {
            // Zsh uses ~/.zfunc/ or fpath
            Some(home.join(".zfunc/_bse-rs"))
        },
        Shell::Fish => {
            // Fish uses ~/.config/fish/completions/
            Some(home.join(".config/fish/completions/bse-rs.fish"))
        },
        Shell::Elvish => Some(home.join(".local/share/elvish/lib/bse-rs.elv")),
        Shell::PowerShell => {
            // PowerShell uses a scripts directory
            let profile = std::env::var("USERPROFILE").ok()?;
            Some(PathBuf::from(profile).join("Documents/PowerShell/bse-rs.ps1"))
        },
        _ => None,
    }
}

/// Get instructions for enabling completion after installation.
fn get_completion_instructions(shell: Shell) -> &'static str {
    match shell {
        Shell::Bash => {
            "To enable completion, restart your shell or run:\n  source ~/.local/share/bash-completion/completions/bse-rs"
        }
        Shell::Zsh => {
            "To enable completion, add this to your ~/.zshrc:\n  fpath+=~/.zfunc\n  autoload -U compinit && compinit\nThen restart your shell or run: source ~/.zshrc"
        }
        Shell::Fish => {
            "Completion will be automatically enabled on next shell start."
        }
        Shell::Elvish => {
            "To enable completion, add this to your ~/.elvish/rc.elv:\n  eval (bse-rs completion elvish)"
        }
        Shell::PowerShell => {
            "To enable completion, add this to your PowerShell profile:\n  . $HOME\\Documents\\PowerShell\\bse-rs.ps1"
        }
        _ => "Unknown shell. Please refer to your shell's documentation.",
    }
}

/// Handle the completion subcommand.
fn handle_completion(shell: Option<Shell>, install: bool) -> Result<String, bse::BseError> {
    use bse::{bse_raise, BseError};

    let shell = match shell.or_else(detect_shell) {
        Some(s) => s,
        None => {
            return bse_raise!(
                ValueError,
                "Could not auto-detect shell. Please specify a shell: bash, zsh, fish, powershell, elvish"
            );
        },
    };

    if install {
        // Install completion to the appropriate location
        let path = match get_completion_path(shell) {
            Some(p) => p,
            None => {
                return bse_raise!(ValueError, "Could not determine completion installation path for {:?}", shell);
            },
        };

        // Create parent directories if needed
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)
                .map_err(|e| BseError::IOError(format!("Failed to create directory {}: {}", parent.display(), e)))?;
        }

        // Generate and write completion
        let mut file = std::fs::File::create(&path)
            .map_err(|e| BseError::IOError(format!("Failed to create file {}: {}", path.display(), e)))?;

        clap_complete::generate(shell, &mut Cli::command(), "bse-rs", &mut file);

        Ok(format!("Completion installed to: {}\n\n{}", path.display(), get_completion_instructions(shell)))
    } else {
        // Just print completion to stdout
        clap_complete::generate(shell, &mut Cli::command(), "bse-rs", &mut std::io::stdout());
        Ok(String::new())
    }
}

// ============================================================================
// Main entry point
// ============================================================================

fn parse_source(source_str: Option<&str>) -> BseDataSource {
    match source_str {
        // If --source is explicitly provided, parse it
        Some(s) => parse_source_from_str(s),
        // If --source is not provided, use BSE_REMOTE env var default
        None => get_bse_source_default(),
    }
}

fn main() {
    let cli = Cli::parse();

    let data_dir_str = cli.data_dir.as_ref().map(|p| p.to_string_lossy().to_string());
    let source = parse_source(cli.source.as_deref());

    // Check if this is a directory format (needed for output handling)
    // Resolve CLI-only aliases like "rest" -> "dir-json" before checking
    let is_dir_output = match &cli.command {
        Commands::GetBasis(args) => is_dir_format(&resolve_cli_format(&args.fmt)),
        Commands::ConvertBasis(args) => {
            args.out_fmt.as_ref().map(|f| is_dir_format(&resolve_cli_format(f))).unwrap_or(false)
        },
        _ => false,
    };

    // Handle the command and get output
    let result = match cli.command {
        // Simple listings
        Commands::ListWriterFormats(args) => handle_list_writer_formats(args.no_description),
        Commands::ListReaderFormats(args) => handle_list_reader_formats(args.no_description),
        Commands::ListRefFormats(args) => handle_list_ref_formats(args.no_description),
        Commands::ListRoles(args) => handle_list_roles(args.no_description),
        Commands::GetDataDir => handle_get_data_dir(),
        Commands::ListFamilies => handle_list_families(data_dir_str),

        // Basis set listings with filters
        Commands::ListBasisSets(args) => handle_list_basis_sets(
            args.substr,
            args.family,
            args.role,
            args.elements,
            data_dir_str,
            args.no_description,
        ),

        // Lookup by role
        Commands::LookupByRole(args) => handle_lookup_by_role(args.basis, args.role, data_dir_str),

        // Get basis set
        Commands::GetBasis(args) => handle_get_basis(
            args.basis,
            args.fmt,
            args.elements,
            args.version,
            args.noheader,
            args.unc_gen,
            args.unc_spdf,
            args.unc_seg,
            args.rm_free,
            args.opt_gen,
            args.make_gen,
            args.aug_diffuse,
            args.aug_steep,
            args.get_aux,
            data_dir_str,
            cli.output.clone(),
            source,
        ),

        // Get references
        Commands::GetRefs(args) => handle_get_refs(args.basis, args.reffmt, args.elements, args.version, data_dir_str),

        // Get info
        Commands::GetInfo(args) => handle_get_info(args.basis, data_dir_str),

        // Get notes
        Commands::GetNotes(args) => handle_get_notes(args.basis, data_dir_str),

        // Get family
        Commands::GetFamily(args) => handle_get_family(args.basis, data_dir_str),

        // Get versions
        Commands::GetVersions(args) => handle_get_versions(args.basis, data_dir_str, args.no_description),

        // Get family notes
        Commands::GetFamilyNotes(args) => handle_get_family_notes(args.family, data_dir_str),

        // Convert basis
        Commands::ConvertBasis(args) => {
            handle_convert_basis(args.input_file, args.output_file, args.in_fmt, args.out_fmt, args.make_gen)
        },

        // AutoAux basis
        Commands::AutoauxBasis(args) => {
            handle_autoaux_basis(args.input_file, args.output_file, args.in_fmt, args.out_fmt)
        },

        // AutoABS basis
        Commands::AutoabsBasis(args) => {
            handle_autoabs_basis(args.input_file, args.output_file, args.in_fmt, args.out_fmt)
        },

        // Shell completion
        Commands::Completion(args) => handle_completion(args.shell, args.install),
    };

    // Handle result
    match result {
        Ok(output) => {
            // For directory formats, output is a success message (already written)
            // For regular formats, write to file or stdout
            if is_dir_output {
                // Directory format - output is already written, just print message
                if !output.is_empty() {
                    println!("{}", output);
                }
            } else if let Some(output_path) = cli.output {
                if let Err(e) = std::fs::write(&output_path, output + "\n") {
                    eprintln!("Error writing to {}: {}", output_path.display(), e);
                    std::process::exit(1);
                }
            } else if !output.is_empty() {
                println!("{}", output);
            }
        },
        Err(e) => {
            eprintln!("Error: {}", e);
            std::process::exit(1);
        },
    }
}