pub struct MagicDb { /* private fields */ }Expand description
Represents a database of MagicRule
Implementations§
Source§impl MagicDb
impl MagicDb
Sourcepub fn load(&mut self, ms: MagicSource) -> &mut Self
pub fn load(&mut self, ms: MagicSource) -> &mut Self
Sourcepub fn load_bulk<I: Iterator<Item = MagicSource>>(&mut self, it: I) -> &mut Self
pub fn load_bulk<I: Iterator<Item = MagicSource>>(&mut self, it: I) -> &mut Self
Loads multiple MagicSource items efficiently in bulk.
This is more efficient than loading each individually. After processing all sources, it applies finalization step only once.
Sourcepub fn first_magic<R: DataRead>(
&self,
r: &mut R,
extension: Option<&str>,
) -> Result<Magic<'_>, Error>
pub fn first_magic<R: DataRead>( &self, r: &mut R, extension: Option<&str>, ) -> Result<Magic<'_>, Error>
Detects file Magic stopping at the first matching magic. Magic
rules are evaluated from the best to the least relevant, so this method
returns most of the time the best magic. For the rare cases where
it doesn’t or if the best result is always required, use MagicDb::best_magic
§Arguments
r- A reader implementingDataReadextension- Optional file extension to use for acceleration
§Returns
Result<Magic<'_>, Error>- The detection result or an error
§Notes
- Use this method only if you need to re-use a
readerfor future read operations. - Use
DataReaderto create a genericreader
§Warning
File extension acceleration is made to evaluate rules faster by testing
first the rules defining this extension with an !:ext entry.
Whether you use extension acceleration or not with this function should not
produce different results. Yet this makes the assumption rules are written
correctly and every rule concerned defines !:ext when it is appropriate.
If some rules are missing it, results might differ.
Sourcepub fn first_magic_file<P: AsRef<Path>>(
&self,
path: P,
) -> Result<Magic<'_>, Error>
pub fn first_magic_file<P: AsRef<Path>>( &self, path: P, ) -> Result<Magic<'_>, Error>
Detects file Magic from a file path.
This is a convenience method that opens the file and creates a DataReader::File
internally. The file extension is automatically extracted and passed to
MagicDb::first_magic.
§Errors
Returns an error if the file cannot be opened or if magic detection fails.
Sourcepub fn first_magic_slice<S: AsRef<[u8]>>(
&self,
s: S,
extension: Option<&str>,
) -> Result<Magic<'_>, Error>
pub fn first_magic_slice<S: AsRef<[u8]>>( &self, s: S, extension: Option<&str>, ) -> Result<Magic<'_>, Error>
Detects file Magic from an in-memory byte slice.
This is a convenience method that creates a DataReader::Slice internally.
§Errors
Returns an error if magic detection fails.
Sourcepub fn all_magics<R: DataRead>(
&self,
r: &mut R,
) -> Result<Vec<Magic<'_>>, Error>
pub fn all_magics<R: DataRead>( &self, r: &mut R, ) -> Result<Vec<Magic<'_>>, Error>
Detects all Magic matching a given content.
§Arguments
r- A reader implementingDataRead
§Returns
Result<Vec<Magic<'_>>, Error>- All detection results sorted by strength or an error
§Notes
- Use this method only if you need to re-use a
readerfor future read operations. - Use
DataReaderto create a genericreader
Sourcepub fn all_magics_file<P: AsRef<Path>>(
&self,
path: P,
) -> Result<Vec<Magic<'_>>, Error>
pub fn all_magics_file<P: AsRef<Path>>( &self, path: P, ) -> Result<Vec<Magic<'_>>, Error>
Detects all matching Magic entries from a file path.
This is a convenience method that opens the file and creates a DataReader::File
internally, then calls MagicDb::all_magics.
§Errors
Returns an error if the file cannot be opened or if magic detection fails.
Sourcepub fn all_magics_slice<S: AsRef<[u8]>>(
&self,
slice: S,
) -> Result<Vec<Magic<'_>>, Error>
pub fn all_magics_slice<S: AsRef<[u8]>>( &self, slice: S, ) -> Result<Vec<Magic<'_>>, Error>
Detects all matching Magic entries from an in-memory byte slice.
This is a convenience method that creates a DataReader::Slice internally,
then calls MagicDb::all_magics.
§Errors
Returns an error if magic detection fails.
Sourcepub fn best_magic_file<P: AsRef<Path>>(
&self,
path: P,
) -> Result<Magic<'_>, Error>
pub fn best_magic_file<P: AsRef<Path>>( &self, path: P, ) -> Result<Magic<'_>, Error>
Detects the best matching Magic from a file path.
This is a convenience method that opens the file and creates a DataReader::File
internally, then calls MagicDb::best_magic.
§Errors
Returns an error if the file cannot be opened or if magic detection fails.
Sourcepub fn best_magic_slice<S: AsRef<[u8]>>(
&self,
slice: S,
) -> Result<Magic<'_>, Error>
pub fn best_magic_slice<S: AsRef<[u8]>>( &self, slice: S, ) -> Result<Magic<'_>, Error>
Detects the best matching Magic from an in-memory byte slice.
This is a convenience method that creates a DataReader::Slice internally,
then calls MagicDb::best_magic.
§Errors
Returns an error if magic detection fails.