Code Monkey home page Code Monkey logo

rust-magic's Introduction

rust-magic linux build status Documentation REUSE status CII Best Practices OpenSSF Scorecard

libmagic bindings for Rust.

Usage

This magic crate is published on the crates.io Rust package registry.

Use cargo add to specify dependencies:

$ cargo add magic

You might be familiar with libmagic's CLI; file:

$ file data/tests/rust-logo-128x128-blk.png
data/tests/rust-logo-128x128-blk.png: PNG image data, 128 x 128, 8-bit/color RGBA, non-interlaced

You can implement something similar in Rust with the magic crate (see examples/file-ish.rs):

fn file_example() -> Result<(), magic::MagicError> {
    // Open a new configuration with flags
    let cookie = magic::Cookie::open(magic::CookieFlags::ERROR)?;

    // Load a specific database (so exact text assertion below works regardless of the system's default database)
    cookie.load(&vec!["data/tests/db-images-png"])?;

    let file = "data/tests/rust-logo-128x128-blk.png";

    // Analyze the file
    assert_eq!(cookie.file(file)?, "PNG image data, 128 x 128, 8-bit/color RGBA, non-interlaced");

    Ok(())
}
$ cargo run --example file-ish -- data/tests/rust-logo-128x128-blk.png
PNG image data, 128 x 128, 8-bit/color RGBA, non-interlaced

Read the magic rustdoc for further examples and info.

MSRV

The Minimum Supported Rust Version (MSRV) is Rust 1.56 or higher.

This version might be changed in the future, but it will be done with a crate version bump.

Requirements

By default, compiling the magic crate will search your system library paths for a shared library version of libmagic to link against. For this to work, you need to install the development version of libmagic in a standard location:

$ # On Debian based Linux systems:
$ sudo apt-get install libmagic1 libmagic-dev

$ # On MacOs:
$ brew install libmagic

$ # On Windows:
$ cargo install cargo-vcpkg
$ cargo vcpkg build

If you're cross-compiling, or need more control over which library is selected, see how to build magic-sys.

License

This project is licensed under either of

at your option.

For further details, see LICENSE.md.

Contribution

See CONTRIBUTING.md.

rust-magic's People

Contributors

robo9k avatar thestinger avatar wunki avatar onur avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.