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
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
#![deny(
    absolute_paths_not_starting_with_crate,
    ambiguous_associated_items,
    anonymous_parameters,
    arithmetic_overflow,
    array_into_iter,
    asm_sub_register,
    bindings_with_variant_name,
    cenum_impl_drop_cast,
    clashing_extern_declarations,
    coherence_leak_check,
    conflicting_repr_hints,
    confusable_idents,
    const_err,
    dead_code,
    deprecated,
    deprecated_in_future,
    ellipsis_inclusive_range_patterns,
    explicit_outlives_requirements,
    exported_private_dependencies,
    ill_formed_attribute_input,
    illegal_floating_point_literal_pattern,
    improper_ctypes,
    improper_ctypes_definitions,
    incomplete_features,
    incomplete_include,
    indirect_structural_match,
    inline_no_sanitize,
    invalid_codeblock_attribute,
    invalid_type_param_default,
    invalid_value,
    irrefutable_let_patterns,
    keyword_idents,
    late_bound_lifetime_arguments,
    macro_expanded_macro_exports_accessed_by_absolute_paths,
    meta_variable_misuse,
    missing_copy_implementations,
    missing_crate_level_docs,
    missing_docs,
    missing_fragment_specifier,
    mixed_script_confusables,
    mutable_borrow_reservation_conflict,
    mutable_transmutes,
    no_mangle_const_items,
    no_mangle_generic_items,
    non_ascii_idents,
    non_camel_case_types,
    non_shorthand_field_patterns,
    non_snake_case,
    non_upper_case_globals,
    order_dependent_trait_objects,
    overflowing_literals,
    overlapping_patterns,
    path_statements,
    patterns_in_fns_without_body,
    private_doc_tests,
    private_in_public,
    proc_macro_derive_resolution_fallback,
    pub_use_of_private_extern_crate,
    redundant_semicolons,
    safe_packed_borrows,
    soft_unstable,
    stable_features,
    trivial_bounds,
    trivial_casts,
    trivial_numeric_casts,
    type_alias_bounds,
    tyvar_behind_raw_pointer,
    unaligned_references,
    uncommon_codepoints,
    unconditional_panic,
    unconditional_recursion,
    unknown_crate_types,
    unnameable_test_items,
    unreachable_code,
    unreachable_patterns,
    unreachable_pub,
    unsafe_code,
    unstable_features,
    unstable_name_collisions,
    unused_allocation,
    unused_assignments,
    unused_attributes,
    unused_braces,
    unused_comparisons,
    unused_crate_dependencies,
    unused_doc_comments,
    unused_extern_crates,
    unused_features,
    unused_import_braces,
    unused_imports,
    unused_labels,
    unused_lifetimes,
    unused_macros,
    unused_must_use,
    unused_mut,
    unused_parens,
    unused_qualifications,
    unused_unsafe,
    unused_variables,
    where_clauses_object_safety,
    while_true
)]
#![warn(macro_use_extern_crate, unknown_lints)]
#![allow(
    bare_trait_objects,
    box_pointers,
    elided_lifetimes_in_paths,
    intra_doc_link_resolution_failure,
    missing_doc_code_examples,
    missing_debug_implementations,
    single_use_lifetimes,
    unused_results,
    variant_size_differences,
    warnings,
    renamed_and_removed_lints
)]

