Giter VIP home page Giter VIP logo

Comments (2)

robo9k avatar robo9k commented on August 28, 2024

After adding rustdoc to the functions, I wondered if there is any use to separate open() and load(). Bindings in other languages either stay close to the C API (like we do so far) or have a constructor/function that needs both flags and databases.

Just to check if there might be an uncommon usecase where one does not want to load databases, I tried the following with the current implementation:

#[test]
fn no_databases(){
    let cookie = Cookie::open(flags::NO_CHECK_SOFT).unwrap();
    let path = "data/tests/rust-logo-128x128-blk.png";
    assert_eq!(cookie.file(path).unwrap(), "huh?");
}

However this fails with an error from libmagic itself: no magic files loaded

Which leads me to believe you can not use the most important magic_file() and magic_buffer() functions without calling both magic_open() and magic_load() first.
You should be able to just call magic_open() and then magic_compile(), magic_check() or magic_list(), but I don't consider those to be the common use case. Both check() and compile() could be implemented separately and their cookie/flags value only seems to affect debug output anyways.

So maybe it makes sense to drop the idea of CookieBuilder and just have something like this:

impl Cookie {
    pub fn new<P: AsRef<Path>>(flags: CookieFlags, databases: &[P]) -> Cookie {
    }

    pub fn set_flags(&self, flags: CookieFlags) -> Result<(), MagicError> {
    }
    pub fn load<P: AsRef<Path>>(&self, databases: &[P]) -> Result<(), MagicError> {
    }
}

In the same vein, maybe it makes sense to have an enum for databases with DefaultFile and File(&[Path]) and error if you pass an empty slice.
Also note that newer versions of libmagic support loading the database from a buffer instead of file(s).
Extra note: set_flags() and load() could store their parameters in the struct Cookie so getters can be added. They might need to take &mut self anyways for thread safety.

This all sounds like some redesign is in order 😄

from rust-magic.

robo9k avatar robo9k commented on August 28, 2024

Fast forward a few days and we still have the ::open() & ::load() API for Cookie..

When refactoring error handling in #112 it became clear, that the - here "DefaultFile"/"File" called enum - data type for "a list of valid database filenames or the default unnamed database" would help there as well.

There's now a magic_getflags() in libmagic (along with magic_getparam() and magic_setparam()).
There's no getter for the loaded databases, which would be difficult with magic_load_buffers().

There's now magic_getpath() which can return the path of the unnamed default database file.

from rust-magic.

Related Issues (20)

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.