Compare commits

..

1 commit

Author SHA1 Message Date
bad
459ed33bdd Add integration for libnixstore
This adds a conditional feature flag for loading data from libnixstore
exposed behind a feature flag dependency
2022-08-19 23:33:27 +02:00
6 changed files with 58 additions and 4 deletions

View file

@ -1,11 +1,18 @@
[package] [package]
name = "narinfo" name = "narinfo"
version = "1.0.1" version = "1.0.0"
edition = "2021" edition = "2021"
description = "A parser for the narinfo file format" description = "A parser for the narinfo file format"
repository = "https://im.badat.dev/bad/narinfo-rs" repository = "https://im.badat.dev/bad/narinfo-rs"
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
keywords = ["nix", "nixos"] keywords = ["nix", "nixos"]
[features]
libnixstore = ["dep:libnixstore"]
[dependencies] [dependencies]
derive_builder = { version = "0.11.2", default-features = false } derive_builder = { version = "0.11.2", default-features = false }
libnixstore = { version = "0.2.0", optional = true }
[package.metadata.docs.rs]
all-features = true

10
shell.nix Normal file
View file

@ -0,0 +1,10 @@
with import <nixpkgs> { };
mkShell {
nativeBuildInputs = [
nix
nlohmann_json
libsodium
boost
pkg-config
];
}

View file

@ -6,10 +6,13 @@ extern crate alloc;
pub mod error; pub mod error;
mod narinfo; pub mod narinfo;
mod nix_cache_info; pub mod nix_cache_info;
mod sig; mod sig;
#[cfg(feature = "libnixstore")]
mod libnixstore;
pub use crate::narinfo::NarInfo; pub use crate::narinfo::NarInfo;
pub use nix_cache_info::NixCacheInfo; pub use nix_cache_info::NixCacheInfo;
pub use sig::Sig; pub use sig::Sig;

27
src/libnixstore.rs Normal file
View file

@ -0,0 +1,27 @@
use crate::narinfo::NarInfoBuilder;
use crate::Sig;
use alloc::borrow::Cow;
impl<'a> NarInfoBuilder<'a> {
/// Read and set the store dir from [`libnixstore`].
///
/// This function assumes that [`libnixstore::init`] has already been called
pub fn store_dir_from_libnixstore(&mut self) -> &Self {
let store_dir = libnixstore::get_store_dir();
self.store_path(Cow::Owned(store_dir));
self
}
/// Read and set `path`, `references` and `sigs` from a [`libnixstore::PathInfo`]
pub fn attr_from_libnixstore_path(&mut self, path: &'a libnixstore::PathInfo) -> &Self {
self.file_size(Some(path.size));
self.references(path.refs.iter().map(Cow::from).collect());
self.sigs(
path.sigs
.iter()
.map(|v| Sig::parse(v).expect("libnixstore provided signature should be in a valid format"))
.collect());
self
}
}

View file

@ -49,7 +49,7 @@ impl<'a> NarInfo<'a> {
"System" => builder.system(Some(Cow::from(value))), "System" => builder.system(Some(Cow::from(value))),
"References" => builder.references(value.split(' ').map(Cow::from).collect()), "References" => builder.references(value.split(' ').map(Cow::from).collect()),
"Sig" => { "Sig" => {
sigs.push(Sig::parse(value)?); sigs.push(Sig::try_from(value)?);
&mut builder &mut builder
}, },
_ => return Err(ParsingError::UnknownKey { key }), _ => return Err(ParsingError::UnknownKey { key }),

View file

@ -12,6 +12,13 @@ pub struct Sig<'a> {
pub sig: Cow<'a, str>, pub sig: Cow<'a, str>,
} }
impl<'a> TryFrom<&'a str> for Sig<'a> {
type Error = ParsingError<'a>;
fn try_from(value: &'a str) -> ParsingResult<Self> {
Sig::parse(value)
}
}
// Neither the parse nor the serializa method is public since // Neither the parse nor the serializa method is public since
// it doesn't really make sense to de/serialize the // it doesn't really make sense to de/serialize the
// sig into the narinfo format outside of de/serializing a whole narinfo // sig into the narinfo format outside of de/serializing a whole narinfo