Skip to content

Commit dd661d3

Browse files
committed
Put metadata-related code into submodule
1 parent 520e6b6 commit dd661d3

2 files changed

Lines changed: 77 additions & 69 deletions

File tree

src/output.rs

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ use std::fs;
1414
use std::fs::File;
1515
use std::path::{Path, PathBuf};
1616

17+
pub mod metadata;
18+
use metadata::write_metadata;
19+
1720
/// The root folder in which model-specific output folders will be created
1821
const OUTPUT_DIRECTORY_ROOT: &str = "muse2_results";
1922

20-
/// The output file name for metadata
21-
const METADATA_FILE_NAME: &str = "metadata.toml";
22-
2323
/// The output file name for commodity flows
2424
const COMMODITY_FLOWS_FILE_NAME: &str = "commodity_flows.csv";
2525

@@ -66,72 +66,6 @@ pub fn create_output_directory(output_dir: &Path) -> Result<()> {
6666
Ok(())
6767
}
6868

69-
#[derive(Serialize, Default)]
70-
struct Metadata<'a> {
71-
program: ProgramMetadata<'a>,
72-
}
73-
74-
/// Information about the version and build of MUSE
75-
#[derive(Serialize)]
76-
struct ProgramMetadata<'a> {
77-
/// The program name
78-
name: &'a str,
79-
/// The program version as specified in Cargo.toml
80-
version: &'a str,
81-
/// The target architecture for the build (e.g. x86_64-unknown-linux-gnu)
82-
target: &'a str,
83-
/// Whether it is a debug build
84-
is_debug: bool,
85-
/// The version of rustc used to compile MUSE
86-
rustc_version: &'a str,
87-
/// When MUSE was built
88-
build_time_utc: &'a str,
89-
/// The git commit hash for the version of MUSE (if known)
90-
git_commit_hash: String,
91-
}
92-
93-
/// Information about the program build via `built` crate
94-
mod built_info {
95-
// The file has been placed there by the build script.
96-
include!(concat!(env!("OUT_DIR"), "/built.rs"));
97-
}
98-
99-
/// Get information about program version from git
100-
fn get_git_hash() -> String {
101-
let Some(hash) = built_info::GIT_COMMIT_HASH_SHORT else {
102-
return "unknown".into();
103-
};
104-
105-
if built_info::GIT_DIRTY == Some(true) {
106-
format!("{hash}-dirty")
107-
} else {
108-
hash.into()
109-
}
110-
}
111-
112-
impl Default for ProgramMetadata<'_> {
113-
fn default() -> Self {
114-
Self {
115-
name: built_info::PKG_NAME,
116-
version: built_info::PKG_VERSION,
117-
target: built_info::TARGET,
118-
is_debug: built_info::DEBUG,
119-
rustc_version: built_info::RUSTC_VERSION,
120-
build_time_utc: built_info::BUILT_TIME_UTC,
121-
git_commit_hash: get_git_hash(),
122-
}
123-
}
124-
}
125-
126-
/// Write metadata to the specified output path in TOML format
127-
fn write_metadata(output_path: &Path) -> Result<()> {
128-
let metadata = Metadata::default();
129-
let file_path = output_path.join(METADATA_FILE_NAME);
130-
fs::write(&file_path, toml::to_string(&metadata)?)?;
131-
132-
Ok(())
133-
}
134-
13569
/// Used to represent assets in assets output CSV file and other output files.
13670
///
13771
/// NB: It may be better to represent assets in these other files with IDs instead, see

src/output/metadata.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//! Code for writing metadata to file
2+
use anyhow::Result;
3+
use serde::Serialize;
4+
use std::fs;
5+
use std::path::Path;
6+
7+
/// The output file name for metadata
8+
const METADATA_FILE_NAME: &str = "metadata.toml";
9+
10+
/// Information about the program build via `built` crate
11+
mod built_info {
12+
// The file has been placed there by the build script.
13+
include!(concat!(env!("OUT_DIR"), "/built.rs"));
14+
}
15+
16+
/// Get information about program version from git
17+
fn get_git_hash() -> String {
18+
let Some(hash) = built_info::GIT_COMMIT_HASH_SHORT else {
19+
return "unknown".into();
20+
};
21+
22+
if built_info::GIT_DIRTY == Some(true) {
23+
format!("{hash}-dirty")
24+
} else {
25+
hash.into()
26+
}
27+
}
28+
29+
#[derive(Serialize, Default)]
30+
struct Metadata<'a> {
31+
program: ProgramMetadata<'a>,
32+
}
33+
34+
/// Information about the version and build of MUSE
35+
#[derive(Serialize)]
36+
struct ProgramMetadata<'a> {
37+
/// The program name
38+
name: &'a str,
39+
/// The program version as specified in Cargo.toml
40+
version: &'a str,
41+
/// The target architecture for the build (e.g. x86_64-unknown-linux-gnu)
42+
target: &'a str,
43+
/// Whether it is a debug build
44+
is_debug: bool,
45+
/// The version of rustc used to compile MUSE
46+
rustc_version: &'a str,
47+
/// When MUSE was built
48+
build_time_utc: &'a str,
49+
/// The git commit hash for the version of MUSE (if known)
50+
git_commit_hash: String,
51+
}
52+
53+
impl Default for ProgramMetadata<'_> {
54+
fn default() -> Self {
55+
Self {
56+
name: built_info::PKG_NAME,
57+
version: built_info::PKG_VERSION,
58+
target: built_info::TARGET,
59+
is_debug: built_info::DEBUG,
60+
rustc_version: built_info::RUSTC_VERSION,
61+
build_time_utc: built_info::BUILT_TIME_UTC,
62+
git_commit_hash: get_git_hash(),
63+
}
64+
}
65+
}
66+
67+
/// Write metadata to the specified output path in TOML format
68+
pub fn write_metadata(output_path: &Path) -> Result<()> {
69+
let metadata = Metadata::default();
70+
let file_path = output_path.join(METADATA_FILE_NAME);
71+
fs::write(&file_path, toml::to_string(&metadata)?)?;
72+
73+
Ok(())
74+
}

0 commit comments

Comments
 (0)