//! # envmnt
//!
//! Environment variables utility functions.
//!
//! This library has many helper functions to access/modify/check environment variables.
//!
//! # Examples
//!
//! ## Get/Set/Remove environment variables
//!
//! ```
//! use envmnt::{ExpandOptions, ExpansionType};
//!
//! fn main() {
//!     if !envmnt::exists("MY_ENV_VAR") {
//!         envmnt::set("MY_ENV_VAR", "SOME VALUE");
//!     }
//!
//!     let mut value = envmnt::get_or("MY_ENV_VAR", "DEFAULT_VALUE");
//!     println!("Env Value: {}", &value);
//!
//!     value = envmnt::get_or_panic("MY_ENV_VAR");
//!     println!("Env Value: {}", &value);
//!
//!     let pre_value = envmnt::get_set("MY_ENV_VAR", "SOME NEW VALUE");
//!
//!     let value = envmnt::get_or("MY_ENV_VAR", "DEFAULT_VALUE");
//!     println!("New Env Value: {}", &value);
//!     println!("Previous Env Value: {:?}", &pre_value);
//!
//!     let var_was_set = envmnt::set_optional("MY_ENV_VAR", &Some("OPTIONAL VALUE"));
//!     println!("Env Was Modified: {}", var_was_set);
//!
//!     let all_vars = envmnt::vars(); // returned as Vec<(String, String)>
//!
//!     for (key, value) in all_vars {
//!         println!("{}: {}", key, value);
//!     }
//!
//!     envmnt::set("MY_ENV_VAR2", "SOME VALUE2");
//!
//!     let value = envmnt::get_any(&vec!["MY_ENV_VAR1", "MY_ENV_VAR2"], "default");
//!     println!("MY_ENV_VAR1 exists: {}", envmnt::exists("MY_ENV_VAR1"));
//!     println!("MY_ENV_VAR2 exists: {}", envmnt::exists("MY_ENV_VAR2"));
//!     println!("Found value: {}", value);
//!
//!     let mut options = ExpandOptions::new();
//!     options.expansion_type = Some(ExpansionType::Unix);
//!     let mut value = envmnt::expand("Env: MY_ENV value is: ${MY_ENV}", Some(options));
//!     println!("Expanded: {}", &value);
//!     options.expansion_type = Some(ExpansionType::UnixBracketsWithDefaults);
//!     value = envmnt::expand(
//!         "Env: MY_ENV_NOT_FOUND value is: ${MY_ENV_NOT_FOUND:default value}",
//!         Some(options),
//!     );
//!     println!("Expanded: {}", &value);
//! }
//! ```
//!
//! ## Get/Set boolean environment variables and other comparisons
//!
//! ```
//! fn main() {
//!     envmnt::set_bool("FLAG_VAR", true);
//!     let mut flag_value = envmnt::is_or("FLAG_VAR", false);
//!     println!("Bool Flag: {}", &flag_value);
//!
//!     flag_value = envmnt::is("FLAG_VAR");
//!     assert!(flag_value);
//!
//!     envmnt::set_bool("FLAG_VAR", true);
//!     assert!(envmnt::is_equal("FLAG_VAR", "true"));
//!
//!     envmnt::set("MY_ENV_VAR", "SOME VALUE");
//!     let same = envmnt::is_equal("MY_ENV_VAR", "SOME VALUE");
//!     println!("Value Is Same: {}", &same);
//!     let mut contains = envmnt::contains("MY_ENV_VAR", "_ENV_");
//!     println!("Value Contained: {}", &contains);
//!     contains = envmnt::contains_ignore_case("MY_ENV_VAR", "_env_");
//!     println!("Value Contained (case insensitive): {}", &contains);
//! }
//! ```
//!
//! ## Get/Set numeric environment variables
//!
//! ```
//! fn main() {
//!     // all numeric data types: u8/i8/u16/i16/u32/i32/u64/i64/u128/i128/f32/f64/isize/usize
//!     // are supported by specific set/get functions.
//!     envmnt::set_u8("U8_TEST_ENV", 50);
//!     let mut value_u8 = envmnt::get_u8("U8_TEST_ENV", 5);
//!     println!("u8 value: {}", value_u8);
//!
//!     envmnt::set_isize("ISIZE_TEST_ENV", -50);
//!     let mut value_isize = envmnt::get_isize("ISIZE_TEST_ENV", 5);
//!     println!("isize value: {}", value_isize);
//!
//!     // increment/decrement values
//!     value_isize = envmnt::increment("U8_TEST_ENV");
//!     assert_eq!(value_isize, 51);
//!     value_u8 = envmnt::get_u8("U8_TEST_ENV", 5);
//!     assert_eq!(value_u8, 51);
//!     value_isize = envmnt::decrement("U8_TEST_ENV");
//!     assert_eq!(value_isize, 50);
//!     value_u8 = envmnt::get_u8("U8_TEST_ENV", 5);
//!     assert_eq!(value_u8, 50);
//! }
//! ```
//!
//! ## Get/Set list environment variables
//!
//! ```
//! fn main() {
//!     envmnt::set_list(
//!         "LIST_TEST_ENV",
//!         &vec!["1".to_string(), "2".to_string(), "3".to_string()],
//!     );
//!
//!     let mut values = envmnt::get_list("LIST_TEST_ENV").unwrap();
//!     println!("List Values: {:?}", values);
//!
//!     let mut same = envmnt::is_equal("LIST_TEST_ENV", "1;2;3");
//!     println!("Same: {}", same);
//!
//!     let mut options = envmnt::ListOptions::new();
//!     options.separator = Some(",".to_string());
//!     envmnt::set_list_with_options(
//!         "LIST_TEST_ENV",
//!         &vec!["1".to_string(), "2".to_string(), "3".to_string()],
//!         &options,
//!     );
//!
//!     values = envmnt::get_list_with_options("LIST_TEST_ENV", &options).unwrap();
//!     println!("List Values: {:?}", values);
//!
//!     same = envmnt::is_equal("LIST_TEST_ENV", "1,2,3");
//!     println!("Same: {}", same);
//! }
//! ```
//!
//! ## Bulk Operations
//!
//! ```
//! use indexmap::IndexMap;
//!
//! fn main() {
//!     let mut env: IndexMap<String, String> = IndexMap::new();
//!     env.insert("ENV_VAR1".to_string(), "MY VALUE".to_string());
//!     env.insert("ENV_VAR2".to_string(), "MY VALUE2".to_string());
//!
//!     envmnt::set_all(&env);
//!
//!     let value = envmnt::get_or_panic("ENV_VAR1");
//!     println!("Value Is: {}", &value);
//!
//!     let mut found = envmnt::is_any_exists(&vec!["ENV_VAR1", "ENV_VAR2"]);
//!
//!     println!("Any Found: {}", &found);
//!
//!     found = envmnt::is_all_exists(&vec!["ENV_VAR1", "ENV_VAR2"]);
//!
//!     println!("All Found: {}", &found);
//!
//!     envmnt::remove_all(&vec!["ENV_VAR1", "ENV_VAR2"]);
//!
//!     found = envmnt::is_any_exists(&vec!["ENV_VAR1", "ENV_VAR2"]);
//!
//!     println!("Any Found: {}", &found);
//!
//!     env = IndexMap::new();
//!     env.insert("ENV_VAR1".to_string(), "MY VALUE".to_string());
//!     env.insert("ENV_VAR2".to_string(), "MY VALUE2".to_string());
//!
//!     let eval_env = |value: String| {
//!         let mut buffer = String::from("VALUE-");
//!         buffer.push_str(&value);
//!         buffer
//!     };
//!
//!     envmnt::evaluate_and_set_all(&env, eval_env);
//!
//!     let value = envmnt::get_or_panic("ENV_VAR1");
//!     println!("Value Is: {}", &value);
//! }
//! ```
//!
//! ## File Operations
//!
//! ```
//! fn main() {
//!     let mut output = envmnt::load_file("./src/test/var.env");
//!     assert!(output.is_ok());
//!
//!     let eval_env = |value: String| {
//!         let mut buffer = String::from("PREFIX-");
//!         buffer.push_str(&value);
//!         buffer
//!     };
//!
//!     output = envmnt::evaluate_and_load_file("./src/test/var.env", eval_env);
//!     assert!(output.is_ok());
//! }
//! ```
//!
//! # Installation
//! In order to use this library, just add it as a dependency:
//!
//! ```ini
//! [dependencies]
//! envmnt = "*"
//! ```
//!
//! # Contributing
//! See [contributing guide](https://github.com/sagiegurari/envmnt/blob/master/.github/CONTRIBUTING.md)
//!
//! # License
//! Developed by Sagie Gur-Ari and licensed under the
//! [Apache 2](https://github.com/sagiegurari/envmnt/blob/master/LICENSE) open source license.
//!

