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
//! This crate contains a new data structure that acts as a wrapper around a `HashMap`. //! It provides its own data enum for values `(CfgValue)`, and contains multiple helper functions //! that let you navigate the hashmap easily. //! //! Its primary purpose is for configuration, allowing for validation as well. In essence, a `CfgMap` //! would represent a configuration for an application. So far, alternatives for configuration would be //! to use a data format library directly, or utilise a struct that a //! configuration file, like JSON or TOML, would serialise into. //! //! This can be more than satisfactory, especially for basic configurations, however in certain situations //! it can prove to be more than a bit cumbersome. For example, if you plan on using default options in the case //! that certain options aren't set, having multiple nested objects to validate and go through, etc. //! //! If you'd like to use the most common features supplied by this crate, you can simply do: //! //! ``` //! use cfgmap::prelude::*; //! ``` //! //! This will include the `CfgMap`, all `CfgValue`s, all public macros, all `Conditions`, and the `Checkable` trait. //! //! ## Features //! //! This crate is customizable, allowing for multiple features depending on your needs: //! - `from_toml`: Allows to create a hashmap from `TOML` values, also having an additional `Datetime` `CfgValue`. //! - `from_json`: Allows to create a hashmap from `JSON` values, also having an additional `Null` `CfgValue`. //! - `generator`: Includes additional methods for `CfgValue`s that allows for generating numbers (int or float) using a value. //! //! ## Tutorial (of sorts): //! //! It is very easy to make a new `CfgMap`, there are multiple methods: //! //! ``` //! use cfgmap::CfgMap; //! //! let map1 = CfgMap::new(); //! let mut map2 = CfgMap::new(); //! map2.default = "default".into(); //! ``` //! //! `CfgMap` allows for some functionality with regards to default values. For `map1` above, `default` was never set, so //! the values would be retrieved from the root. For `map2` however, it's assumed that all default values are located in //! `default`. //! //! ### Path syntax //! //! `CfgMap` also comes with support for a certain `path` syntax with its keys: //! //! ``` //! # use cfgmap::CfgMap; //! # let cfgmap = CfgMap::new(); //! cfgmap.get("hello/there/pal"); //! ``` //! //! This helps to make access to nested items easy. The line above is essentially equal to: //! //! ``` //! # use cfgmap::CfgMap; //! # let map = CfgMap::new(); //! map.get("hello") //! .and_then(|a| a.as_map()) //! .and_then(|a| a.get("there")) //! .and_then(|a| a.as_map()) //! .and_then(|a| a.get("pal")); //! ``` //! //! Note that if `hello` or `there` weren't `CfgMap`s as well, the whole expression would evaluate to `None`. //! This key can also contain array indexes. For example, with `a/0/c`, it will check whether `a` is a `Map` or //! a `List`. If its the former, it will try to find a key with the value `0`. If its the latter, it will instead //! try to index into the list. //! //! ### Conditions //! //! Now, what if you want to check what a certain value evaluates to? This is something that you'll encounter //! very quickly if you'd like to use any value. This crate comes with an extensive support for `Conditions`! //! //! ``` //! # use cfgmap::CfgMap; //! use cfgmap::{Condition::*, Checkable}; //! # let cfgmap = CfgMap::new(); //! let is_number = cfgmap.get("hello/there/pal").check_that(IsInt | IsFloat); //! ``` //! //! The above line will check whether the value at `hello/there/pal` is a `CfgValue::Int` or a `CfgValue::Float`. //! There are more conditions listed [*here*](./enum.Condition.html). If there are more conditions that you'd like added, //! feel free to open up an issue or open a PR! All of these serve as utilities to help validate a certain value. //! //! ### Default values //! //! Defaults can also be used quite easily:+ //! //! ``` //! # use cfgmap::CfgMap; //! # let map = CfgMap::new(); //! map.get_option("http_settings", "ip_address"); //! ``` //! //! Let's say that `map` was initialised with its default at `default`. The above line will be equivalent to the following: //! //! ``` //! # use cfgmap::CfgMap; //! # let map = CfgMap::new(); //! map.get("http_settings/ip_address").or(map.get("default/ip_address")); //! ``` //! //! You can also update an option like this, using `update_option`. This works similar to using `add`, except that it doesn't //! add a new option if it isn't found, only updating an existing one. //! //! ### HashMap methods //! //! All `HashMap` methods are also available, since `CfgMap` implements `Deref` and `DerefMut` for `HashMap<String, CfgValue>`. //! For example, you can call `.iter()` on it, even though that is not directly implemented. //! //! ## Complete example //! ```ignore //! use cfgmap::{CfgMap, CfgValue::*, Condition::*, Checkable}; //! //!let toml = toml::toml! { //! [package] //! name = "cfgmap" //! version = "0.1.0" //! authors = ["Andrea Jenkins <mctech26@gmail.com>"] //! //! [lib] //! name = "cfgmap" //! path = "src/cfgmap.rs" //! //! [dependencies] //! serde_json = { version = "1.0.48", optional = true } //! toml = { version = "0.5.6", optional = true } //! //! [other] //! date = 2020-02-29 //! float = 1.2 //! int = 3 //! internal.more = "hello" //! //! [[person]] //! name = "a" //! //! [[person]] //! name = "b" //!}; //! //!let cmap = CfgMap::from_toml(toml); //! //!assert!(cmap.get("package/name").check_that(IsExactlyStr("cfgmap".into()))); //!assert!(cmap.get("package/version").check_that(IsExactlyStr("0.1.0".into()))); //!assert!(cmap.get("package/authors").check_that(IsExactlyList(vec![Str("Andrea Jenkins <mctech26@gmail.com>".into())]))); //! //!assert!(cmap.get("lib/name").check_that(IsExactlyStr("cfgmap".into()))); //!assert!(cmap.get("lib/path").check_that(IsExactlyStr("src/cfgmap.rs".into()))); //! //!assert!(cmap.get("dependencies/serde_json/version").check_that(IsExactlyStr("1.0.48".into()))); //!assert!(cmap.get("dependencies/serde_json/optional").check_that(IsTrue)); //!assert!(cmap.get("dependencies/toml/version").check_that(IsExactlyStr("0.5.6".into()))); //!assert!(cmap.get("dependencies/toml/optional").check_that(IsTrue)); //! //!assert!(cmap.get("other/date").check_that(IsDatetime)); //!assert!(cmap.get("other/float").check_that(IsExactlyFloat(1.2))); //!assert!(cmap.get("other/int").check_that(IsExactlyInt(3))); //!assert!(cmap.get("other/internal/more").check_that(IsExactlyStr("hello".into()))); //! //!assert!(cmap.get("person").check_that(IsListWith(Box::new(IsMap)))); //!assert!(cmap.get("person/0/name").check_that(IsExactlyStr("a".into()))); //!assert!(cmap.get("person/1/name").check_that(IsExactlyStr("b".into()))); //! ``` use std::collections::HashMap; mod conditions; pub use conditions::{Checkable, Condition}; use std::concat; use std::mem; use std::ops::Deref; use std::ops::DerefMut; #[cfg(feature = "from_json")] use serde_json::Value as JsonValue; #[cfg(feature = "from_toml")] use toml::value::Value as TomlValue; #[cfg(feature = "from_json")] mod from_json; #[cfg(feature = "from_toml")] mod from_toml; #[cfg(feature = "from_toml")] use toml::value::Datetime; #[cfg(feature = "generator")] use rand::Rng; // The type contained within `CfgValue::Int` pub(crate) type _Int = i64; // The type contained within `CfgValue::Float` pub(crate) type _Float = f64; // The type contained within `CfgValue::Str` pub(crate) type _Str = String; /// The type contained within `CfgValue::Bool` pub(crate) type _Bool = bool; macro_rules! doc_comment { ($x:expr, $($tt:tt)*) => { #[doc = $x] $($tt)* }; } macro_rules! is_type { ($fn_name:ident, $enum_type:path) => { doc_comment! { concat!("Checks whether the enum is a `", stringify!($enum_type), "`."), pub fn $fn_name (&self) -> bool { if let $enum_type(..) = self { true } else { false } } } }; ($fn_name:ident [0], $enum_type:path) => { doc_comment! { concat!("Checks whether the enum is a `", stringify!($enum_type), "`."), pub fn $fn_name (&self) -> bool { if let $enum_type = self { true } else { false } } } } } macro_rules! as_type { ($fn_name:ident, $type:ty, $enum_type:path) => { doc_comment! { concat!("Returns a reference to the `", stringify!($type), "`. Result is `None` if contents aren't a `", stringify!($enum_type), "`."), pub fn $fn_name (&self) -> Option<&$type> { if let $enum_type(x) = self { Some(x) } else { None } } } }; } macro_rules! as_mut_type { ($fn_name:ident, $type:ty, $enum_type:path) => { doc_comment! { concat!("Returns a reference to the `", stringify!($type), "`. Result is `None` if contents aren't a `", stringify!($enum_type), "`."), pub fn $fn_name (&mut self) -> Option<&mut $type> { if let $enum_type(x) = self { Some(x) } else { None } } } }; } macro_rules! from_int { ($($type:ty),*) => { $( impl From<$type> for CfgValue { fn from(i: $type) -> Self { CfgValue::Int(i.into()) } } )* }; } macro_rules! from_float { ($($type:ty),*) => { $( impl From<$type> for CfgValue { fn from(f: $type) -> Self { CfgValue::Float(f.into()) } } )* }; } macro_rules! from_str { ($($type:ty),*) => { $( impl From<$type> for CfgValue { fn from(s: $type) -> Self { CfgValue::Str(s.into()) } } )* }; } #[macro_export] /// Creates a `CfgValue` value using the passed variable. /// /// ## Examples: /// ``` /// # use cfgmap::prelude::*; /// let s = value!(4); /// let x = value!(3.2); /// let y = value!("hello there"); /// let a = value!(vec![value!(3), value!(9.4), value!("amazing")]); /// let m = value!(CfgMap::new()); /// /// assert!(s.check_that(IsInt)); /// assert!(x.check_that(IsFloat)); /// assert!(y.check_that(IsStr)); /// assert!(a.check_that(IsListWith(Box::new(IsInt | IsFloat | IsStr)))); /// assert!(m.check_that(IsMap)); /// ``` macro_rules! value { ($($tt:tt)*) => { { let __value: $crate::CfgValue = ($($tt)*).into(); __value } }; } #[macro_export] /// Creates a `CfgValue::List` from the values passed. /// Works very similarly to the `vec!` macro. /// /// ## Examples: /// ``` /// # use cfgmap::prelude::*; /// let arr1 = list![2, 3.2, "hello there"]; /// let arr2 = value!(vec![value!(2), value!(3.2), value!("hello there")]); /// /// assert_eq!(arr1, arr2); /// ``` macro_rules! list { ($($tt:tt),*) => { value!(vec![$(value!($tt)),*]) }; } impl From<bool> for CfgValue { fn from(b: bool) -> Self { CfgValue::Bool(b) } } impl From<Vec<CfgValue>> for CfgValue { fn from(l: Vec<CfgValue>) -> Self { CfgValue::List(l) } } impl From<CfgMap> for CfgValue { fn from(m: CfgMap) -> Self { CfgValue::Map(m) } } from_int!(u8, u16, u32, i8, i16, i32, i64); from_float!(f32, f64); from_str!(&str, String); /// Represents a value within a `CfgMap` #[derive(Debug, Clone, PartialEq)] pub enum CfgValue { /// Represents an integer value. Int(_Int), /// Represents a float value. Float(_Float), /// Represents a string. Str(_Str), /// Represents a bool. Bool(_Bool), /// Represents a nested configuration map. Map(CfgMap), /// Represents a list of values. These values can have differing types. List(Vec<CfgValue>), /// Represents a `Datetime`. Only available if using `from_toml`. #[cfg(feature = "from_toml")] Datetime(Datetime), /// Represents a null value. Only available if using `from_json`. #[cfg(feature = "from_json")] Null, } impl CfgValue { /// Assumes the value is a `CfgMap` and attempts to execute `.get()` on it. /// Returns `None` if the value isn't a `CfgMap`, or for any reasons `.get()` /// may return `None`. pub fn get(&self, key: &str) -> Option<&CfgValue> { self.as_map().and_then(|map| map.get(key)) } /// Assumes the value is a `CfgMap` and attempts to execute `.get_mut()` on it. /// Returns `None` if the value isn't a `CfgMap`, or for any reasons `.get_mut()` /// may return `None`. pub fn get_mut(&mut self, key: &str) -> Option<&mut CfgValue> { self.as_map_mut().and_then(|map| map.get_mut(key)) } #[cfg(feature = "generator")] /// Generates an integer using the value, using `rand`. There are 3 total cases this function handles: /// /// - `Int(x)`: returns x /// - `List([Int(x)])`: returns x /// - `List([Int(x),Int(y)])`: returns an integer between x and y. /// - Else: returns `None`. /// /// ## Examples: /// ``` /// # use cfgmap::{CfgValue::*}; /// /// let num = Int(5); /// let vnum = List(vec![Int(10)]); /// let range = List(vec![Int(10), Int(20)]); /// /// assert_eq!(5, num.generate_int().unwrap()); /// assert_eq!(10, vnum.generate_int().unwrap()); /// /// let generated = range.generate_int().unwrap(); /// assert!((generated >= 10) & (generated < 20)); /// ``` pub fn generate_int(&self) -> Option<i64> { let validate = |size| Condition::IsListWith(Box::new(Condition::IsInt)) & Condition::IsListWithLength(size); if self.check_that(Condition::IsInt) { Some(*self.as_int().unwrap()) } else if self.check_that(validate(1)) { Some(*self.as_list().unwrap().get(0).unwrap().as_int().unwrap()) } else if self.check_that(validate(2)) { let list = self.as_list().unwrap(); let min = *list.get(0).unwrap().as_int().unwrap(); let max = *list.get(1).unwrap().as_int().unwrap(); Some(rand::thread_rng().gen_range(min, max)) } else { None } } #[cfg(feature = "generator")] /// Generates an float using the value, using `rand`. There are 3 total cases this function handles: /// /// - `Float(x)`: returns x /// - `List([Float(x)])`: returns x /// - `List([Float(x),Float(y)])`: returns an integer between x and y. /// - Else: returns `None`. /// /// ## Examples: /// ``` /// # use cfgmap::{CfgValue::*}; /// /// let num = Float(5.0); /// let vnum = List(vec![Float(10.0)]); /// let range = List(vec![Float(10.0), Float(20.0)]); /// /// assert_eq!(5.0, num.generate_float().unwrap()); /// assert_eq!(10.0, vnum.generate_float().unwrap()); /// /// let generated = range.generate_float().unwrap(); /// assert!((generated >= 10.0) & (generated < 20.0)); /// ``` pub fn generate_float(&self) -> Option<f64> { let validate = |size| Condition::IsListWith(Box::new(Condition::IsFloat)) & Condition::IsListWithLength(size); if self.check_that(Condition::IsFloat) { Some(*self.as_float().unwrap()) } else if self.check_that(validate(1)) { Some(*self.as_list().unwrap().get(0).unwrap().as_float().unwrap()) } else if self.check_that(validate(2)) { let list = self.as_list().unwrap(); let min = *list.get(0).unwrap().as_float().unwrap(); let max = *list.get(1).unwrap().as_float().unwrap(); Some(rand::thread_rng().gen_range(min, max)) } else { None } } /// Returns the contents of the enum converted into an integer, if possible. /// /// If the enum represents a float, it will be converted into an integer. pub fn to_int(&self) -> Option<_Int> { if let CfgValue::Int(x) = self { Some(*x) } else if let CfgValue::Float(x) = self { Some(*x as _Int) } else { None } } /// Returns the contents of the enum converted into a float, if possible. /// /// If the enum represents an integer, it will be converted into a float. pub fn to_float(&self) -> Option<_Float> { if let CfgValue::Float(x) = self { Some(*x) } else if let CfgValue::Int(x) = self { Some(*x as _Float) } else { None } } is_type!(is_int, CfgValue::Int); is_type!(is_float, CfgValue::Float); is_type!(is_str, CfgValue::Str); is_type!(is_bool, CfgValue::Bool); is_type!(is_map, CfgValue::Map); is_type!(is_list, CfgValue::List); #[cfg(feature = "from_json")] is_type!(is_null [0], CfgValue::Null); #[cfg(feature = "from_toml")] is_type!(is_datetime, CfgValue::Datetime); as_type!(as_int, _Int, CfgValue::Int); as_type!(as_float, _Float, CfgValue::Float); as_type!(as_str, _Str, CfgValue::Str); as_type!(as_bool, _Bool, CfgValue::Bool); as_type!(as_map, CfgMap, CfgValue::Map); as_type!(as_list, Vec<CfgValue>, CfgValue::List); as_mut_type!(as_int_mut, _Int, CfgValue::Int); as_mut_type!(as_float_mut, _Float, CfgValue::Float); as_mut_type!(as_str_mut, _Str, CfgValue::Str); as_mut_type!(as_bool_mut, _Bool, CfgValue::Bool); as_mut_type!(as_map_mut, CfgMap, CfgValue::Map); as_mut_type!(as_list_mut, Vec<CfgValue>, CfgValue::List); } impl conditions::Checkable for CfgValue { fn check_that(&self, c: conditions::Condition) -> bool { return c.execute(self).to_bool(); } } impl conditions::Checkable for Option<CfgValue> { fn check_that(&self, condition: conditions::Condition) -> bool { self.as_ref().map_or(false, |val| val.check_that(condition)) } } impl conditions::Checkable for Option<&CfgValue> { fn check_that(&self, condition: conditions::Condition) -> bool { self.as_ref().map_or(false, |val| val.check_that(condition)) } } impl conditions::Checkable for Option<&mut CfgValue> { fn check_that(&self, condition: conditions::Condition) -> bool { self.as_ref().map_or(false, |val| val.check_that(condition)) } } fn split_once(in_string: &str, pat: char) -> (String, Option<String>) { if in_string.find(pat).is_none() { return (in_string.into(), None); } let mut splitter = in_string.splitn(2, pat); let first = splitter.next().unwrap().to_string(); let second = splitter.next().unwrap().to_string(); (first, Some(second)) } fn rsplit_once(in_string: &str, pat: char) -> (Option<String>, String) { if in_string.find(pat).is_none() { return (None, in_string.into()); } let mut splitter = in_string.rsplitn(2, pat); let first = splitter.next().unwrap().to_string(); let second = splitter.next().unwrap().to_string(); (Some(second), first) } impl Deref for CfgMap { type Target = HashMap<String, CfgValue>; fn deref(&self) -> &Self::Target { &self.internal_map } } impl DerefMut for CfgMap { fn deref_mut (&mut self) -> &mut Self::Target { &mut self.internal_map } } #[cfg(feature = "from_json")] impl From<Option<CfgValue>> for CfgValue { fn from(opt: Option<CfgValue>) -> Self { opt.unwrap_or(CfgValue::Null) } } /// A configuration map, containing helper functions and effectively being a wrapper /// around a `HashMap`s. #[derive(Debug, Clone, PartialEq)] pub struct CfgMap { /// An internal map representing the configuration. internal_map: HashMap<String, CfgValue>, /// A path to the default subobject. pub default: String } impl CfgMap { /// Creates a new empty CfgMap. pub fn new() -> CfgMap { CfgMap { internal_map: HashMap::new(), default: String::new() } } /// Initialises a `CfgMap` using the `map` that's passed in. pub fn with_hashmap(map: HashMap<String, CfgValue>) -> CfgMap { CfgMap { internal_map: map, default: String::new() } } #[cfg(feature = "from_json")] /// Initialises a `CfgMap` from a json `Value`. pub fn from_json(value: JsonValue) -> CfgMap { from_json::json_to_cfg(value) } #[cfg(feature = "from_toml")] /// Initialises a `CfgMap` from a toml `Value`. pub fn from_toml(value: TomlValue) -> CfgMap { from_toml::toml_to_cfg(value) } /// Adds a new entry in the configuration. /// /// The `key` can be of the form of the path `"a/b/...y/z/"`, in which case it will /// get the inner submap `a/b/...y/`, and add `z` onto it. This is for convenience sake, /// as doing this manually can prove to be verbose. /// /// This key can also index into lists. So, for example `a/0/b` would try checking if `"a"` /// is a list, and index into it. Otherwise it will try to find an internal map with the key `0`. /// /// In order to add a default value to a normal submap - you would need to do this manually, /// as this function will always use `get_mut`. /// /// ## Examples /// /// ``` /// use cfgmap::{CfgMap, CfgValue::*}; /// /// let mut cmap = CfgMap::new(); /// /// // Works - a root add like this will always work. /// assert!(cmap.add("k1", Int(5)).is_ok()); /// /// // Doesn't work, because k1 isn't a map. /// assert!(cmap.add("k1/k2", Int(10)).is_err()); /// /// // Works - returns the old value. /// let r = cmap.add("k1", Float(8.0)); /// assert_eq!(Ok(Some(Int(5))), r); /// ``` /// /// ## Return values /// /// - `Err` if the path as specified by `key` isn't found. In the case above for example, `get_mut("a")` returns a `None`. /// - `Ok(Some(CfgValue))` if the path as specified by key already contained a value, and was overwritten. In this case, the old value is returned. /// - `Ok(None)` otherwise. pub fn add(&mut self, key: &str, value: CfgValue) -> Result<Option<CfgValue>, ()> { let (path, key) = rsplit_once(key, '/'); if path.is_none(){ Ok(self.internal_map.insert(key.to_string(), value)) } else { let subtree = self.get_mut(&path.unwrap()); if subtree.check_that(Condition::IsMap) { subtree.unwrap().as_map_mut().unwrap().add(&key, value) } else { Err(()) } } } /// Gets a reference to a value from within the configuration. /// /// The `key` can be of the form of the path `"a/b/...y/z/"`, in which case it will /// go through the inner submaps `"a/b/..."` until a submap isn't found, or the end is reached. /// This is for convenience sake, as doing this manually can prove to be verbose. /// /// This key can also index into lists. So, for example `a/0/b` would try checking if `"a"` /// is a list, and index into it. Otherwise it will try to find an internal map with the key `0`. /// /// Returns `None` if the key doesn't exist. /// /// ## Examples /// ``` /// use cfgmap::{CfgMap, CfgValue::*, Condition::*, Checkable}; /// /// let mut cmap = CfgMap::new(); /// let mut submap = CfgMap::new(); /// /// submap.add("key", Int(5)); /// /// cmap.add("sub", Map(submap)); /// /// assert!(cmap.get("sub").check_that(IsMap)); /// assert!(cmap.get("sub/key").check_that(IsExactlyInt(5))); /// ``` pub fn get(&self, key: &str) -> Option<&CfgValue> { let (h, t) = split_once(key, '/'); if t.is_none() { self.internal_map.get(key) } else { let next = self.internal_map.get(&h); if let Some(CfgValue::Map(map)) = next { map.get(&t.unwrap()) } else if let Some(CfgValue::List(list)) = next { // Get the next segment of the path, and parse as a list index. let (index,new_t) = split_once(&t.unwrap(), '/'); let index = index.parse::<usize>(); // If it's an invalid usize, then the whole path is invalid. if index.is_err() { None } else if new_t.is_none() { list.get(index.unwrap()) } else { list.get(index.unwrap()).and_then(|op| { op.as_map() }).and_then(|map| { map.get(&new_t.unwrap()) }) } } else { None } } } /// Gets a mutable reference to a value from within the configuration. /// /// Returns `None` if the key doesn't exist. /// /// The `key` can be of the form of the path `"a/b/...y/z/"`, in which case it will /// go through the inner submaps `"a/b/..."` until a submap isn't found, or the end is reached. /// This is for convenience sake, as doing this manually can prove to be verbose. /// /// This key can also index into lists. So, for example `a/0/b` would try checking if `"a"` /// is a list, and index into it. Otherwise it will try to find an internal map with the key `0`. /// /// ## Examples /// ``` /// use cfgmap::{CfgMap, CfgValue::*, Condition::*, Checkable}; /// /// let mut cmap = CfgMap::new(); /// let mut submap = CfgMap::new(); /// /// cmap.add("sub", Map(submap)); /// /// let mut submap = cmap.get_mut("sub"); /// assert!(submap.check_that(IsMap)); /// /// submap.unwrap().as_map_mut().unwrap().add("key", Int(5)); /// assert!(cmap.get_mut("sub/key").check_that(IsExactlyInt(5))); /// ``` pub fn get_mut(&mut self, key: &str) -> Option<&mut CfgValue> { let (h, t) = split_once(key, '/'); if t.is_none() { self.internal_map.get_mut(key) } else { let next = self.internal_map.get_mut(&h); if let Some(CfgValue::Map(map)) = next { map.get_mut(&t.unwrap()) } else if let Some(CfgValue::List(list)) = next { // Get the next segment of the path, and parse as a list index. let (index,new_t) = split_once(&t.unwrap(), '/'); let index = index.parse::<usize>(); // If it's an invalid usize, then the whole path is invalid. if index.is_err() { None } else if new_t.is_none() { list.get_mut(index.unwrap()) } else { list.get_mut(index.unwrap()).and_then(|op| { op.as_map_mut() }).and_then(|map| { map.get_mut(&new_t.unwrap()) }) } } else { None } } } /// Deletes a key from the map, and returns the value associated with it. /// /// Returns `None` if the key doesn't exist. /// /// The `key` can be of the form of the path `"a/b/...y/z/"`, in which case it will /// go through the inner submaps `"a/b/..."` until a submap isn't found, or the end is reached. /// This is for convenience sake, as doing this manually can prove to be verbose. /// /// This key can also index into lists. So, for example `a/0/b` would try checking if `"a"` /// is a list, and index into it. Otherwise it will try to find an internal map with the key `0`. /// /// ## Examples /// ``` /// use cfgmap::{CfgMap, CfgValue::*, Condition::*, Checkable}; /// /// let mut cmap = CfgMap::new(); /// /// cmap.add("sub", Map(CfgMap::new())); /// cmap.add("sub/int", Int(5)); /// /// let num = cmap.remove("sub/int"); /// let nothing = cmap.remove("sub/nothing"); /// /// assert!(cmap.get("sub/int").is_none()); /// assert!(num.check_that(IsExactlyInt(5))); /// assert!(nothing.is_none()); /// ``` pub fn remove(&mut self, key: &str) -> Option<CfgValue> { self.remove_entry(key).map(|(_, value)| value) } /// Deletes a key from the map, and returns the value associated with it, if the value obeys the /// conditions as passed. Useful for when you want to make sure to avoid deleting another value. /// /// Returns `None` if the key doesn't exist, or the value associated with the key doesn't obey the condition. /// /// The `key` can be of the form of the path `"a/b/...y/z/"`, in which case it will /// go through the inner submaps `"a/b/..."` until a submap isn't found, or the end is reached. /// This is for convenience sake, as doing this manually can prove to be verbose. /// /// This key can also index into lists. So, for example `a/0/b` would try checking if `"a"` /// is a list, and index into it. Otherwise it will try to find an internal map with the key `0`. /// /// ## Examples /// ``` /// use cfgmap::{CfgMap, CfgValue::*, Condition::*, Checkable}; /// /// let mut cmap = CfgMap::new(); /// /// cmap.add("sub", Map(CfgMap::new())); /// cmap.add("sub/int", Int(5)); /// /// let float = cmap.remove_if("sub/int", IsFloat); /// assert!(cmap.get("sub/int").is_some()); /// assert!(float.is_none()); /// /// let int = cmap.remove_if("sub/int", IsInt); /// assert!(cmap.get("sub/int").is_none()); /// assert!(int.check_that(IsExactlyInt(5))); /// ``` pub fn remove_if(&mut self, key: &str, condition: Condition) -> Option<CfgValue> { if self.get(key).check_that(condition) { self.remove(key) } else { None } } /// Deletes a key from the map, and returns the key and value associated with it. /// /// Returns `None` if the key doesn't exist. /// /// The `key` can be of the form of the path `"a/b/...y/z/"`, in which case it will /// go through the inner submaps `"a/b/..."` until a submap isn't found, or the end is reached. /// This is for convenience sake, as doing this manually can prove to be verbose. /// /// This key can also index into lists. So, for example `a/0/b` would try checking if `"a"` /// is a list, and index into it. Otherwise it will try to find an internal map with the key `0`. /// /// ## Examples /// ``` /// use cfgmap::{CfgMap, CfgValue::*, Condition::*, Checkable}; /// /// let mut cmap = CfgMap::new(); /// /// cmap.add("sub", Map(CfgMap::new())); /// cmap.add("sub/int", Int(5)); /// /// let (key, num) = cmap.remove_entry("sub/int").unwrap(); /// let nothing = cmap.remove("sub/nothing"); /// /// assert!(cmap.get("sub/int").is_none()); /// assert_eq!(key, "int"); /// assert!(num.check_that(IsExactlyInt(5))); /// assert!(nothing.is_none()); /// ``` pub fn remove_entry(&mut self, key: &str) -> Option<(String, CfgValue)> { let (path, key) = rsplit_once(key, '/'); if path.is_none(){ self.internal_map.remove_entry(&key) } else { let subtree = self.get_mut(&path.unwrap()); if subtree.check_that(Condition::IsMap) { subtree.unwrap().as_map_mut().unwrap().remove_entry(&key) } else { None } } } /// Deletes a key from the map, and returns the key and value associated with it, if the value obeys the /// conditions as passed. Useful for when you want to make sure to avoid deleting another value. /// /// Returns `None` if the key doesn't exist, or the value associated with the key doesn't obey the condition. /// /// The `key` can be of the form of the path `"a/b/...y/z/"`, in which case it will /// go through the inner submaps `"a/b/..."` until a submap isn't found, or the end is reached. /// This is for convenience sake, as doing this manually can prove to be verbose. /// /// This key can also index into lists. So, for example `a/0/b` would try checking if `"a"` /// is a list, and index into it. Otherwise it will try to find an internal map with the key `0`. /// /// ## Examples /// ``` /// use cfgmap::{CfgMap, CfgValue::*, Condition::*, Checkable}; /// /// let mut cmap = CfgMap::new(); /// /// cmap.add("sub", Map(CfgMap::new())); /// cmap.add("sub/int", Int(5)); /// /// let float = cmap.remove_entry_if("sub/int", IsFloat); /// assert!(cmap.get("sub/int").is_some()); /// assert!(float.is_none()); /// /// let (key, int) = cmap.remove_entry_if("sub/int", IsInt).unwrap(); /// assert!(cmap.get("sub/int").is_none()); /// assert_eq!(key, "int"); /// assert!(int.check_that(IsExactlyInt(5))); /// ``` pub fn remove_entry_if(&mut self, key: &str, condition: Condition) -> Option<(String, CfgValue)> { if self.get(key).check_that(condition) { self.remove_entry(key) } else { None } } /// Checks whether a certain path exists. /// /// The `key` can be of the form of the path `"a/b/...y/z/"`, in which case it will /// go through the inner submaps `"a/b/..."` until a submap isn't found, or the end is reached. /// This is for convenience sake, as doing this manually can prove to be verbose. /// /// This key can also index into lists. So, for example `a/0/b` would try checking if `"a"` /// is a list, and index into it. Otherwise it will try to find an internal map with the key `0`. /// /// ## Examples /// ``` /// use cfgmap::{CfgMap, CfgValue::*, Condition::*, Checkable}; /// /// let mut cmap = CfgMap::new(); /// let mut submap = CfgMap::new(); /// /// cmap.add("num", Int(10)); /// submap.add("num", Int(20)); /// cmap.add("sub", Map(submap)); /// /// assert!(cmap.contains_key("num")); /// assert!(cmap.contains_key("sub/num")); /// ``` pub fn contains_key(&self, key: &str) -> bool { self.get(key).is_some() } /// Gets a reference to an option within the configuration. /// /// It first tries to get /// `category/option` within the normal values. If this doesn't exist, it will then /// try to retrieve `option` from the default path instead (`self.default/option`). /// /// Note that if `default` wasn't set on construction, this function will instead retrieve /// the value from the root directory (`option`) directly. /// /// Returns `None` if the key doesn't exist in either map. /// /// The `key` can be of the form of the path `"a/b/...y/z/"`, in which case it will /// go through the inner submaps `"a/b/..."` until a submap isn't found, or the end is reached. /// This is for convenience sake, as doing this manually can prove to be verbose. /// /// This key can also index into lists. So, for example `a/0/b` would try checking if `"a"` /// is a list, and index into it. Otherwise it will try to find an internal map with the key `0`. /// /// ## Examples /// ``` /// use cfgmap::{CfgMap, CfgValue::*, Checkable, Condition::*}; /// /// let mut cmap = CfgMap::new(); /// let mut submap = CfgMap::new(); /// /// submap.add("OP1", Int(5)); /// cmap.add("OP1", Int(8)); /// /// cmap.add("sub", Map(submap)); /// /// assert!(cmap.get_option("sub", "OP1").check_that(IsExactlyInt(5))); /// assert!(cmap.get_option("sub", "OP1").check_that(IsExactlyInt(5))); /// assert!(cmap.get_option("sub", "OP2").is_none()); /// ``` pub fn get_option(&self, category: &str, option: &str) -> Option<&CfgValue> { let fullkey = format!("{}/{}", category, option); let default = format!("{}{}", self.default, option); self.get(&fullkey).or(self.get(&default)) } /// Updates the option with the new value `to`. /// /// It first tries to get /// `category/option` within the normal values. If this doesn't exist, it will then /// try to retrieve `option` from the default path instead (`self.default/option`). /// /// Note that if `default` wasn't set on construction, this function will instead retrieve /// the value from the root directory (`option`) directly. /// /// The `key` can be of the form of the path `"a/b/...y/z/"`, in which case it will /// go through the inner submaps `"a/b/..."` until a submap isn't found, or the end is reached. /// This is for convenience sake, as doing this manually can prove to be verbose. /// /// This key can also index into lists. So, for example `a/0/b` would try checking if `"a"` /// is a list, and index into it. Otherwise it will try to find an internal map with the key `0`. /// /// ## Examples /// ``` /// use cfgmap::{CfgMap, CfgValue::*, Checkable, Condition::*}; /// /// let mut cmap = CfgMap::new(); /// let mut submap = CfgMap::new(); /// /// submap.add("OP1", Int(5)); /// cmap.add("OP1", Int(8)); /// /// cmap.add("sub", Map(submap)); /// /// let ol1 = cmap.update_option("sub", "OP1", Int(10)); /// let ol2 = cmap.update_option("foo", "OP1", Int(16)); /// let ol3 = cmap.update_option("sub", "OP2", Int(99)); /// /// assert!(cmap.get_option("sub", "OP1").check_that(IsExactlyInt(10))); /// assert!(cmap.get_option("foo", "OP1").check_that(IsExactlyInt(16))); /// assert!(cmap.get_option("sub", "OP2").is_none()); /// /// assert_eq!(ol1, Some(Int(5))); /// assert_eq!(ol2, Some(Int(8))); /// assert_eq!(ol3, None); /// ``` pub fn update_option(&mut self, category: &str, option: &str, to: CfgValue) -> Option<CfgValue> { let fullkey = format!("{}/{}", category, option); let default = format!("{}{}", self.default, option); if let Some(x) = self.get_mut(&fullkey) { Some(mem::replace(x, to)) } else if let Some(x) = self.get_mut(&default) { Some(mem::replace(x, to)) } else { None } } } #[cfg(feature = "from_json")] impl From<JsonValue> for CfgMap { fn from(opt: JsonValue) -> Self { CfgMap::from_json(opt) } } #[cfg(feature = "from_toml")] impl From<TomlValue> for CfgMap { fn from(opt: TomlValue) -> Self { CfgMap::from_toml(opt) } } pub mod prelude { pub use crate::{CfgMap, CfgValue::*, Checkable, Condition::*, value, list}; } #[cfg(test)] mod tests { #[cfg(feature = "from_json")] use serde_json; #[cfg(feature = "from_toml")] use toml; use crate::{CfgMap, CfgValue::*, Condition::*, Checkable}; #[test] #[cfg(feature = "from_json")] fn from_json_test() { let json = serde_json::json! ({ "string": "string", "integer": 12, "float": 1.2, "null": null, "sub": { "integer": 20 }, "array": [10,20], }); let cmap = CfgMap::from_json(json); assert!(cmap.get("string").check_that(IsExactlyStr("string".into()))); assert!(cmap.get("integer").check_that(IsExactlyInt(12))); assert!(cmap.get("float").check_that(IsExactlyFloat(1.2))); assert!(cmap.get("null").check_that(IsNull)); assert!(cmap.get("sub/integer").check_that(IsExactlyInt(20))); assert!(cmap.get("array").check_that(IsListWith(Box::new(IsInt)) & IsListWithLength(2))); } #[test] #[cfg(feature = "from_toml")] fn from_toml_test() { let toml = toml::toml! { [package] name = "cfgmap" version = "0.1.0" authors = ["Andrea Jenkins <mctech26@gmail.com>"] [lib] name = "cfgmap" path = "src/cfgmap.rs" [dependencies] serde_json = { version = "1.0.48", optional = true } toml = { version = "0.5.6", optional = true } [other] date = 2020-02-29 float = 1.2 int = 3 internal.more = "hello" [[person]] name = "a" [[person]] name = "b" }; let cmap = CfgMap::from_toml(toml); assert!(cmap.get("package/name").check_that(IsExactlyStr("cfgmap".into()))); assert!(cmap.get("package/version").check_that(IsExactlyStr("0.1.0".into()))); assert!(cmap.get("package/authors").check_that(IsExactlyList(vec![Str("Andrea Jenkins <mctech26@gmail.com>".into())]))); assert!(cmap.get("lib/name").check_that(IsExactlyStr("cfgmap".into()))); assert!(cmap.get("lib/path").check_that(IsExactlyStr("src/cfgmap.rs".into()))); assert!(cmap.get("dependencies/serde_json/version").check_that(IsExactlyStr("1.0.48".into()))); assert!(cmap.get("dependencies/serde_json/optional").check_that(IsTrue)); assert!(cmap.get("dependencies/toml/version").check_that(IsExactlyStr("0.5.6".into()))); assert!(cmap.get("dependencies/toml/optional").check_that(IsTrue)); assert!(cmap.get("other/date").check_that(IsDatetime)); assert!(cmap.get("other/float").check_that(IsExactlyFloat(1.2))); assert!(cmap.get("other/int").check_that(IsExactlyInt(3))); assert!(cmap.get("other/internal/more").check_that(IsExactlyStr("hello".into()))); assert!(cmap.get("person").check_that(IsListWith(Box::new(IsMap)))); assert!(cmap.get("person/0/name").check_that(IsExactlyStr("a".into()))); assert!(cmap.get("person/1/name").check_that(IsExactlyStr("b".into()))); } }