pub mod ffi;

mod sha2;
pub use sha2::*;

pub const HASH_EXAMPLE_DATA: &[u8] = b"lalilolela";

#[cfg(feature = "std")]
pub fn format_digest(digest: &[u32]) -> String {
    digest
        .iter()
        .map(|b| format!("{b:08x}"))
        .collect::<String>()
}

#[cfg(test)]
pub(crate) mod test {
    use super::*;
    use assert_hex::*;
    use pretty_assertions::{assert_eq, assert_ne};

    #[macro_export]
    macro_rules! assert_eq_bin {
    ($left:expr, $right:expr $(,)?) => ({
        match (&$left, &$right) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    panic!(r#"assertion `left == right` failed
  left: {:b}
 right: {:b}"#, &*left_val, &*right_val)
                }
            }
        }
    });
    ($left:expr, $right:expr, $($arg:tt)+) => ({
        match (&($left), &($right)) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    panic!(r#"assertion `left == right` failed: {}
  left: {:b}
 right: {:b}"#, format_args!($($arg)+), &*left_val, &*right_val)
                }
            }
        }
    });
}

    pub(crate) const TEST_VALUES: &[(&str, Digest256)] = &[
        (
            "AAAA",
            [
                0x63c1dd95, 0x1ffedf6f, 0x7fd968ad, 0x4efa39b8, 0xed584f16, 0x2f46e715, 0x114ee184,
                0xf8de9201,
            ],
        ),
        (
            "BAAA",
            [
                0x49e3cd45, 0x27c96cdc, 0x010160ff, 0x08520e0c, 0xb63c6ef8, 0xc4e7d486, 0x08995343,
                0x7f83a159,
            ],
        ),
    ];

    #[test]
    fn test_sha2_ffi_same_as_native() {
        let mut dig_native;
        let mut dig_ffi;
        for (input, expected_output) in TEST_VALUES.iter().copied() {
            dig_native = sha2_256_oneshot(input.as_bytes()).unwrap();
            dig_ffi = ffi::sha2_256_oneshot(input.as_bytes()).unwrap();
            assert_eq_hex!(
                dig_native,
                dig_ffi,
                "ffi does not return the same result as native"
            )
        }
    }
}
