-
Notifications
You must be signed in to change notification settings - Fork 124
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
Ergonomics of optional fields #55
Comments
@ozkriff If you don't specify a font, you'll use some fallback, right? |
Correct. |
#[derive(Deserialize)]
struct Settings {
#[serde(default = "default_font")]
font: String,
}
fn default_font() -> String {
"default.ttf".to_owned()
} |
The problem in this specific case is that if there's no font in the config, I want to use embedded font: let font = if let Some(ref fontpath) = settings.font {
new_font_from_vec(fs::load(fontpath))
} else {
let data = include_bytes!("Karla-Regular.ttf");
new_font_from_vec(data.to_vec())
}; |
@ozkriff isn't it easy to workaround? set default to something like |
@kvark yep, that's what I'm going to do. Though it's still a hack and I think I would prefer some kind of "(configurable) option promotion" from RON or something. |
@kvark Do we want an option for implicit |
@torkleyy I don't see a strong for it yet. The serde defaults should cover it mostly, and help keep |
I understand how it makes sense to work around it from the Rust side of things, but it does feel really counter-intuitive to have to wrap lots of values with Some() on the scripting side. Even though the scripter should know whether a field is optional or not, I feel like it expects too much understanding of Rust. I've done something similar to with TOML in a previous project and it handled options implicitly. I don't mean to imply that you should do it just because they did, but it just felt more like the right way to do things from the scripting side of things. Could the parser check to see if an option is expected and wrap the value with Some() automatically? |
The current strategy was chosen because it is more concise. E.g. |
I would like the current concise scheme to be default, but I don't mind an option in the new pragmas. There are more clients who want it (@ozkriff).
… On Jan 13, 2018, at 12:41, Thomas Schaller ***@***.***> wrote:
The current strategy was chosen because it is more concise. E.g. None could be None in Rust or Some(None).
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
I already started working on a solution, but couldn't finish it yet. |
@kvark @torkleyy It just feels like the default behavior is a little inconsistent:
Shouldn't it see that I correctly supplied a |
You can only skip the field if you added |
If missing fields become None, and existing fields become Some(val), that would mean the format won't be self-describing any longer, right? |
If I have a struct like
I can omit the field in
.ron
file to getNone
:But I'm forced to write
Some()
around the actual value which clutters up the config:Seems like an ergonomic problem to me :(
See https://gitter.im/ron-rs/ron?at=59d228bd614889d47565733d
The text was updated successfully, but these errors were encountered: