Repository: paritytech/substrate
File: utils/frame/benchmarking-cli/src/overhead/template.rs (Rust)

Code before:
	/// Header for the generated file.
	header: String,
	/// Command line arguments that were passed to the CLI.
	args: Vec<String>,
	/// Params of the executed command.
	params: OverheadParams,
	/// Stats about the benchmark result.
	stats: Stats,
	/// The resulting weight in ns.
	weight: u64,
}

impl TemplateData {
	/// Returns a new [`Self`] from the given params.
	pub(crate) fn new(
		t: BenchmarkType,
		cfg: &Configuration,
		params: &OverheadParams,
		stats: &Stats,
	) -> Result<Self> {
		let weight = params.weight.calc_weight(stats)?;
		let header = if let Some(ref path) = params.header {
			std::fs::read_to_string(path)?
		} else {
			String::new()
		};

		Ok(TemplateData {
			short_name: t.short_name().into(),
			long_name: t.long_name().into(),
			runtime_name: cfg.chain_spec.name().into(),
			version: VERSION.into(),
			date: chrono::Utc::now().format("%Y-%m-%d (Y/M/D)").to_string(),
			hostname: params.hostinfo.hostname(),
			cpuname: params.hostinfo.cpuname(),
			header,
			args: env::args().collect::<Vec<String>>(),
			params: params.clone(),
			stats: stats.clone(),
			weight,
		})
	}

	/// Fill out the `weights.hbs` HBS template with its own data.
	/// Writes the result to `path` which can be a directory or a file.
	pub fn write(&self, path: &Option<PathBuf>) -> Result<()> {
		let mut handlebars = Handlebars::new();
		// Format large integers with underscores.
		handlebars.register_helper("underscore", Box::new(UnderscoreHelper));
		// Don't HTML escape any characters.
		handlebars.register_escape_fn(|s| -> String { s.to_string() });

Review comment (suggestion):
```suggestion
		let header = params.header.map(|p| std::fs::read_to_string(p)).transpose()?.unwrap_or_default();
```

Code after:
	/// Header for the generated file.
	header: String,
	/// Command line arguments that were passed to the CLI.
	args: Vec<String>,
	/// Params of the executed command.
	params: OverheadParams,
	/// Stats about the benchmark result.
	stats: Stats,
	/// The resulting weight in ns.
	weight: u64,
}

impl TemplateData {
	/// Returns a new [`Self`] from the given params.
	pub(crate) fn new(
		t: BenchmarkType,
		cfg: &Configuration,
		params: &OverheadParams,
		stats: &Stats,
	) -> Result<Self> {
		let weight = params.weight.calc_weight(stats)?;
		let header = params
			.header
			.as_ref()
			.map(|p| std::fs::read_to_string(p))
			.transpose()?
			.unwrap_or_default();

		Ok(TemplateData {
			short_name: t.short_name().into(),
			long_name: t.long_name().into(),
			runtime_name: cfg.chain_spec.name().into(),
			version: VERSION.into(),
			date: chrono::Utc::now().format("%Y-%m-%d (Y/M/D)").to_string(),
			hostname: params.hostinfo.hostname(),
			cpuname: params.hostinfo.cpuname(),
			header,
			args: env::args().collect::<Vec<String>>(),
			params: params.clone(),
			stats: stats.clone(),
			weight,
		})
	}

	/// Fill out the `weights.hbs` HBS template with its own data.
	/// Writes the result to `path` which can be a directory or a file.
	pub fn write(&self, path: &Option<PathBuf>) -> Result<()> {
		let mut handlebars = Handlebars::new();
		// Format large integers with underscores.
		handlebars.register_helper("underscore", Box::new(UnderscoreHelper));
		// Don't HTML escape any characters.