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 = [
20042004 "temp-dir",
20052005 "thiserror",
20062006 "tokio",
2007 "toml",
20072008 "tracing",
20082009 "tracing-appender",
20092010 "tracing-subscriber",
@@ -2527,7 +2528,7 @@ version = "0.1.1"
25272528source = "registry+https://github.com/rust-lang/crates.io-index"
25282529checksum = "93779aa78d39c2fe34746287b10a866192cf8af1b81767fff76bd64099acc0f5"
25292530dependencies = [
2530 "proc-macro-crate 2.0.2",
2531 "proc-macro-crate 2.0.0",
25312532 "proc-macro-error",
25322533 "proc-macro2",
25332534 "quote",
@@ -3416,11 +3417,10 @@ dependencies = [
34163417
34173418[[package]]
34183419name = "proc-macro-crate"
3419version = "2.0.2"
3420version = "2.0.0"
34203421source = "registry+https://github.com/rust-lang/crates.io-index"
3421checksum = "b00f26d3400549137f92511a46ac1cd8ce37cb5598a96d382381458b992a5d24"
3422checksum = "7e8366a6159044a37876a2b9817124296703c586a5c92e2c53751fa06d8d43e8"
34223423dependencies = [
3423 "toml_datetime",
34243424 "toml_edit 0.20.2",
34253425]
34263426
@@ -3925,7 +3925,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
39253925checksum = "0280534a4b3e34416f883285fac4f9c408cd0b737890ae66f3e7a7056d14be80"
39263926dependencies = [
39273927 "once_cell",
3928 "proc-macro-crate 2.0.2",
3928 "proc-macro-crate 2.0.0",
39293929 "proc-macro2",
39303930 "quote",
39313931 "ruma-identifiers-validation",
@@ -4743,21 +4743,21 @@ dependencies = [
47434743
47444744[[package]]
47454745name = "toml"
4746version = "0.8.2"
4746version = "0.8.12"
47474747source = "registry+https://github.com/rust-lang/crates.io-index"
4748checksum = "185d8ab0dfbb35cf1399a6344d8484209c088f75f8f68230da55d48d95d43e3d"
4748checksum = "e9dd1545e8208b4a5af1aa9bbd0b4cf7e9ea08fabc5d0a5c67fcaafa17433aa3"
47494749dependencies = [
47504750 "serde",
47514751 "serde_spanned",
47524752 "toml_datetime",
4753 "toml_edit 0.20.2",
4753 "toml_edit 0.22.9",
47544754]
47554755
47564756[[package]]
47574757name = "toml_datetime"
4758version = "0.6.3"
4758version = "0.6.5"
47594759source = "registry+https://github.com/rust-lang/crates.io-index"
4760checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b"
4760checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1"
47614761dependencies = [
47624762 "serde",
47634763]
@@ -4770,7 +4770,7 @@ checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421"
47704770dependencies = [
47714771 "indexmap 2.0.2",
47724772 "toml_datetime",
4773 "winnow",
4773 "winnow 0.5.15",
47744774]
47754775
47764776[[package]]
@@ -4778,12 +4778,23 @@ name = "toml_edit"
47784778version = "0.20.2"
47794779source = "registry+https://github.com/rust-lang/crates.io-index"
47804780checksum = "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"
47814792dependencies = [
47824793 "indexmap 2.0.2",
47834794 "serde",
47844795 "serde_spanned",
47854796 "toml_datetime",
4786 "winnow",
4797 "winnow 0.6.5",
47874798]
47884799
47894800[[package]]
@@ -5530,6 +5541,15 @@ dependencies = [
55305541 "memchr",
55315542]
55325543
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
55335553[[package]]
55345554name = "winreg"
55355555version = "0.50.0"
Cargo.toml +1
@@ -57,6 +57,7 @@ serde_json = "^1.0"
5757sled = "0.34.7"
5858temp-dir = "0.1.12"
5959thiserror = "^1.0.37"
60toml = "^0.8.12"
6061tracing = "~0.1.36"
6162tracing-appender = "~0.2.2"
6263tracing-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 {
141141 IO(#[from] std::io::Error),
142142
143143 #[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),
145148}
146149
147150#[derive(Clone, Debug, Eq, Hash, PartialEq)]
@@ -717,18 +720,16 @@ pub struct IambConfig {
717720}
718721
719722impl 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)?;
728726
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)?;
732733
733734 Ok(config)
734735 }
@@ -758,9 +759,22 @@ impl ApplicationSettings {
758759 For more information try '--help'"
759760 );
760761 });
762
761763 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 };
764778
765779 let IambConfig {
766780 mut profiles,
@@ -769,7 +783,7 @@ impl ApplicationSettings {
769783 settings: global,
770784 layout,
771785 macros,
772 } = IambConfig::load(config_json.as_path())?;
786 } = config;
773787
774788 validate_profile_names(&profiles);
775789
@@ -786,9 +800,8 @@ impl ApplicationSettings {
786800 } else {
787801 usage!(
788802 "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\
790804 For more information try '--help'",
791 config_json.display()
792805 );
793806 };
794807
@@ -1158,9 +1171,9 @@ mod tests {
11581171 }
11591172
11601173 #[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");
11641177
11651178 let IambConfig {
11661179 profiles,
@@ -1169,7 +1182,7 @@ mod tests {
11691182 dirs,
11701183 layout,
11711184 macros,
1172 } = config;
1185 } = &config;
11731186
11741187 // There should be an example object for each top-level field.
11751188 assert!(!profiles.is_empty());