feat(nix-compat/narinfo/signature): generalize name field

Requiring `name` to be a `&str` means it'll get annoying to pass around
`Signature`, but being able to pass them around in an owned fashion is
kinda a requirement for a stronger typed `PathInfo` struct, where we
want to have full ownership.

Rework the `Signature` struct to become generic over the type of the
`name` field. This means, it becomes possible to have owned versions
of it.

We don't want to impose `String` or `SmolStr` for example, but want to
leave it up to the nix-compat user to decide.

Provide a type alias for the existing `&str` variant (`SignatureRef`),
and use it where we previously used the non-generic `Signature` one.

Add some tests to ensure it's possible to *use* `Signature` with both
`String` and `SmolStr` (but only pull in `smol_str` as dev dependency
for the tests).

Also, add some more docstrings, these were a bit sparse.

Change-Id: I3f75691498c6bda9cd072d2d9dac83c4f6c57287
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12253
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Reviewed-by: raitobezarius <tvl@lahfa.xyz>
This commit is contained in:
Florian Klink 2024-08-19 16:57:26 +03:00 committed by clbot
parent 7612cb4c31
commit a259613c76
9 changed files with 117 additions and 45 deletions

View file

@ -32,7 +32,7 @@ mod signing_keys;
mod verifying_keys;
pub use fingerprint::fingerprint;
pub use signature::{Error as SignatureError, Signature};
pub use signature::{Error as SignatureError, Signature, SignatureRef};
pub use signing_keys::parse_keypair;
pub use signing_keys::{Error as SigningKeyError, SigningKey};
pub use verifying_keys::{Error as VerifyingKeyError, VerifyingKey};
@ -51,7 +51,7 @@ pub struct NarInfo<'a> {
pub references: Vec<StorePathRef<'a>>,
// authenticity
/// Ed25519 signature over the path fingerprint
pub signatures: Vec<Signature<'a>>,
pub signatures: Vec<SignatureRef<'a>>,
/// Content address (for content-defined paths)
pub ca: Option<CAHash>,
// derivation metadata
@ -246,7 +246,7 @@ impl<'a> NarInfo<'a> {
};
}
"Sig" => {
let val = Signature::parse(val)
let val = SignatureRef::parse(val)
.map_err(|e| Error::UnableToParseSignature(signatures.len(), e))?;
signatures.push(val);
@ -578,7 +578,7 @@ CA: fixed:r:sha1:1ak1ymbmsfx7z8kh09jzkr3a4dvkrfjw
// ensure the signature is added
let new_sig = narinfo.signatures.last().unwrap();
assert_eq!(signing_key.name(), new_sig.name());
assert_eq!(signing_key.name(), *new_sig.name());
// verify the new signature against the verifying key
let verifying_key = super::VerifyingKey::parse(super::DUMMY_VERIFYING_KEY)