#[cfg(test)]
#[path = "./lib_test.rs"]
mod lib_test;

#[cfg(doctest)]
doc_comment::doctest!("../README.md");

mod bulk;
mod environment;
mod errors;
mod expansion;
mod file;
mod numeric;
pub mod types;
mod util;

use crate::errors::EnvmntError;
use indexmap::IndexMap;
use std::ffi::OsStr;

/// Get/Set list options
pub type ListOptions = types::ListOptions;

/// Expansion Type - unix/windows style
pub type ExpansionType = types::ExpansionType;

/// Expand options
pub type ExpandOptions = types::ExpandOptions;

/// Returns true environment variable is defined.
///
/// # Arguments
///
/// * `key` - The environment variable name
///
/// # Example
///
/// ```
/// fn main() {
///     if !envmnt::exists("MY_ENV_VAR") {
///         envmnt::set("MY_ENV_VAR", "SOME VALUE");
///         assert!(envmnt::is_equal("MY_ENV_VAR", "SOME VALUE"));
///     }
/// }
/// ```
pub fn exists<K: AsRef<OsStr>>(key: K) -> bool {
    environment::exists(key)
}

/// Removes the provided environment variable.
///
/// # Arguments
///
/// * `key` - The environment variable to remove
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set("MY_ENV_VAR", "SOME VALUE");
///     assert!(envmnt::is_equal("MY_ENV_VAR", "SOME VALUE"));
///
///     envmnt::remove("MY_ENV_VAR");
///     assert!(!envmnt::exists("MY_ENV_VAR"));
/// }
/// ```
pub fn remove<K: AsRef<OsStr>>(key: K) {
    environment::remove(key)
}

