use std::fmt;
use std::str::FromStr;
use crate::{Error, Hash, Luma, LumaBuf, Result, Rgba, SignatureAlgorithm};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Algorithm {
AverageV1,
DifferenceV1,
PerceptualV1,
SimHashV1,
PdqV1,
BlockhashV1,
}
impl Algorithm {
pub const ALL: [Self; 6] = [
Self::AverageV1,
Self::DifferenceV1,
Self::PerceptualV1,
Self::SimHashV1,
Self::PdqV1,
Self::BlockhashV1,
];
#[must_use]
pub const fn id(self) -> &'static str {
match self {
Self::AverageV1 => "average-v1",
Self::DifferenceV1 => "difference-v1",
Self::PerceptualV1 => "perceptual-v1",
Self::SimHashV1 => "simhash-v1",
Self::PdqV1 => "pdq-v1",
Self::BlockhashV1 => "blockhash-v1",
}
}
#[must_use]
pub const fn bits(self) -> u32 {
match self {
Self::AverageV1 | Self::DifferenceV1 | Self::PerceptualV1 | Self::SimHashV1 => 64,
Self::PdqV1 | Self::BlockhashV1 => 256,
}
}
#[must_use]
pub const fn is_image(self) -> bool {
match self {
Self::AverageV1 | Self::DifferenceV1 | Self::PerceptualV1 | Self::PdqV1 | Self::BlockhashV1 => true,
Self::SimHashV1 => false,
}
}
pub fn from_id(id: &str) -> Result<Self> {
if let Some(algorithm) = Self::ALL.into_iter().find(|algorithm| algorithm.id() == id) {
return Ok(algorithm);
}
match SignatureAlgorithm::ALL
.into_iter()
.find(|algorithm| algorithm.id() == id)
{
Some(algorithm) => Err(Error::NotABitHash(algorithm)),
None => Err(Error::UnknownAlgorithm(id.to_owned())),
}
}
pub fn hash_luma(self, image: Luma<'_>) -> Result<Hash> {
match self {
Self::AverageV1 => Ok(crate::average_v1(image)),
Self::DifferenceV1 => Ok(crate::difference_v1(image)),
Self::PerceptualV1 => Ok(crate::perceptual_v1(image)),
Self::PdqV1 => Ok(crate::pdq_v1_luma(image).hash),
Self::BlockhashV1 => Ok(crate::blockhash_v1(crate::RgbaBuf::from_luma(image).view())),
Self::SimHashV1 => Err(Error::NotAnImageAlgorithm(self)),
}
}
pub fn hash_rgba(self, image: Rgba<'_>) -> Result<Hash> {
match self {
Self::PdqV1 => Ok(crate::pdq_v1(image).hash),
Self::BlockhashV1 => Ok(crate::blockhash_v1(image)),
Self::AverageV1 | Self::DifferenceV1 | Self::PerceptualV1 => {
let luma = LumaBuf::from_rgba8_v1(image.width(), image.height(), image.pixels())?;
self.hash_luma(luma.view())
}
Self::SimHashV1 => Err(Error::NotAnImageAlgorithm(self)),
}
}
#[cfg(feature = "image")]
pub fn hash_encoded(self, bytes: &[u8]) -> Result<Hash> {
match self {
Self::PdqV1 | Self::BlockhashV1 => self.hash_rgba(crate::decode_rgba_v1(bytes)?.view()),
Self::AverageV1 | Self::DifferenceV1 | Self::PerceptualV1 => {
self.hash_luma(crate::decode_luma_v1(bytes)?.view())
}
Self::SimHashV1 => Err(Error::NotAnImageAlgorithm(self)),
}
}
}
impl fmt::Display for Algorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.id())
}
}
impl FromStr for Algorithm {
type Err = Error;
fn from_str(id: &str) -> Result<Self> {
Self::from_id(id)
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for Algorithm {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
serializer.serialize_str(self.id())
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Algorithm {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
deserializer.deserialize_str(crate::serde_support::FromStrVisitor::new("a hash algorithm id"))
}
}