diff options
-rw-r--r-- | src/app.cpp | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/src/app.cpp b/src/app.cpp index 8c929fe..353557e 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -59,12 +59,24 @@ result<struct layout> layout_from_json(json const &j) { return Ok(l); } +result<json> file_to_json(char const *filename) { + std::ifstream i(filename); + if (i.fail()) { + return Err<json>("Could not open config file"); + } + json j; + i >> j; + return Ok(j); +} + struct result<layouts_type> load_layout(char const *filename) { DB("loading layout from " << filename); - json jlayouts; - std::ifstream i(filename); - i >> jlayouts; + auto j = file_to_json(filename); + if (j.is_err()) { + return Err<layouts_type>(j.unwrap_err()); + } + json jlayouts = j.unwrap(); auto layouts = layouts_type(); layouts.reserve(jlayouts.size()); @@ -78,9 +90,11 @@ struct result<surface_id_to_layer_map> load_layer_ids(char const *filename) { DB("loading IDs from " << filename); - json jids; - std::ifstream i(filename); - i >> jids; + auto j = file_to_json(filename); + if (j.is_err()) { + return Err<surface_id_to_layer_map>(j.unwrap_err()); + } + json jids = j.unwrap(); return to_surface_id_to_layer_map(jids); } |