/// Removes the provided environment variable and returns its previous value (if any).
///
/// # Arguments
///
/// * `key` - The environment variable to remove
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set("MY_ENV_VAR", "SOME VALUE");
///     assert!(envmnt::is_equal("MY_ENV_VAR", "SOME VALUE"));
///
///     let value = envmnt::get_remove("MY_ENV_VAR");
///     assert!(!envmnt::exists("MY_ENV_VAR"));
///     assert_eq!(value.unwrap(), "SOME VALUE");
/// }
/// ```
pub fn get_remove<K: AsRef<OsStr>>(key: K) -> Option<String> {
    environment::get_remove(key)
}

/// Removes all provided environment variables.
///
/// # Arguments
///
/// * `keys` - A list of environment variables to remove
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set("MY_ENV_VAR1", "SOME VALUE 1");
///     envmnt::set("MY_ENV_VAR2", "SOME VALUE 2");
///     assert!(envmnt::is_equal("MY_ENV_VAR1", "SOME VALUE 1"));
///     assert!(envmnt::is_equal("MY_ENV_VAR2", "SOME VALUE 2"));
///
///     envmnt::remove_all(&vec!["MY_ENV_VAR1", "MY_ENV_VAR2"]);
///     assert!(!envmnt::exists("MY_ENV_VAR1"));
///     assert!(!envmnt::exists("MY_ENV_VAR2"));
/// }
/// ```
pub fn remove_all<K: AsRef<OsStr>>(keys: &Vec<K>) {
    bulk::remove_all(keys)
}

/// Returns the environment variable value or if is not defined, the default value will be returned.
///
/// # Arguments
///
/// * `key` - The environment variable name
/// * `default_value` - In case the environment variable is not defined, this value will be returned.
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set("MY_ENV_VAR", "SOME VALUE");
///     assert!(envmnt::is_equal("MY_ENV_VAR", "SOME VALUE"));
///
///     let mut value = envmnt::get_or("MY_ENV_VAR", "DEFAULT_VALUE");
///     assert_eq!(value, "SOME VALUE");
///
///     value = envmnt::get_or("ANOTHER_ENV_VAR", "DEFAULT_VALUE");
///     assert_eq!(value, "DEFAULT_VALUE");
/// }
/// ```
pub fn get_or<K: AsRef<OsStr>>(key: K, default_value: &str) -> String {
    environment::get_or(key, default_value)
}

/// Returns the environment variable value.
/// If the variable is not defined, this function will panic.
///
/// # Arguments
///
/// * `key` - The environment variable name
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set("MY_ENV_VAR", "SOME VALUE");
///     assert!(envmnt::is_equal("MY_ENV_VAR", "SOME VALUE"));
///
///     let value = envmnt::get_or_panic("MY_ENV_VAR");
///     assert_eq!(value, "SOME VALUE");
/// }
/// ```
pub fn get_or_panic<K: AsRef<OsStr>>(key: K) -> String {
    environment::get_or_panic(key)
}

/// Returns the first environment variable found.
///
/// # Arguments
///
/// * `keys` - The environment variables to search
/// * `default_value` - In case the environment variables are not defined, this value will be returned.
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set("MY_ENV_VAR2", "SOME VALUE2");
///
///     let value = envmnt::get_any(&vec!["MY_ENV_VAR1", "MY_ENV_VAR2"], "default");
///     assert!(!envmnt::exists("MY_ENV_VAR1"));
///     assert!(envmnt::exists("MY_ENV_VAR2"));
///     assert_eq!(value, "SOME VALUE2");
/// }
/// ```
pub fn get_any<K: AsRef<OsStr>>(keys: &Vec<K>, default_value: &str) -> String {
    environment::get_any(keys, default_value)
}

/// Returns false if environment variable value if falsy.<br>
/// Any other value is returned as true.<br>
/// The value is falsy if it is one of the following:
/// * Empty string
/// * "false" (case insensitive)
/// * "no" (case insensitive)
/// * "0"
///
/// # Arguments
///
/// * `key` - The environment variable name
/// * `default_value` - In case the environment variable is not defined, this value will be returned.
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set_bool("FLAG_VAR", true);
///     assert!(envmnt::is_equal("FLAG_VAR", "true"));
///
///     let flag_value = envmnt::is_or("FLAG_VAR", false);
///     assert!(flag_value);
/// }
/// ```
pub fn is_or<K: AsRef<OsStr>>(key: K, default_value: bool) -> bool {
    environment::is_or(key, default_value)
}

