Ultimately I'm happy we just picked something and moved on, but still mildly annoys me whenever I write it. If only we found that perfect method name way back when...
Edit: 'or_panic(msg)' would be shorter and also good.
.otherwise(panic(msg))
(although I assume a rust panic! isn't really a function call).
But isn't the way to get the default, simply to use unwrap()?
In a simple script, failing to open a configuration file for reading is likely a show stopper, and you probably want to log/print an error (no such file, wrong permission, etc).
But in, say, a paint program, you'd normally not want to panic and crash if the image file a user selected to open in a file dialog is invalid or went away between the click-to-select and the click-to-open. In such a program you'd want to handle most file errors much more defensively.
.beware("could not open file"); .be_wary_of("but hole")I could see "required" or "require" as a better name. Or even just break the "positive names" rule and go with "notOptional".
Instead of
let mut file = File::open("conf.json")
.expect("could not open file");
and let config: Value = serde::from_reader(file)
.expect("config has invalid json");
and let jenkins_server = config.get("jenkins_server")
.expect("jenkins_server key not in config")
.as_str()
.expect("jenkins_server key is not a string");
One could write let mut file = File::open("conf.json")
.expect("Need to be able to open file `conf.json'.");
and let config: Value = serde::from_reader(file)
.expect("The file `conf.json' must contain valid JSON.");
and let jenkins_server = config.get("jenkins_server")
.expect("The config must have a key named `jenkins_server'.")
.as_str()
.expect("The config value of `jenkins_server' must be a string.");
Something like that.