bojand / infer

Small crate to infer file and MIME type by checking the magic number signature
MIT License
299 stars 28 forks source link

API using Read trait #76

Closed bojand closed 1 year ago

bojand commented 2 years ago

Initial work and idea to support Read trait.

Addresses #51.

Example usage:

use std::fs;
use std::io::prelude::*;
use std::fs::File;

fn main() -> std::io::Result<()> {
    let info = infer::Infer::new();
    let mut f = File::open("testdata/sample.jpg")?;
    let kind = info.get_read(&mut f).unwrap().expect("file type is known");
    assert_eq!(kind.mime_type(), "image/jpeg");
    assert_eq!(kind.extension(), "jpg");
    Ok(())
}

Complementary *_read functions are provided to match existing ones that take a buffer.