Commit ed1b88c197

ed1b88c19774115a613f1f2ac3e145cc357a41ed

parent: 99996e275b

Unregistered key

Ulyssa <git@ulyssa.dev> · 2024-03-26 04:30 UTC
committer: <noreply@github.com>

Support loading a TOML configuration (#229)

Layout: unified · split

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