Commit ed1b88c197
Unregistered key
Layout: unified · split
Cargo.lock +32 −12
| @@ -2004,6 +2004,7 @@ dependencies = [ | ||
| 2004 | 2004 | "temp-dir", |
| 2005 | 2005 | "thiserror", |
| 2006 | 2006 | "tokio", |
| 2007 | "toml", | |
| 2007 | 2008 | "tracing", |
| 2008 | 2009 | "tracing-appender", |
| 2009 | 2010 | "tracing-subscriber", |
| @@ -2527,7 +2528,7 @@ version = "0.1.1" | ||
| 2527 | 2528 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 2528 | 2529 | checksum = "93779aa78d39c2fe34746287b10a866192cf8af1b81767fff76bd64099acc0f5" |
| 2529 | 2530 | dependencies = [ |
| 2530 | "proc-macro-crate 2.0.2", | |
| 2531 | "proc-macro-crate 2.0.0", | |
| 2531 | 2532 | "proc-macro-error", |
| 2532 | 2533 | "proc-macro2", |
| 2533 | 2534 | "quote", |
| @@ -3416,11 +3417,10 @@ dependencies = [ | ||
| 3416 | 3417 | |
| 3417 | 3418 | [[package]] |
| 3418 | 3419 | name = "proc-macro-crate" |
| 3419 | version = "2.0.2" | |
| 3420 | version = "2.0.0" | |
| 3420 | 3421 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 3421 | checksum = "b00f26d3400549137f92511a46ac1cd8ce37cb5598a96d382381458b992a5d24" | |
| 3422 | checksum = "7e8366a6159044a37876a2b9817124296703c586a5c92e2c53751fa06d8d43e8" | |
| 3422 | 3423 | dependencies = [ |
| 3423 | "toml_datetime", | |
| 3424 | 3424 | "toml_edit 0.20.2", |
| 3425 | 3425 | ] |
| 3426 | 3426 | |
| @@ -3925,7 +3925,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 3925 | 3925 | checksum = "0280534a4b3e34416f883285fac4f9c408cd0b737890ae66f3e7a7056d14be80" |
| 3926 | 3926 | dependencies = [ |
| 3927 | 3927 | "once_cell", |
| 3928 | "proc-macro-crate 2.0.2", | |
| 3928 | "proc-macro-crate 2.0.0", | |
| 3929 | 3929 | "proc-macro2", |
| 3930 | 3930 | "quote", |
| 3931 | 3931 | "ruma-identifiers-validation", |
| @@ -4743,21 +4743,21 @@ dependencies = [ | ||
| 4743 | 4743 | |
| 4744 | 4744 | [[package]] |
| 4745 | 4745 | name = "toml" |
| 4746 | version = "0.8.2" | |
| 4746 | version = "0.8.12" | |
| 4747 | 4747 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 4748 | checksum = "185d8ab0dfbb35cf1399a6344d8484209c088f75f8f68230da55d48d95d43e3d" | |
| 4748 | checksum = "e9dd1545e8208b4a5af1aa9bbd0b4cf7e9ea08fabc5d0a5c67fcaafa17433aa3" | |
| 4749 | 4749 | dependencies = [ |
| 4750 | 4750 | "serde", |
| 4751 | 4751 | "serde_spanned", |
| 4752 | 4752 | "toml_datetime", |
| 4753 | "toml_edit 0.20.2", | |
| 4753 | "toml_edit 0.22.9", | |
| 4754 | 4754 | ] |
| 4755 | 4755 | |
| 4756 | 4756 | [[package]] |
| 4757 | 4757 | name = "toml_datetime" |
| 4758 | version = "0.6.3" | |
| 4758 | version = "0.6.5" | |
| 4759 | 4759 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 4760 | checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" | |
| 4760 | checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" | |
| 4761 | 4761 | dependencies = [ |
| 4762 | 4762 | "serde", |
| 4763 | 4763 | ] |
| @@ -4770,7 +4770,7 @@ checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" | ||
| 4770 | 4770 | dependencies = [ |
| 4771 | 4771 | "indexmap 2.0.2", |
| 4772 | 4772 | "toml_datetime", |
| 4773 | "winnow", | |
| 4773 | "winnow 0.5.15", | |
| 4774 | 4774 | ] |
| 4775 | 4775 | |
| 4776 | 4776 | [[package]] |
| @@ -4778,12 +4778,23 @@ name = "toml_edit" | ||
| 4778 | 4778 | version = "0.20.2" |
| 4779 | 4779 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 4780 | 4780 | checksum = "396e4d48bbb2b7554c944bde63101b5ae446cff6ec4a24227428f15eb72ef338" |
| 4781 | dependencies = [ | |
| 4782 | "indexmap 2.0.2", | |
| 4783 | "toml_datetime", | |
| 4784 | "winnow 0.5.15", | |
| 4785 | ] | |
| 4786 | ||
| 4787 | [[package]] | |
| 4788 | name = "toml_edit" | |
| 4789 | version = "0.22.9" | |
| 4790 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 4791 | checksum = "8e40bb779c5187258fd7aad0eb68cb8706a0a81fa712fbea808ab43c4b8374c4" | |
| 4781 | 4792 | dependencies = [ |
| 4782 | 4793 | "indexmap 2.0.2", |
| 4783 | 4794 | "serde", |
| 4784 | 4795 | "serde_spanned", |
| 4785 | 4796 | "toml_datetime", |
| 4786 | "winnow", | |
| 4797 | "winnow 0.6.5", | |
| 4787 | 4798 | ] |
| 4788 | 4799 | |
| 4789 | 4800 | [[package]] |
| @@ -5530,6 +5541,15 @@ dependencies = [ | ||
| 5530 | 5541 | "memchr", |
| 5531 | 5542 | ] |
| 5532 | 5543 | |
| 5544 | [[package]] | |
| 5545 | name = "winnow" | |
| 5546 | version = "0.6.5" | |
| 5547 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 5548 | checksum = "dffa400e67ed5a4dd237983829e66475f0a4a26938c4b04c21baede6262215b8" | |
| 5549 | dependencies = [ | |
| 5550 | "memchr", | |
| 5551 | ] | |
| 5552 | ||
| 5533 | 5553 | [[package]] |
| 5534 | 5554 | name = "winreg" |
| 5535 | 5555 | version = "0.50.0" |
Cargo.toml +1
| @@ -57,6 +57,7 @@ serde_json = "^1.0" | ||
| 57 | 57 | sled = "0.34.7" |
| 58 | 58 | temp-dir = "0.1.12" |
| 59 | 59 | thiserror = "^1.0.37" |
| 60 | toml = "^0.8.12" | |
| 60 | 61 | tracing = "~0.1.36" |
| 61 | 62 | tracing-appender = "~0.2.2" |
| 62 | 63 | tracing-subscriber = "0.3.16" |
config.example.toml added +45
| @@ -0,0 +1,45 @@ | ||
| 1 | default_profile = "default" | |
| 2 | ||
| 3 | [profiles.default] | |
| 4 | user_id = "@user:matrix.org" | |
| 5 | url = "https://matrix.org" | |
| 6 | ||
| 7 | [settings] | |
| 8 | default_room = "#iamb-users:0x.badd.cafe" | |
| 9 | log_level = "warn" | |
| 10 | message_shortcode_display = false | |
| 11 | open_command = ["my-open", "--file"] | |
| 12 | reaction_display = true | |
| 13 | reaction_shortcode_display = false | |
| 14 | read_receipt_display = true | |
| 15 | read_receipt_send = true | |
| 16 | request_timeout = 10000 | |
| 17 | typing_notice_display = true | |
| 18 | typing_notice_send = true | |
| 19 | user_gutter_width = 30 | |
| 20 | username_display = "username" | |
| 21 | ||
| 22 | [settings.image_preview] | |
| 23 | protocol.type = "sixel" | |
| 24 | size = { "width" = 66, "height" = 10 } | |
| 25 | ||
| 26 | [settings.sort] | |
| 27 | rooms = ["favorite", "lowpriority", "unread", "name"] | |
| 28 | members = ["power", "id"] | |
| 29 | ||
| 30 | [settings.users] | |
| 31 | "@user:matrix.org" = { "name" = "John Doe", "color" = "magenta" } | |
| 32 | ||
| 33 | [layout] | |
| 34 | style = "restore" | |
| 35 | ||
| 36 | [macros.insert] | |
| 37 | "jj" = "<Esc>" | |
| 38 | ||
| 39 | [macros."normal|visual"] | |
| 40 | "V" = "<C-W>m" | |
| 41 | ||
| 42 | [dirs] | |
| 43 | cache = "/home/user/.cache/iamb/" | |
| 44 | logs = "/home/user/.local/share/iamb/logs/" | |
| 45 | downloads = "/home/user/Downloads/" | |
docs/example_config.json deleted −54
| @@ -1,54 +0,0 @@ | ||
| 1 | { | |
| 2 | "default_profile": "default", | |
| 3 | "profiles": { | |
| 4 | "default": { | |
| 5 | "user_id": "@user:matrix.org", | |
| 6 | "url": "https://matrix.org", | |
| 7 | "settings": {}, | |
| 8 | "dirs": {} | |
| 9 | } | |
| 10 | }, | |
| 11 | "settings": { | |
| 12 | "log_level": "warn", | |
| 13 | "message_shortcode_display": false, | |
| 14 | "reaction_display": true, | |
| 15 | "reaction_shortcode_display": false, | |
| 16 | "read_receipt_send": true, | |
| 17 | "read_receipt_display": true, | |
| 18 | "request_timeout": 10000, | |
| 19 | "typing_notice_send": true, | |
| 20 | "typing_notice_display": true, | |
| 21 | "users": { | |
| 22 | "@user:matrix.org": { | |
| 23 | "name": "John Doe", | |
| 24 | "color": "magenta" | |
| 25 | } | |
| 26 | }, | |
| 27 | "default_room": "#iamb-users:0x.badd.cafe", | |
| 28 | "image_preview": { | |
| 29 | "protocol": { | |
| 30 | "type": "sixel" | |
| 31 | }, | |
| 32 | "size": { | |
| 33 | "width": 66, | |
| 34 | "height": 10 | |
| 35 | } | |
| 36 | } | |
| 37 | }, | |
| 38 | "layout": { | |
| 39 | "style": "restore" | |
| 40 | }, | |
| 41 | "macros": { | |
| 42 | "n|v": { | |
| 43 | "V": "<C-W>m" | |
| 44 | }, | |
| 45 | "i": { | |
| 46 | "jj": "<Esc>" | |
| 47 | } | |
| 48 | }, | |
| 49 | "dirs": { | |
| 50 | "cache": "/home/user/.cache/iamb/", | |
| 51 | "logs": "/home/user/.local/share/iamb/logs/", | |
| 52 | "downloads": "/home/user/Downloads/" | |
| 53 | } | |
| 54 | } | |
src/config.rs +34 −21
| @@ -141,7 +141,10 @@ pub enum ConfigError { | ||
| 141 | 141 | IO(#[from] std::io::Error), |
| 142 | 142 | |
| 143 | 143 | #[error("Error loading configuration file: {0}")] |
| 144 | Invalid(#[from] serde_json::Error), | |
| 144 | Invalid(#[from] toml::de::Error), | |
| 145 | ||
| 146 | #[error("Error loading JSON configuration file: {0}")] | |
| 147 | InvalidJSON(#[from] serde_json::Error), | |
| 145 | 148 | } |
| 146 | 149 | |
| 147 | 150 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| @@ -717,18 +720,16 @@ pub struct IambConfig { | ||
| 717 | 720 | } |
| 718 | 721 | |
| 719 | 722 | impl IambConfig { |
| 720 | pub fn load(config_json: &Path) -> Result<Self, ConfigError> { | |
| 721 | if !config_json.is_file() { | |
| 722 | usage!( | |
| 723 | "Please create a configuration file at {}\n\n\ | |
| 724 | For more information try '--help'", | |
| 725 | config_json.display(), | |
| 726 | ); | |
| 727 | } | |
| 723 | pub fn load_toml(path: &Path) -> Result<Self, ConfigError> { | |
| 724 | let s = std::fs::read_to_string(path)?; | |
| 725 | let config = toml::from_str(&s)?; | |
| 728 | 726 | |
| 729 | let file = File::open(config_json)?; | |
| 730 | let reader = BufReader::new(file); | |
| 731 | let config = serde_json::from_reader(reader)?; | |
| 727 | Ok(config) | |
| 728 | } | |
| 729 | ||
| 730 | pub fn load_json(path: &Path) -> Result<Self, ConfigError> { | |
| 731 | let s = std::fs::read_to_string(path)?; | |
| 732 | let config = serde_json::from_str(&s)?; | |
| 732 | 733 | |
| 733 | 734 | Ok(config) |
| 734 | 735 | } |
| @@ -758,9 +759,22 @@ impl ApplicationSettings { | ||
| 758 | 759 | For more information try '--help'" |
| 759 | 760 | ); |
| 760 | 761 | }); |
| 762 | ||
| 761 | 763 | config_dir.push("iamb"); |
| 762 | let mut config_json = config_dir.clone(); | |
| 763 | config_json.push("config.json"); | |
| 764 | let config_json = config_dir.join("config.json"); | |
| 765 | let config_toml = config_dir.join("config.toml"); | |
| 766 | ||
| 767 | let config = if config_toml.is_file() { | |
| 768 | IambConfig::load_toml(config_toml.as_path())? | |
| 769 | } else if config_json.is_file() { | |
| 770 | IambConfig::load_json(config_json.as_path())? | |
| 771 | } else { | |
| 772 | usage!( | |
| 773 | "Please create a configuration file at {}\n\n\ | |
| 774 | For more information try '--help'", | |
| 775 | config_toml.display(), | |
| 776 | ); | |
| 777 | }; | |
| 764 | 778 | |
| 765 | 779 | let IambConfig { |
| 766 | 780 | mut profiles, |
| @@ -769,7 +783,7 @@ impl ApplicationSettings { | ||
| 769 | 783 | settings: global, |
| 770 | 784 | layout, |
| 771 | 785 | macros, |
| 772 | } = IambConfig::load(config_json.as_path())?; | |
| 786 | } = config; | |
| 773 | 787 | |
| 774 | 788 | validate_profile_names(&profiles); |
| 775 | 789 | |
| @@ -786,9 +800,8 @@ impl ApplicationSettings { | ||
| 786 | 800 | } else { |
| 787 | 801 | usage!( |
| 788 | 802 | "No profile specified. \ |
| 789 | Please use -P or add \"default_profile\" to {}.\n\n\ | |
| 803 | Please use -P or add \"default_profile\" to your configuration.\n\n\ | |
| 790 | 804 | For more information try '--help'", |
| 791 | config_json.display() | |
| 792 | 805 | ); |
| 793 | 806 | }; |
| 794 | 807 | |
| @@ -1158,9 +1171,9 @@ mod tests { | ||
| 1158 | 1171 | } |
| 1159 | 1172 | |
| 1160 | 1173 | #[test] |
| 1161 | fn test_load_example_config_json() { | |
| 1162 | let path = PathBuf::from("docs/example_config.json"); | |
| 1163 | let config = IambConfig::load(&path).expect("can load example_config.json"); | |
| 1174 | fn test_load_example_config_toml() { | |
| 1175 | let path = PathBuf::from("config.example.toml"); | |
| 1176 | let config = IambConfig::load_toml(&path).expect("can load example_config.toml"); | |
| 1164 | 1177 | |
| 1165 | 1178 | let IambConfig { |
| 1166 | 1179 | profiles, |
| @@ -1169,7 +1182,7 @@ mod tests { | ||
| 1169 | 1182 | dirs, |
| 1170 | 1183 | layout, |
| 1171 | 1184 | macros, |
| 1172 | } = config; | |
| 1185 | } = &config; | |
| 1173 | 1186 | |
| 1174 | 1187 | // There should be an example object for each top-level field. |
| 1175 | 1188 | assert!(!profiles.is_empty()); |