11//! The command line interface for the simulation.
2+ use crate :: graph:: save_commodity_graphs_for_model;
23use crate :: input:: load_model;
34use crate :: log;
45use crate :: output:: { create_output_directory, get_output_dir} ;
@@ -62,6 +63,13 @@ enum Commands {
6263 /// The path to the model directory.
6364 model_dir : PathBuf ,
6465 } ,
66+ /// Build and output commodity flow graphs for a model.
67+ BuildCommodityGraphs {
68+ /// The path to the model directory.
69+ model_dir : PathBuf ,
70+ /// Output path
71+ output_dir : PathBuf ,
72+ } ,
6573 /// Manage settings file.
6674 Settings {
6775 /// The subcommands for managing the settings file.
@@ -77,6 +85,10 @@ impl Commands {
7785 Self :: Run { model_dir, opts } => handle_run_command ( & model_dir, & opts, None ) ,
7886 Self :: Example { subcommand } => subcommand. execute ( ) ,
7987 Self :: Validate { model_dir } => handle_validate_command ( & model_dir, None ) ,
88+ Self :: BuildCommodityGraphs {
89+ model_dir,
90+ output_dir,
91+ } => handle_build_commodity_graphs_command ( & model_dir, & output_dir, None ) ,
8092 Self :: Settings { subcommand } => subcommand. execute ( ) ,
8193 }
8294 }
@@ -178,3 +190,30 @@ pub fn handle_validate_command(model_path: &Path, settings: Option<Settings>) ->
178190
179191 Ok ( ( ) )
180192}
193+
194+ /// Handle the `build-commodity-graphs` command.
195+ pub fn handle_build_commodity_graphs_command (
196+ model_path : & Path ,
197+ output_path : & Path ,
198+ settings : Option < Settings > ,
199+ ) -> Result < ( ) > {
200+ // Load program settings, if not provided
201+ let settings = if let Some ( settings) = settings {
202+ settings
203+ } else {
204+ Settings :: load ( ) . context ( "Failed to load settings." ) ?
205+ } ;
206+
207+ // Initialise program logger (we won't save log files when running the validate command)
208+ log:: init ( & settings. log_level , None ) . context ( "Failed to initialise logging." ) ?;
209+
210+ // Load/validate the model
211+ let ( model, _) = load_model ( model_path) . context ( "Failed to load model." ) ?;
212+ info ! ( "Loaded model from {}" , model_path. display( ) ) ;
213+
214+ // Save commodity flow graphs to file
215+ save_commodity_graphs_for_model ( & model. commodity_graphs , output_path) ?;
216+ info ! ( "Commodity flow graphs saved to file" ) ;
217+
218+ Ok ( ( ) )
219+ }
0 commit comments