-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Change how default config values are handed in the code #132
Comments
I'm absolutely a fan of this, but I think we need to do some planning to make sure we're not locking in on something that would limit extensibility. The main reason I didn't decide to go for this from the get-go is because I had other ideas around where I thought configuration might be headed. The end-goal I had in mind was to have all segments also be configurable in the same way modules are. Providing a string value as a segment's configuration would set that as its "value" while providing a table would allow for very granular configuration (style, value, segment-specific configuration). Here's one such example of what I had in mind, taken from spaceship-prompt/spaceship-prompt#397: [git_status]
prefix = false
suffix = false
[git_status.added]
value = "•"
style = "yellow"
[git_status.untracked]
value = "•"
style = "blue"
[git_status.deleted]
value = "•"
style = "green"
[git_status.modified]
value = "•"
style = "red" Working with enums would certainly still be possible, but it would make it quite a bit more complex. What are your thoughts on the above configuration approach? |
Mmmm. I definitely like the idea of configuring segments as well. I think doing it with enums would be possible, but it would depend on how good the support for nested enums in Rust looks like (a quick google search doesn't give me a solid idea--seems like not many people use these things). Whether we use strings as keys or enums as keys doesn't affect the part where the config framework can check config files and provide default values for us...so I guess the big question is whether the ergonomics of enums are good enough to allow us to do this without too much trouble (and of course questions about design flexibility of switching to this system) |
We can create a folder named // src/configs/character.rs
pub struct /* or enum */ CharacterConfig { ... };
impl ModuleConfig<CharacterConfig> for CharacterConfig {...};
pub const DEFAULT_CHARACTER_CONFIG: CharacterConfig = CharacterConfig {...}; where // Somewhere
pub trait ModuleConfig {
fn load_config(&self, &toml::Value::Table) -> Self;
} or we can use some macros like This should avoid accessing invalid options, simplify reading options and even make it possible to generate a default |
However, already from this small example we can see that boilerplate is introduced and while it reduces some problems, what you would really want is to directly convert to a data class / struct like this
This layer should follow directly after reading the user configuration, which may only set a subset of the To make sure that the pattern is followed by all modules, we could setup a trait and every module is an implementation of that trait. |
The most common way to handle default values for config options seems to be to attempt to look up the key in the toml, then assign a default value if that fails, something like
config.get_as_type(key).unwrap_or(default_value)
. There are several potential problems I can see with this approach:use_colour
)A possible alternative would be to create a config manager class that holds
enums
of all the config options, along with their default values. You would then be able to query it by asking it for the value of the enum, e.g. we would replacewith
We could even start to encode things like implied arguments (if one value is specified in the config, then multiple other values are implicitly set to certain values), or incompatible arguments across modules (if doing A in one module, cannot do B in another) into this config manager, and give starship the ability to check the toml for correctness.
Advantages:
Disadvantages:
Would this seem like a net positive for us, or does it seem closer to neutral for what we have at the moment?
The text was updated successfully, but these errors were encountered: