Skip to content

Commit d02a129

Browse files
committed
Tidy-ups
1 parent bf934db commit d02a129

3 files changed

Lines changed: 26 additions & 31 deletions

File tree

src/cli.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct Cli {
2727
markdown_help: bool,
2828
}
2929

30-
/// Options for the run command
30+
/// Options for the `run` command
3131
#[derive(Args)]
3232
pub struct RunOpts {
3333
/// Directory for output files
@@ -41,6 +41,17 @@ pub struct RunOpts {
4141
pub debug_model: bool,
4242
}
4343

44+
/// Options for the `graph` command
45+
#[derive(Args)]
46+
pub struct GraphOpts {
47+
/// Directory for graph files
48+
#[arg(short, long)]
49+
pub output_dir: Option<PathBuf>,
50+
/// Whether to overwrite the output directory if it already exists
51+
#[arg(long)]
52+
pub overwrite: bool,
53+
}
54+
4455
/// The available commands.
4556
#[derive(Subcommand)]
4657
enum Commands {
@@ -64,12 +75,12 @@ enum Commands {
6475
model_dir: PathBuf,
6576
},
6677
/// Build and output commodity flow graphs for a model.
67-
BuildCommodityGraphs {
78+
Graph {
6879
/// The path to the model directory.
6980
model_dir: PathBuf,
70-
/// Other options (uses the same settings as the `run` command)
81+
/// Other options
7182
#[command(flatten)]
72-
opts: RunOpts,
83+
opts: GraphOpts,
7384
},
7485
/// Manage settings file.
7586
Settings {
@@ -86,9 +97,7 @@ impl Commands {
8697
Self::Run { model_dir, opts } => handle_run_command(&model_dir, &opts, None),
8798
Self::Example { subcommand } => subcommand.execute(),
8899
Self::Validate { model_dir } => handle_validate_command(&model_dir, None),
89-
Self::BuildCommodityGraphs { model_dir, opts } => {
90-
handle_build_commodity_graphs_command(&model_dir, &opts, None)
91-
}
100+
Self::Graph { model_dir, opts } => handle_graph_command(&model_dir, &opts, None),
92101
Self::Settings { subcommand } => subcommand.execute(),
93102
}
94103
}
@@ -191,10 +200,10 @@ pub fn handle_validate_command(model_path: &Path, settings: Option<Settings>) ->
191200
Ok(())
192201
}
193202

194-
/// Handle the `build-commodity-graphs` command.
195-
pub fn handle_build_commodity_graphs_command(
203+
/// Handle the `graph` command.
204+
pub fn handle_graph_command(
196205
model_path: &Path,
197-
opts: &RunOpts,
206+
opts: &GraphOpts,
198207
settings: Option<Settings>,
199208
) -> Result<()> {
200209
// Load program settings, if not provided
@@ -205,9 +214,6 @@ pub fn handle_build_commodity_graphs_command(
205214
};
206215

207216
// These settings can be overridden by command-line arguments
208-
if opts.debug_model {
209-
settings.debug_model = true;
210-
}
211217
if opts.overwrite {
212218
settings.overwrite = true;
213219
}
@@ -224,24 +230,23 @@ pub fn handle_build_commodity_graphs_command(
224230
let overwrite =
225231
create_output_directory(output_path, settings.overwrite).with_context(|| {
226232
format!(
227-
"Failed to create commodity flow graphs directory: {}",
233+
"Failed to create graphs directory: {}",
228234
output_path.display()
229235
)
230236
})?;
231237

232-
// Initialise program logger (we won't save log files when running this command
238+
// Initialise program logger (we won't save log files when running this command)
233239
log::init(&settings.log_level, None).context("Failed to initialise logging.")?;
234240

235241
// NB: We have to wait until the logger is initialised to display this warning
236242
if overwrite {
237-
warn!("Commodity flow graphs directory will be overwritten");
243+
warn!("Graphs directory will be overwritten");
238244
}
239245

240246
// Load commodity flow graphs and save to file
241-
let commodity_graphs =
242-
load_commodity_graphs(model_path).context("Failed to build commodity flow graphs.")?;
247+
let commodity_graphs = load_commodity_graphs(model_path).context("Failed to build graphs.")?;
243248
save_commodity_graphs_for_model(&commodity_graphs, output_path)?;
244-
info!("Commodity flow graphs saved to file");
249+
info!("Graphs saved to file");
245250

246251
Ok(())
247252
}

src/input.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,8 @@ pub fn load_model<P: AsRef<Path>>(model_dir: P) -> Result<(Model, AssetPool)> {
243243

244244
/// Load commodity flow graphs for a model.
245245
///
246-
/// This begins by reading time slice, region, year, commodity and process data which is necessary
247-
/// to build the graphs. Other model data (agents and assets) that is not necessary for graph
248-
/// building is not read.
249-
///
250-
/// Once data is loaded (and assuming the validation checks for this data pass), this will create a
251-
/// graph of commodity flows for each region and year, where nodes are commodities and edges are
252-
/// processes.
246+
/// Loads necessary input data and creates a graph of commodity flows for each region and year,
247+
/// where nodes are commodities and edges are processes.
253248
///
254249
/// Graphs validation is NOT performed. This ensures that graphs can be generated even when
255250
/// validation would fail, which may be helpful for debugging.

src/output.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,14 @@ pub fn get_output_dir(model_dir: &Path) -> Result<PathBuf> {
7171

7272
/// Get the default output directory for commodity flow graphs for the model
7373
pub fn get_graphs_dir(model_dir: &Path) -> Result<PathBuf> {
74-
// Get the model name from the dir path. This ends up being convoluted because we need to check
75-
// for all possible errors. Ugh.
7674
let model_dir = model_dir
7775
.canonicalize() // canonicalise in case the user has specified "."
7876
.context("Could not resolve path to model")?;
79-
8077
let model_name = model_dir
8178
.file_name()
8279
.context("Model cannot be in root folder")?
8380
.to_str()
8481
.context("Invalid chars in model dir name")?;
85-
86-
// Construct path
8782
Ok([GRAPHS_DIRECTORY_ROOT, model_name].iter().collect())
8883
}
8984

0 commit comments

Comments
 (0)