From fc73fa9722a17a45c1e1650ff5ff729d4bc30017 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Thu, 8 May 2025 22:40:25 +0300 Subject: [PATCH] feat: delete &StorePath, make StorePathBuf owned and remove Buf suffix --- src/lib.rs | 31 ++++++++++++++++++++----------- src/store.rs | 7 +++---- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 92f49b5..9779dee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,37 +1,46 @@ use std::{ - path::{ - Path, - PathBuf, - }, + path::PathBuf, sync, }; use anyhow::{ Context as _, + Error, Result, anyhow, bail, }; use derive_more::Deref; -use ref_cast::RefCast; -pub mod error; -pub mod print; +// pub mod diff; +// pub mod print; pub mod store; mod version; +use ref_cast::RefCast as _; pub use version::Version; #[derive(Deref, Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct DerivationId(i64); #[derive(Deref, Debug, Clone, PartialEq, Eq)] -pub struct StorePathBuf(PathBuf); +pub struct StorePath(PathBuf); -#[derive(RefCast, Deref, Debug, PartialEq, Eq)] -#[repr(transparent)] -pub struct StorePath(Path); +impl TryFrom for StorePath { + type Error = Error; + + fn try_from(path: PathBuf) -> Result { + if !path.starts_with("/nix/store") { + bail!( + "path {path} must start with /nix/store", + path = path.display(), + ); + } + + Ok(StorePath(path)) + } +} impl StorePath { /// Parses a Nix store path to extract the packages name and possibly its diff --git a/src/store.rs b/src/store.rs index d710917..f2fd957 100644 --- a/src/store.rs +++ b/src/store.rs @@ -13,7 +13,6 @@ use rustc_hash::{ use crate::{ DerivationId, StorePath, - StorePathBuf, }; macro_rules! path_to_str { @@ -53,7 +52,7 @@ impl Connection { pub fn query_depdendents( &mut self, path: &StorePath, - ) -> Result> { + ) -> Result> { const QUERY: &str = " WITH RECURSIVE graph(p) AS ( @@ -70,12 +69,12 @@ impl Connection { path_to_str!(path); - let packages: result::Result, _> = self + let packages: result::Result, _> = self .prepare_cached(QUERY)? .query_map([path], |row| { Ok(( DerivationId(row.get(0)?), - StorePathBuf(row.get::<_, String>(1)?.into()), + StorePath(row.get::<_, String>(1)?.into()), )) })? .collect();