1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! # `magic-db`: Precompiled Magic Rules Database
//!
//! A precompiled database of file type detection rules based on the original `libmagic` project,
//! optimized and adapted for use with [`pure-magic`](https://crates.io/crates/pure-magic).
//! This crate provides ready-to-use file type detection capabilities without requiring external rule files.
//!
//! ## Features
//!
//! - **Precompiled Rules**: Optimized database embedded directly in your binary
//! - **No External Dependencies**: All rules are included in the compiled crate
//! - **Enhanced Rules**: Improved and extended versions of the original `libmagic` rules
//! - **Easy Integration**: Simple one-line access to the compiled database
//!
//! ### Optional Cargo Features
//!
//! - **global**: Enables `magic_db::global()`, a lazily-initialized, process-wide `MagicDb`.
//! This provides a convenient singleton but is optional. If you need explicit lifetime
//! control or multiple independent instances, use `magic_db::load()` instead.
//!
//! ## Installation
//!
//! Add `magic-db` to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! magic-db = "0.1" # Replace with the latest version
//! pure-magic = "0.1" # Required peer dependency
//! ```
//!
//! ## Usage
//!
//! ### Manual lifecycle (default)
//!
//! ```rust
//! use std::fs::File;
//! use std::env::current_exe;
//! use pure_magic::readers::DataReader;
//!
//! fn main() -> Result<(), pure_magic::Error> {
//! // Open the precompiled database
//! let db = magic_db::load()?;
//!
//! // Use it to detect file types
//! let magic = db.first_magic_file(current_exe()?)?;
//! assert!(!magic.is_default());
//!
//! println!("File type: {}", magic.message());
//! println!("MIME type: {}", magic.mime_type());
//! Ok(())
//! }
//! ```
//!
//! ### Global singleton (optional)
//!
//! The crate provides a **convenience global database** via the
//! `global` feature. This is process-wide, lazily initialized, and
//! kept alive until program termination.
//!
//! Enable it in `Cargo.toml`:
//!
//! ```toml
//! magic_db = { version = "0.1", features = ["global"] }
//! ```
//!
//! Then use it like this:
//!
//! ```rust
//! use magic_db::global;
//!
//! let db = global().unwrap();
//! ```
//!
//! **Note:** Use the global feature only if you want a single, shared
//! database. For multiple independent instances or explicit lifetime
//! management, use `magic_db::load()`.
//!
//! ## About the Rules
//!
//! This database contains slightly modified versions of the original `libmagic` rules that are available
//! in the [`src/magdir`](https://github.com/qjerome/magic-rs/tree/main/magic-db/src/magdir) directory of this repository.
//!
//! Some of the rules have been:
//! - **Adapted**: Modified to work with the [`pure-magic`](https://crates.io/crates/pure-magic) parser
//! - **Optimized**: Performance improvements for common file types
//! - **Extended**: Additional rules were created
//! - **Fixed**: Corrections to inaccurate or problematic original rules
//!
//! ## Rule Exclusions
//!
//! The database intentionally excludes the `der` rules (ASN.1/DER encoding rules) because:
//! - The [`pure-magic`](https://crates.io/crates/pure-magic) parser doesn't support (yet) the specific DER test types
//! implemented in the original `libmagic`
//!
//! ## Source Rules
//!
//! The source magic rules are available in the repository at:
//! [`src/magdir`](https://github.com/qjerome/magic-rs/tree/main/magic-db/src/magdir)
//!
//! You can:
//! 1. Browse the rules to understand how file types are detected
//! 2. Suggest improvements by opening issues or pull requests
//! 3. Use these rules as a reference for creating your own custom rules
//!
//! ## License
//!
//! This project is dual-licensed under either:
//! - **GPL-3.0**
//! - **BSD-2-Clause**
//!
//! ## See Also
//!
//! - [`pure-magic`](https://crates.io/crates/pure-magic): The core file type detection library
//! - [`magic-embed`](https://crates.io/crates/magic-embed): The macro used to create this database
//! - [`magic`](https://www.man7.org/linux/man-pages/man4/magic.4.html): Expected magic rule format
use magic_embed;
use ;
use OnceLock;
static DB: = new;
;
/// Returns a process-wide read-only [`MagicDb`] initialized on first use.
///
/// This function is provided as a convenience for applications that
/// want a shared database without managing its lifetime explicitly.
/// The database is kept alive until program termination.
///
/// If you need explicit control over the database lifetime or want
/// multiple independent instances, use [`load`] instead.
/// Loads a [`MagicDb`] from the embedded, precompiled database.
///
/// This function constructs an owned [`MagicDb`] from data embedded
/// at compile time. No file system access or runtime dependencies
/// are involved.
///
/// Each call returns a new, independent instance.