magical_rs 0.1.2

Zero-dependency, no_std-friendly file type detection by magic bytes — accurate, fast, and extensible
Documentation
#[test]
fn test_iso_detect() {
    use magical_rs::magical::bytes_read::DEFAULT_MAX_BYTES_READ;
    use magical_rs::magical::{
        bytes_read::{read_file_header, with_bytes_read},
        magic::FileKind,
    };

    let iso_file = "tests/2.iso";
    let bytes_max = with_bytes_read();
    let wrong_max = DEFAULT_MAX_BYTES_READ;

    let header_bytes = read_file_header(iso_file, bytes_max).unwrap();
    let wrong_header = read_file_header(iso_file, wrong_max).unwrap();

    assert_eq!(FileKind::match_types(&header_bytes).unwrap(), FileKind::ISO);
    assert!(FileKind::match_types(&header_bytes).is_some());
    assert!(FileKind::match_types(&wrong_header).is_none());
}

#[test]
fn test_png_detect() {
    use magical_rs::magical::{
        bytes_read::{read_file_header, with_bytes_read},
        magic::FileKind,
    };

    let png_file = "tests/1.png";
    let bytes_max = with_bytes_read();
    let header_bytes = read_file_header(png_file, bytes_max).unwrap();

    assert_eq!(FileKind::match_types(&header_bytes).unwrap(), FileKind::Png);
    assert!(FileKind::match_types(&header_bytes).is_some());
}

#[test]
fn test_class_detect() {
    use magical_rs::magical::{
        bytes_read::{read_file_header, with_bytes_read},
        magic::FileKind,
    };

    let png_file = "tests/3.class";
    let bytes_max = with_bytes_read();
    let header_bytes = read_file_header(png_file, bytes_max).unwrap();

    assert_eq!(
        FileKind::match_types(&header_bytes).unwrap(),
        FileKind::Class
    );
    assert!(FileKind::match_types(&header_bytes).is_some());
}

#[test]
fn test_webp_detect() {
    use magical_rs::magical::{
        bytes_read::{read_file_header, with_bytes_read},
        magic::FileKind,
    };

    let webp_file = "tests/4.webp";
    let bytes_max = with_bytes_read();
    let header_bytes = read_file_header(webp_file, bytes_max).unwrap();

    assert_eq!(
        FileKind::match_types(&header_bytes).unwrap(),
        FileKind::WEBP
    );
    assert!(FileKind::match_types(&header_bytes).is_some());
}