diff --git a/gix-hash/src/kind.rs b/gix-hash/src/kind.rs index 8d8e01681fa..3cede752386 100644 --- a/gix-hash/src/kind.rs +++ b/gix-hash/src/kind.rs @@ -174,4 +174,21 @@ impl Kind { pub const fn empty_tree(&self) -> ObjectId { ObjectId::empty_tree(*self) } + + /// Return a list of available hash kinds. + #[inline] + pub const fn all() -> &'static [Self] { + #[cfg(all(feature = "sha1", not(feature = "sha256")))] + { + &[Self::Sha1] + } + #[cfg(all(not(feature = "sha1"), feature = "sha256"))] + { + &[Self::Sha256] + } + #[cfg(all(feature = "sha1", feature = "sha256"))] + { + &[Self::Sha1, Self::Sha256] + } + } } diff --git a/gix-hash/tests/hash/kind.rs b/gix-hash/tests/hash/kind.rs index 1a6ea642e6e..fa0b0227183 100644 --- a/gix-hash/tests/hash/kind.rs +++ b/gix-hash/tests/hash/kind.rs @@ -103,3 +103,21 @@ fn longest_sha1_and_sha256() { let longest = Kind::longest(); assert_eq!(longest, Kind::Sha256); } + +#[test] +#[cfg(all(feature = "sha1", not(feature = "sha256")))] +fn all() { + assert_eq!(Kind::all(), &[Kind::Sha1]); +} + +#[test] +#[cfg(all(not(feature = "sha1"), feature = "sha256"))] +fn all() { + assert_eq!(Kind::all(), &[Kind::Sha256]); +} + +#[test] +#[cfg(all(feature = "sha1", feature = "sha256"))] +fn all() { + assert_eq!(Kind::all(), &[Kind::Sha1, Kind::Sha256]); +}