foo().pop().expected("foo length >= 1");
and if it fails, the error is something like "panicked at 'expected foo length >= 1'".[1]https://gist.github.com/edmccard/8898dd397eec0ff3595c28ada52...
let config: HashMap<String, String> = serde_json::from_reader(file)
.expect("config has invalid json");
This means that you can just do let jenkins_server = config.get("jenkins_server")
.expect("jenkins_server key not in config");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...
.beware("could not open file"); .be_wary_of("but hole")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.
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.What you're point out is that it's a large bar to ask a new comer to the language to do this because it requires a deeper understanding of the language to use.
Is it not appropriate to show that you can reduce the complexity of a program by using other features of the language?
It's not significantly different from reducing
x + x + x + x
To 4xBut yes, it does appear that the config library needs an extra wrapper that loads from files and returns configs (in a context) that does the common work for you.
Anyway, so some things that could make your script easier:
* for simple scripts I tend to use the `.expect` method if I plan on killing the program if there is an error. It's just like unwrap, but it will print out a custom error message. So you could write something like this to get a file:
(Aside: I never liked the method name `expect` for this, but is too late to do anything about that now).* next, you don't have to create a struct for serde if you don't want to. serde_derive is definitely cool and magical, but it can be too magical for one off scripts. Instead you could use serde_jaon::Value [0], which is roughly equivalent to when python's json parser would produce. * next, serde_json has a function called from from_reader [1], which you can use to parse directly from a `Read` type. So combined with Value you would get:
* Next you could get the config values out with some methods on Value: There might be some other things we could simplify. Just let us know how to help.[0]: https://docs.serde.rs/serde_json/enum.Value.html
[1] https://docs.serde.rs/serde_json/de/fn.from_reader.html