/// Returns false if environment variable value if falsy.
/// The value is falsy if it is one of the following:
/// * Empty string
/// * "false" (case insensitive)
/// * "no" (case insensitive)
/// * "0"
/// <br>
/// Any other value is returned as true.
/// This is same as calling is_or("varname", false)
///
/// # Arguments
///
/// * `key` - The environment variable name
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set_bool("FLAG_VAR", true);
///     assert!(envmnt::is_equal("FLAG_VAR", "true"));
///
///     let flag_value = envmnt::is("FLAG_VAR");
///     assert!(flag_value);
/// }
/// ```
pub fn is<K: AsRef<OsStr>>(key: K) -> bool {
    environment::is(key)
}

/// Sets the environment variable value.
///
/// # Arguments
///
/// * `key` - The environment variable name
/// * `value` - The environment variable value
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set("MY_ENV_VAR", "SOME VALUE");
///     assert!(envmnt::is_equal("MY_ENV_VAR", "SOME VALUE"));
///
///     let value = envmnt::get_or("MY_ENV_VAR", "DEFAULT_VALUE");
///     assert_eq!(value, "SOME VALUE");
/// }
/// ```
pub fn set<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) {
    environment::set(key, value)
}

/// Sets the environment variable with a true/false value as string.
///
/// # Arguments
///
/// * `key` - The environment variable name
/// * `value` - true/false boolean value which will be converted to string
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set_bool("FLAG_VAR", true);
///     assert!(envmnt::is_equal("FLAG_VAR", "true"));
///
///     let flag_value = envmnt::is_or("FLAG_VAR", false);
///     assert!(flag_value);
/// }
/// ```
pub fn set_bool<K: AsRef<OsStr>>(key: K, value: bool) {
    environment::set_bool(key, value)
}

/// Sets the environment variable if the provided option contains a value.
///
/// # Arguments
///
/// * `key` - The environment variable name
/// * `value` - The optional value to set
///
/// # Example
///
/// ```
/// fn main() {
///     let output = envmnt::set_optional("MY_ENV_VAR", &Some("OPTIONAL VALUE"));
///
///     assert!(output);
///     assert!(envmnt::is_equal(
///         "MY_ENV_VAR",
///         "OPTIONAL VALUE"
///     ));
/// }
/// ```
pub fn set_optional<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: &Option<V>) -> bool {
    environment::set_optional(key, value)
}

/// Sets the environment variable if the provided option contains a value.
/// If no value was provided, the environment variable will be removed.
///
/// # Arguments
///
/// * `key` - The environment variable name
/// * `value` - The optional value to set, none to remove
///
/// # Example
///
/// ```
/// fn main() {
///     let mut output = envmnt::set_or_remove("MY_ENV_VAR", &Some("OPTIONAL VALUE"));
///
///     assert!(output);
///     assert!(envmnt::is_equal(
///         "MY_ENV_VAR",
///         "OPTIONAL VALUE"
///     ));
///
///     output = envmnt::set_or_remove::<&str, &str>("MY_ENV_VAR", &None);
///
///     assert!(!output);
///     assert!(!envmnt::exists("MY_ENV_VAR"));
/// }
/// ```
pub fn set_or_remove<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: &Option<V>) -> bool {
    environment::set_or_remove(key, value)
}

/// Sets the environment variable value and returns the previous value.
///
/// # Arguments
///
/// * `key` - The environment variable name
/// * `value` - The environment variable value
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set("MY_ENV_VAR", "SOME VALUE");
///     assert!(envmnt::is_equal("MY_ENV_VAR", "SOME VALUE"));
///
///     let pre_value = envmnt::get_set("MY_ENV_VAR", "NEW VALUE");
///
///     let value = envmnt::get_or("MY_ENV_VAR", "DEFAULT_VALUE");
///     assert_eq!(value, "NEW VALUE");
///     assert_eq!(pre_value.unwrap(), "SOME VALUE");
/// }
/// ```
pub fn get_set<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) -> Option<String> {
    environment::get_set(key, value)
}

/// Returns all environment variables as a vector.
///
/// # Example
///
/// ```
/// fn main() {
///     let all_vars = envmnt::vars();
///
///     for (key, value) in all_vars {
///         println!("{}: {}", key, value);
///     }
/// }
/// ```
pub fn vars() -> Vec<(String, String)> {
    environment::vars()
}

/// Returns true if the provided environment variable is defined and equals the provided value.
///
/// # Arguments
///
/// * `key` - The environment variable name
/// * `value` - The value to check
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set("MY_ENV_VAR", "SOME VALUE");
///
///     let same = envmnt::is_equal("MY_ENV_VAR", "SOME VALUE");
///     assert!(same);
/// }
/// ```
pub fn is_equal<K: AsRef<OsStr>>(key: K, value: &str) -> bool {
    environment::is_equal(key, value)
}

