pub mod common;
#[cfg(feature = "random")]
#[cfg_attr(docsrs, doc(cfg(feature = "random")))]
pub mod random;
#[cfg(feature = "md5")]
#[cfg_attr(docsrs, doc(cfg(feature = "md5")))]
pub mod md5;
use std::fs::{File, Metadata};
use std::io::Result;
pub trait CheckStrategy {
fn compare_file(&self, data: &'static [u8], metadata: &Metadata, file: &mut File) -> Result<bool>;
}
pub struct DefaultCheckStrategy;
impl DefaultCheckStrategy {
#[inline]
pub fn config() -> ConfigCheckStrategy {
ConfigCheckStrategy
}
#[inline]
pub fn lite() -> LiteCheckStrategy {
LiteCheckStrategy
}
#[inline]
pub fn heavy() -> HeavyCheckStrategy {
HeavyCheckStrategy
}
}
pub struct ConfigCheckStrategy;
impl CheckStrategy for ConfigCheckStrategy {
fn compare_file(&self, data: &'static [u8], metadata: &Metadata, _file: &mut File) -> Result<bool> {
Ok(metadata.len() != 0 || data.len() == 0)
}
}
pub struct LiteCheckStrategy;
impl CheckStrategy for LiteCheckStrategy {
fn compare_file(&self, data: &'static [u8], metadata: &Metadata, file: &mut File) -> Result<bool> {
common::AlwaysDifferent.compare_file(data, metadata, file)
}
}
pub struct HeavyCheckStrategy;
impl CheckStrategy for HeavyCheckStrategy {
fn compare_file(&self, data: &'static [u8], metadata: &Metadata, file: &mut File) -> Result<bool> {
#[cfg(not(feature = "md5"))] {
Ok(common::Size.compare_file(data, metadata, file)? && common::FirstNBytes::default().compare_file(data, metadata, file)?)
}
#[cfg(feature = "md5")] {
Ok(common::Size.compare_file(data, metadata, file)? && md5::Md5CheckStrategy.compare_file(data, metadata, file)?)
}
}
}