From 60d7a4d9ae17f66b9498a07cfa24e75ebcd4a4e6 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Fri, 9 May 2025 18:01:56 +0300 Subject: [PATCH] chore: reduce macro usage --- src/store.rs | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/store.rs b/src/store.rs index 25bcce7..723f19b 100644 --- a/src/store.rs +++ b/src/store.rs @@ -7,6 +7,7 @@ use std::{ use anyhow::{ Context as _, Result, + anyhow, }; use derive_more::Deref; @@ -15,24 +16,6 @@ use crate::{ StorePath, }; -macro_rules! path_to_str { - ($path:ident) => { - let $path = $path.canonicalize().with_context(|| { - format!( - "failed to canonicalize path '{path}'", - path = $path.display(), - ) - })?; - - let $path = $path.to_str().with_context(|| { - format!( - "failed to convert path '{path}' to valid unicode", - path = $path.display(), - ) - })?; - }; -} - #[derive(Deref)] pub struct Connection(rusqlite::Connection); @@ -47,6 +30,24 @@ pub fn connect() -> Result { Ok(Connection(inner)) } +fn path_to_canonical_string(path: &Path) -> Result { + let path = path.canonicalize().with_context(|| { + format!( + "failed to canonicalize path '{path}'", + path = path.display(), + ) + })?; + + let path = path.into_os_string().into_string().map_err(|path| { + anyhow!( + "failed to convert path '{path}' to valid unicode", + path = path.display(), + ) + })?; + + Ok(path) +} + impl Connection { /// Gets the total closure size of the given store path by summing up the nar /// size of all depdendent derivations. @@ -65,7 +66,7 @@ impl Connection { JOIN ValidPaths ON p = id; "; - path_to_str!(path); + let path = path_to_canonical_string(path)?; let closure_size = self .prepare_cached(QUERY)? @@ -93,7 +94,7 @@ impl Connection { JOIN ValidPaths ON id = p; "; - path_to_str!(path); + let path = path_to_canonical_string(path)?; let packages: result::Result, _> = self .prepare_cached(QUERY)? @@ -132,7 +133,7 @@ impl Connection { SELECT p, c from graph; "; - path_to_str!(path); + let path = path_to_canonical_string(path)?; let mut adj = HashMap::>::new();