/// Returns true if the provided environment variable is defined and contains the provided value.
///
/// # Arguments
///
/// * `key` - The environment variable name
/// * `value` - The value to check
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set("MY_ENV_VAR", "SOME TEST VALUE");
///
///     let contains = envmnt::contains("MY_ENV_VAR", "TEST");
///     assert!(contains);
/// }
/// ```
pub fn contains<K: AsRef<OsStr>>(key: K, value: &str) -> bool {
    environment::contains(key, value)
}

/// Returns true if the provided environment variable is defined and contains the provided value regardless of the case.
///
/// # Arguments
///
/// * `key` - The environment variable name
/// * `value` - The value to check
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set("MY_ENV_VAR", "SOME TEST VALUE");
///
///     let contains = envmnt::contains_ignore_case("MY_ENV_VAR", "test");
///     assert!(contains);
/// }
/// ```
pub fn contains_ignore_case<K: AsRef<OsStr>>(key: K, value: &str) -> bool {
    environment::contains_ignore_case(key, value)
}

/// Sets the provided string vector as an environment variable.
///
/// # Arguments
///
/// * `key` - The environment variable name
/// * `values` - String vector of values
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set_list(
///         "LIST_TEST_ENV",
///         &vec!["1".to_string(), "2".to_string(), "3".to_string()],
///     );
///
///     let values = envmnt::get_list("LIST_TEST_ENV").unwrap();
///     assert_eq!(
///         values,
///         vec!["1".to_string(), "2".to_string(), "3".to_string()]
///     );
/// }
/// ```
pub fn set_list<K: AsRef<OsStr>>(key: K, values: &Vec<String>) {
    environment::set_list(key, values)
}

/// Returns the requested environment variable as a string vector.
///
/// # Arguments
///
/// * `key` - The environment variable name
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set_list(
///         "LIST_TEST_ENV",
///         &vec!["1".to_string(), "2".to_string(), "3".to_string()],
///     );
///
///     let values = envmnt::get_list("LIST_TEST_ENV").unwrap();
///     assert_eq!(
///         values,
///         vec!["1".to_string(), "2".to_string(), "3".to_string()]
///     );
/// }
/// ```
pub fn get_list<K: AsRef<OsStr>>(key: K) -> Option<Vec<String>> {
    environment::get_list(key)
}

/// Sets the provided string vector as an environment variable.
///
/// # Arguments
///
/// * `key` - The environment variable name
/// * `values` - String vector of values
/// * `options` - See ListOptions
///
/// # Example
///
/// ```
/// fn main() {
///     let mut options = envmnt::ListOptions::new();
///     options.separator = Some(",".to_string());
///     envmnt::set_list_with_options(
///         "LIST_TEST_ENV",
///         &vec!["1".to_string(), "2".to_string(), "3".to_string()],
///         &options,
///     );
///
///     let values = envmnt::get_list_with_options("LIST_TEST_ENV", &options).unwrap();
///     println!("List Values: {:?}", values);
///
///     let same = envmnt::is_equal("LIST_TEST_ENV", "1,2,3");
///     println!("Same: {}", same);
/// }
/// ```
pub fn set_list_with_options<K: AsRef<OsStr>>(key: K, values: &Vec<String>, options: &ListOptions) {
    environment::set_list_with_options(key, values, options)
}

/// Returns the requested environment variable as a string vector.
///
/// # Arguments
///
/// * `key` - The environment variable name
/// * `options` - See ListOptions
///
/// # Example
///
/// ```
/// fn main() {
///     let mut options = envmnt::ListOptions::new();
///     options.separator = Some(",".to_string());
///     envmnt::set_list_with_options(
///         "LIST_TEST_ENV",
///         &vec!["1".to_string(), "2".to_string(), "3".to_string()],
///         &options,
///     );
///
///     let values = envmnt::get_list_with_options("LIST_TEST_ENV", &options).unwrap();
///     println!("List Values: {:?}", values);
///
///     let same = envmnt::is_equal("LIST_TEST_ENV", "1,2,3");
///     println!("Same: {}", same);
/// }
/// ```
pub fn get_list_with_options<K: AsRef<OsStr>>(
    key: K,
    options: &ListOptions,
) -> Option<Vec<String>> {
    environment::get_list_with_options(key, options)
}

/// Sets all the provided env key/value pairs.
///
/// # Arguments
///
/// * `env` - The environment variables to set
///
/// # Example
///
/// ```
/// use indexmap::IndexMap;
///
/// fn main() {
///     let mut env: IndexMap<String, String> = IndexMap::new();
///     env.insert("MY_ENV_VAR".to_string(), "MY VALUE".to_string());
///     env.insert("MY_ENV_VAR2".to_string(), "MY VALUE2".to_string());
///
///     envmnt::set_all(&env);
///
///     let mut value = envmnt::get_or_panic("MY_ENV_VAR");
///     assert_eq!(value, "MY VALUE");
///     value = envmnt::get_or_panic("MY_ENV_VAR2");
///     assert_eq!(value, "MY VALUE2");
/// }
/// ```
pub fn set_all(env: &IndexMap<String, String>) {
    bulk::set_all(&env)
}

/// Sets all the provided env key/value pairs.
///
/// # Arguments
///
/// * `env` - The environment variables to set
/// * `evaluate` - Evalute function which will modify the read value before it is loaded into the environment
///
/// # Example
///
/// ```
/// use indexmap::IndexMap;
///
/// fn main() {
///     let mut env: IndexMap<String, String> = IndexMap::new();
///     env.insert("MY_ENV_VAR".to_string(), "MY VALUE".to_string());
///     env.insert("MY_ENV_VAR2".to_string(), "MY VALUE2".to_string());
///
///     let eval_env = |value: String| {
///         let mut buffer = String::from("VALUE-");
///         buffer.push_str(&value);
///         buffer
///     };
///
///     envmnt::evaluate_and_set_all(&env, eval_env);
///
///     let mut value = envmnt::get_or_panic("MY_ENV_VAR");
///     assert_eq!(value, "VALUE-MY VALUE");
///     value = envmnt::get_or_panic("MY_ENV_VAR2");
///     assert_eq!(value, "VALUE-MY VALUE2");
/// }
/// ```
pub fn evaluate_and_set_all<F>(env: &IndexMap<String, String>, evaluate: F)
where
    F: Fn(String) -> String,
{
    bulk::evaluate_and_set_all(&env, evaluate)
}

/// Returns true if any of environment variables is defined.
///
/// # Arguments
///
/// * `keys` - The environment variable names
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set("ENV_VAR1", "SOME VALUE");
///     envmnt::remove("ENV_VAR2");
///
///     let found = envmnt::is_any_exists(&vec![
///         "ENV_VAR1",
///         "ENV_VAR2",
///     ]);
///
///     assert!(found);
/// }
/// ```
pub fn is_any_exists<K: AsRef<OsStr>>(keys: &Vec<K>) -> bool {
    bulk::is_any_exists(keys)
}

/// Returns true if all of environment variables are defined.
///
/// # Arguments
///
/// * `keys` - The environment variable names
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set("ENV_VAR1", "SOME VALUE");
///     envmnt::set("ENV_VAR2", "SOME VALUE");
///
///     let mut found = envmnt::is_all_exists(&vec![
///         "ENV_VAR1",
///         "ENV_VAR2",
///     ]);
///
///     assert!(found);
///
///     envmnt::remove("ENV_VAR2");
///
///     found = envmnt::is_all_exists(&vec![
///         "ENV_VAR1",
///         "ENV_VAR2",
///     ]);
///
///     assert!(!found);
/// }
/// ```
pub fn is_all_exists<K: AsRef<OsStr>>(keys: &Vec<K>) -> bool {
    bulk::is_all_exists(keys)
}

/// Parses the provided env file and loads all environment variables.
///
/// # Arguments
///
/// * `file` - The file path to load and parse
///
/// # Example
///
/// ```
/// fn main() {
///     let output = envmnt::load_file("./src/test/var.env");
///
///     assert!(output.is_ok());
/// }
/// ```
pub fn load_file(file: &str) -> Result<(), EnvmntError> {
    file::load_file(file)
}

/// Parses the provided env file and loads all environment variables.
///
/// # Arguments
///
/// * `file` - The file path to load and parse
/// * `evaluate` - Evalute function which will modify the read value before it is loaded into the environment
///
/// # Example
///
/// ```
/// fn main() {
///     let eval_env = |value: String| {
///         let mut buffer = String::from("PREFIX-");
///         buffer.push_str(&value);
///         buffer
///     };
///
///     let output = envmnt::evaluate_and_load_file("./src/test/var.env", eval_env);
///
///     assert!(output.is_ok());
/// }
/// ```
pub fn evaluate_and_load_file<F>(file: &str, evaluate: F) -> Result<(), EnvmntError>
where
    F: Fn(String) -> String,
{
    file::evaluate_and_load_file(file, evaluate)
}

/// Parses the provided env file and returns its content as a map of key/value.
///
/// # Arguments
///
/// * `file` - The file path to load and parse
///
/// # Example
///
/// ```
/// fn main() {
///     let env = envmnt::parse_file("./src/test/var.env").unwrap();
///
///     println!("Parsed Env: {:?}", &env);
/// }
/// ```
pub fn parse_file(file: &str) -> Result<IndexMap<String, String>, EnvmntError> {
    file::parse_file(file)
}

/// Expands the provided string value by replacing the environment variables defined in it.
/// The syntax of the environment variables is based on the type requested.
///
/// # Arguments
///
/// * `value` - The value to expand
/// * `expansion_type` - The expanstion type (unix/windows/unix prefix/...)
///
/// # Example
///
/// ```
/// use envmnt::{ExpandOptions, ExpansionType};
///
/// fn main() {
///     envmnt::set("MY_ENV", "my expanded value");
///     let mut options = ExpandOptions::new();
///     options.expansion_type = Some(ExpansionType::Unix);
///     let value = envmnt::expand("Env: MY_ENV value is: ${MY_ENV}", Some(options));
///     assert_eq!("Env: MY_ENV value is: my expanded value", &value);
/// }
/// ```
pub fn expand(value: &str, options: Option<ExpandOptions>) -> String {
    environment::expand(&value, options)
}

macro_rules! generate_get_numeric {
    ($fn_name:ident, $type:ty) => {
        /// Returns the environment variable value or a default value
        /// in case the variable is not defined or cannot be parsed.
        ///
        /// # Arguments
        ///
        /// * `key` - The environment variable name
        /// * `default_value` - Returned if the variable does not exist or cannot be parsed
        ///
        pub fn $fn_name<K: AsRef<OsStr>>(key: K, default_value: $type) -> $type {
            numeric::$fn_name(key, default_value)
        }
    };
}

generate_get_numeric!(get_u8, u8);
generate_get_numeric!(get_i8, i8);
generate_get_numeric!(get_i16, i16);
generate_get_numeric!(get_u16, u16);
generate_get_numeric!(get_i32, i32);
generate_get_numeric!(get_u32, u32);
generate_get_numeric!(get_i64, i64);
generate_get_numeric!(get_u64, u64);
generate_get_numeric!(get_i128, i128);
generate_get_numeric!(get_u128, u128);
generate_get_numeric!(get_f32, f32);
generate_get_numeric!(get_f64, f64);
generate_get_numeric!(get_isize, isize);
generate_get_numeric!(get_usize, usize);

macro_rules! generate_set_numeric {
    ($fn_name:ident, $type:ty) => {
        /// Sets the environment variable value.
        ///
        /// # Arguments
        ///
        /// * `key` - The environment variable name
        /// * `value` - The new variable value
        ///
        pub fn $fn_name<K: AsRef<OsStr>>(key: K, value: $type) {
            numeric::$fn_name(key, value)
        }
    };
}

generate_set_numeric!(set_u8, u8);
generate_set_numeric!(set_i8, i8);
generate_set_numeric!(set_i16, i16);
generate_set_numeric!(set_u16, u16);
generate_set_numeric!(set_i32, i32);
generate_set_numeric!(set_u32, u32);
generate_set_numeric!(set_i64, i64);
generate_set_numeric!(set_u64, u64);
generate_set_numeric!(set_i128, i128);
generate_set_numeric!(set_u128, u128);
generate_set_numeric!(set_f32, f32);
generate_set_numeric!(set_f64, f64);
generate_set_numeric!(set_isize, isize);
generate_set_numeric!(set_usize, usize);

/// Increments and returns the new value stored by the given environment variable key.
/// In case the variable does not exist, it will increment to 1.
/// The updated value will be returned.
///
/// # Arguments
///
/// * `key` - The environment variable name
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set_u8("ENV_VAR", 5);
///     let value = envmnt::increment("ENV_VAR");
///     assert_eq!(value, 6);
/// }
/// ```
pub fn increment<K: AsRef<OsStr>>(key: K) -> isize {
    numeric::increment(key)
}

/// Decrements and returns the new value stored by the given environment variable key.
/// In case the variable does not exist, it will decrement to -1.
/// The updated value will be returned.
///
/// # Arguments
///
/// * `key` - The environment variable name
///
/// # Example
///
/// ```
/// fn main() {
///     envmnt::set_u8("ENV_VAR", 5);
///     let value = envmnt::decrement("ENV_VAR");
///     assert_eq!(value, 4);
/// }
/// ```
pub fn decrement<K: AsRef<OsStr>>(key: K) -> isize {
    numeric::decrement(key)
}