Skip to content

Commit 02d1c21

Browse files
committed
Extract threshold conversion into a function.
This will ensure that `cargo fix` doesn't remove the target specific code when it's not exercised.
1 parent 1c66918 commit 02d1c21

1 file changed

Lines changed: 31 additions & 22 deletions

File tree

src/run.rs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ async fn image_ids_in_use(docker: &Docker) -> io::Result<HashSet<String>> {
196196

197197
// Determine Docker's root directory.
198198
#[cfg(target_os = "linux")]
199-
async fn docker_root_dir() -> io::Result<PathBuf> {
200-
let docker = Docker::connect_with_local_defaults().map_err(io::Error::other)?;
199+
async fn docker_root_dir(docker: &Docker) -> io::Result<PathBuf> {
201200
let info: SystemInfo = docker.info().await.map_err(io::Error::other)?;
202201
info.docker_root_dir
203202
.map(PathBuf::from)
@@ -221,8 +220,8 @@ async fn get_disk_by_file<'a>(disks: &'a [Disk], path: &Path) -> io::Result<&'a
221220

222221
// Find size of filesystem on which docker root directory is stored.
223222
#[cfg(target_os = "linux")]
224-
async fn docker_root_dir_filesystem_size() -> io::Result<Byte> {
225-
let root_dir = docker_root_dir().await?;
223+
async fn docker_root_dir_filesystem_size(docker: &Docker) -> io::Result<Byte> {
224+
let root_dir = docker_root_dir(docker).await?;
226225
let system = System::new_with_specifics(RefreshKind::new().with_disks_list());
227226
let disks = system.disks();
228227
let disk = get_disk_by_file(disks, &root_dir).await?;
@@ -605,26 +604,11 @@ async fn vacuum(
605604
// Stream Docker events and vacuum when necessary.
606605
#[allow(clippy::type_complexity)]
607606
pub async fn run(settings: &Settings, state: &mut State, first_run: &mut bool) -> io::Result<()> {
608-
// Determine the threshold in bytes.
609-
let threshold = match settings.threshold {
610-
Threshold::Absolute(b) => b,
611-
612-
#[cfg(target_os = "linux")]
613-
Threshold::Percentage(p) =>
614-
{
615-
#[allow(
616-
clippy::cast_precision_loss,
617-
clippy::cast_possible_truncation,
618-
clippy::cast_sign_loss
619-
)]
620-
Byte::from_bytes(
621-
(p * docker_root_dir_filesystem_size().await?.get_bytes() as f64) as u128,
622-
)
623-
}
624-
};
625-
626607
let docker = Docker::connect_with_local_defaults().map_err(io::Error::other)?;
627608

609+
// Determine the threshold in bytes.
610+
let threshold = threshold_unit(&settings.threshold, &docker).await?;
611+
628612
// NOTE: Don't change this log line, since the test in the Homebrew formula
629613
// (https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/d/docuum.rb) relies on it.
630614
info!("Performing an initial vacuum on startup\u{2026}");
@@ -737,6 +721,31 @@ pub async fn run(settings: &Settings, state: &mut State, first_run: &mut bool) -
737721
)))
738722
}
739723

724+
#[allow(clippy::unused_async)]
725+
#[cfg(not(target_os = "linux"))]
726+
async fn threshold_unit(threshold: &Threshold, _docker: &Docker) -> io::Result<Byte> {
727+
let Threshold::Absolute(b) = threshold;
728+
Ok(*b)
729+
}
730+
731+
#[cfg(target_os = "linux")]
732+
async fn threshold_unit(threshold: &Threshold, docker: &Docker) -> io::Result<Byte> {
733+
match threshold {
734+
Threshold::Absolute(b) => Ok(*b),
735+
Threshold::Percentage(p) =>
736+
{
737+
#[allow(
738+
clippy::cast_precision_loss,
739+
clippy::cast_possible_truncation,
740+
clippy::cast_sign_loss
741+
)]
742+
Ok(Byte::from_bytes(
743+
(p * docker_root_dir_filesystem_size(docker).await?.get_bytes() as f64) as u128,
744+
))
745+
}
746+
}
747+
}
748+
740749
#[cfg(test)]
741750
mod tests {
742751
use {

0 commit comments

Comments
 (0)