Repository: paritytech/substrate
File: utils/frame/generate-bags/src/lib.rs (Rust)

Code before:
	use frame_support::traits::CurrencyToVote;

	T::CurrencyToVote::to_vote(
		minimum_balance
			.try_into()
			.map_err(|_| "failed to convert minimum_balance to type Balance")
			.unwrap(),
		total_issuance
			.try_into()
			.map_err(|_| "failed to convert total_issuance to type Balance")
			.unwrap(),
	)
}

/// Return the path to a header file used in this repository if is exists.
///
/// Just searches the git working directory root for files matching certain patterns; it's
/// pretty naive.
fn path_to_header_file() -> Option<PathBuf> {
	let mut workdir: &Path = &std::env::current_dir().ok()?;
	while !workdir.join(".git").exists() {
		if let Some(parent) = workdir.parent() {
			workdir = parent;
		} else {
			return None
		}
	}

	for file_name in &["HEADER-APACHE2", "HEADER-GPL3", "HEADER", "file_header.txt"] {
		let path = workdir.join(file_name);
		if path.exists() {
			return Some(path)
		}
	}
	None
}

/// Create an underscore formatter: a formatter which inserts `_` every 3 digits of a number.
fn underscore_formatter() -> num_format::CustomFormat {
	num_format::CustomFormat::builder()
		.grouping(num_format::Grouping::Standard)
		.separator("_")
		.build()
		.expect("format described here meets all constraints")
}

/// Compute the constant ratio for the thresholds.
///
/// This ratio ensures that each bag, with the possible exceptions of certain small ones and the
/// final one, is a constant multiple of the previous, while fully occupying the `VoteWeight`
/// space.

Review comment (suggestion):
```suggestion
		workdir = workdir.parent()?;
```

Code after:
	use frame_support::traits::CurrencyToVote;

	T::CurrencyToVote::to_vote(
		minimum_balance
			.try_into()
			.map_err(|_| "failed to convert minimum_balance to type Balance")
			.unwrap(),
		total_issuance
			.try_into()
			.map_err(|_| "failed to convert total_issuance to type Balance")
			.unwrap(),
	)
}

/// Return the path to a header file used in this repository if is exists.
///
/// Just searches the git working directory root for files matching certain patterns; it's
/// pretty naive.
fn path_to_header_file() -> Option<PathBuf> {
	let mut workdir: &Path = &std::env::current_dir().ok()?;
	while !workdir.join(".git").exists() {
		workdir = workdir.parent()?;
	}

	for file_name in &["HEADER-APACHE2", "HEADER-GPL3", "HEADER", "file_header.txt"] {
		let path = workdir.join(file_name);
		if path.exists() {
			return Some(path)
		}
	}
	None
}

/// Create an underscore formatter: a formatter which inserts `_` every 3 digits of a number.
fn underscore_formatter() -> num_format::CustomFormat {
	num_format::CustomFormat::builder()
		.grouping(num_format::Grouping::Standard)
		.separator("_")
		.build()
		.expect("format described here meets all constraints")
}

/// Compute the constant ratio for the thresholds.
///
/// This ratio ensures that each bag, with the possible exceptions of certain small ones and the
/// final one, is a constant multiple of the previous, while fully occupying the `VoteWeight`
/// space.
pub fn constant_ratio(existential_weight: VoteWeight, n_bags: usize) -> f64 {
	((VoteWeight::MAX as f64 / existential_weight as f64).ln() / ((n_bags - 1) as f64)).exp()
}