Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support loading a TOML configuration #229

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 32 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ serde_json = "^1.0"
sled = "0.34.7"
temp-dir = "0.1.12"
thiserror = "^1.0.37"
toml = "^0.8.12"
tracing = "~0.1.36"
tracing-appender = "~0.2.2"
tracing-subscriber = "0.3.16"
Expand Down
45 changes: 45 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
default_profile = "default"

[profiles.default]
user_id = "@user:matrix.org"
url = "https://matrix.org"

[settings]
default_room = "#iamb-users:0x.badd.cafe"
log_level = "warn"
message_shortcode_display = false
open_command = ["my-open", "--file"]
reaction_display = true
reaction_shortcode_display = false
read_receipt_display = true
read_receipt_send = true
request_timeout = 10000
typing_notice_display = true
typing_notice_send = true
user_gutter_width = 30
username_display = "username"

[settings.image_preview]
protocol.type = "sixel"
size = { "width" = 66, "height" = 10 }

[settings.sort]
rooms = ["favorite", "lowpriority", "unread", "name"]
members = ["power", "id"]

[settings.users]
"@user:matrix.org" = { "name" = "John Doe", "color" = "magenta" }

[layout]
style = "restore"

[macros.insert]
"jj" = "<Esc>"

[macros."normal|visual"]
"V" = "<C-W>m"

[dirs]
cache = "/home/user/.cache/iamb/"
logs = "/home/user/.local/share/iamb/logs/"
downloads = "/home/user/Downloads/"
54 changes: 0 additions & 54 deletions docs/example_config.json

This file was deleted.

55 changes: 34 additions & 21 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ pub enum ConfigError {
IO(#[from] std::io::Error),

#[error("Error loading configuration file: {0}")]
Invalid(#[from] serde_json::Error),
Invalid(#[from] toml::de::Error),

#[error("Error loading JSON configuration file: {0}")]
InvalidJSON(#[from] serde_json::Error),
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
Expand Down Expand Up @@ -717,18 +720,16 @@ pub struct IambConfig {
}

impl IambConfig {
pub fn load(config_json: &Path) -> Result<Self, ConfigError> {
if !config_json.is_file() {
usage!(
"Please create a configuration file at {}\n\n\
For more information try '--help'",
config_json.display(),
);
}
pub fn load_toml(path: &Path) -> Result<Self, ConfigError> {
let s = std::fs::read_to_string(path)?;
let config = toml::from_str(&s)?;

let file = File::open(config_json)?;
let reader = BufReader::new(file);
let config = serde_json::from_reader(reader)?;
Ok(config)
}

pub fn load_json(path: &Path) -> Result<Self, ConfigError> {
let s = std::fs::read_to_string(path)?;
let config = serde_json::from_str(&s)?;

Ok(config)
}
Expand Down Expand Up @@ -758,9 +759,22 @@ impl ApplicationSettings {
For more information try '--help'"
);
});

config_dir.push("iamb");
let mut config_json = config_dir.clone();
config_json.push("config.json");
let config_json = config_dir.join("config.json");
let config_toml = config_dir.join("config.toml");

let config = if config_toml.is_file() {
IambConfig::load_toml(config_toml.as_path())?
} else if config_json.is_file() {
IambConfig::load_json(config_json.as_path())?
} else {
usage!(
"Please create a configuration file at {}\n\n\
For more information try '--help'",
config_toml.display(),
);
};

let IambConfig {
mut profiles,
Expand All @@ -769,7 +783,7 @@ impl ApplicationSettings {
settings: global,
layout,
macros,
} = IambConfig::load(config_json.as_path())?;
} = config;

validate_profile_names(&profiles);

Expand All @@ -786,9 +800,8 @@ impl ApplicationSettings {
} else {
usage!(
"No profile specified. \
Please use -P or add \"default_profile\" to {}.\n\n\
Please use -P or add \"default_profile\" to your configuration.\n\n\
For more information try '--help'",
config_json.display()
);
};

Expand Down Expand Up @@ -1158,9 +1171,9 @@ mod tests {
}

#[test]
fn test_load_example_config_json() {
let path = PathBuf::from("docs/example_config.json");
let config = IambConfig::load(&path).expect("can load example_config.json");
fn test_load_example_config_toml() {
let path = PathBuf::from("config.example.toml");
let config = IambConfig::load_toml(&path).expect("can load example_config.toml");

let IambConfig {
profiles,
Expand All @@ -1169,7 +1182,7 @@ mod tests {
dirs,
layout,
macros,
} = config;
} = &config;

// There should be an example object for each top-level field.
assert!(!profiles.is_empty());
Expand Down