Comments (2)
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.
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)
- Use `magic_file()` to read from stdin HOT 1
- Extend `CONTRIBUTING.md` doc HOT 1
- Split repo, crate and possibly website readme HOT 1
- Format rustdoc code examples
- Find alternative to hosting rustdoc on rust-ci.org HOT 1
- Refactor error handling HOT 2
- Refactor naming/modules HOT 1
- License of Rust logo test file
- Add even more badges to the GitHub README
- Use of #![feature] requires Rust Nightly
- cookie.load stopped working on nightly for some reason
- Implement `Debug` for `magic::Cookie` HOT 2
- Where to get a more complete database? HOT 7
- Fix LICENSE for GitHub and other use cases
- Investigate all clippy lints
- OpenSSF best practices
- OpenSSF scorecard
- Relicense under dual MIT/Apache-2.0 HOT 4
- Replace `libc` with `core::ffi` in Rust 1.64.0
- Investigate Rust API Guidelines HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust-magic.