qubit-mime 0.2.2

MIME type detection utilities for Rust based on filename glob rules and content magic
Documentation
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*******************************************************************************
 *
 *    Copyright (c) 2026 Haixing Hu.
 *
 *    SPDX-License-Identifier: Apache-2.0
 *
 *    Licensed under the Apache License, Version 2.0.
 *
 ******************************************************************************/
//! Registry for pluggable MIME detector providers.
//!
//! The registry is the selection layer used by the detector wrappers. It maps
//! stable provider names and aliases to factories, checks provider availability,
//! and resolves configured fallback chains. Default wrappers use the process-wide
//! default registry, while applications that need custom provider isolation can
//! pass an explicit registry to
//! [`BoxMimeDetector::from_registry`](crate::BoxMimeDetector::from_registry) or
//! [`ArcMimeDetector::from_registry`](crate::ArcMimeDetector::from_registry).
// qubit-style: allow coverage-cfg

#[cfg(coverage)]
use std::sync::PoisonError;
use std::sync::{
    Arc,
    LazyLock,
    RwLock,
    RwLockReadGuard,
    RwLockWriteGuard,
};

use crate::{
    BoxMimeDetector,
    MimeConfig,
    MimeError,
    MimeResult,
};

use super::{
    FileCommandMimeDetectorProvider,
    MimeDetectorAvailability,
    MimeDetectorProvider,
    RepositoryMimeDetectorProvider,
};

/// Registry of MIME detector providers.
///
/// Provider names and aliases are matched case-insensitively. Duplicate ids or
/// aliases are rejected at registration time so a selector always resolves to
/// at most one provider.
///
/// # Default and fallback selection
///
/// [`MimeDetectorRegistry::create_default`] reads
/// [`MimeConfig::mime_detector_default`](crate::MimeConfig::mime_detector_default)
/// first. When the default selector is empty or `auto`, the registry tries all
/// available providers ordered by descending provider priority and then by
/// provider id. Otherwise it tries the configured default followed by
/// [`MimeConfig::mime_detector_fallbacks`](crate::MimeConfig::mime_detector_fallbacks).
///
/// Selection stops at the first provider that can create a detector. Unknown,
/// unavailable, or failing providers are collected into
/// [`MimeError::NoAvailableDetector`](crate::MimeError::NoAvailableDetector)
/// only when the whole candidate chain fails.
///
/// # Examples
///
/// Use a registry containing only built-in providers:
///
/// ```rust
/// use qubit_mime::{
///     MimeConfig,
///     MimeDetector,
///     MimeDetectorRegistry,
///     MimeResult,
/// };
///
/// # fn main() -> MimeResult<()> {
/// let registry = MimeDetectorRegistry::builtin();
/// assert!(registry.find_provider("repository-mime-detector").is_some());
///
/// let detector = registry.create_default(&MimeConfig::default())?;
/// assert_eq!(
///     Some("text/plain".to_owned()),
///     detector.detect_by_filename("notes.txt"),
/// );
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Default)]
pub struct MimeDetectorRegistry {
    /// Registered detector providers.
    providers: Vec<Arc<dyn MimeDetectorProvider>>,
}

/// Process-wide default detector registry.
static DEFAULT_MIME_DETECTOR_REGISTRY: LazyLock<RwLock<MimeDetectorRegistry>> =
    LazyLock::new(|| RwLock::new(MimeDetectorRegistry::builtin()));
/// Backend name used when reporting default registry lock failures.
#[cfg(not(coverage))]
const BACKEND: &str = "mime-detector-registry";
/// Error reason used when a default registry lock is poisoned.
#[cfg(not(coverage))]
const LOCK_ERR: &str = "lock poisoned";

impl MimeDetectorRegistry {
    /// Creates an empty detector registry.
    ///
    /// # Returns
    /// Empty registry.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a registry containing only built-in detector providers.
    ///
    /// # Returns
    /// Registry with repository and file-command providers.
    pub fn builtin() -> Self {
        Self {
            providers: vec![
                Arc::new(RepositoryMimeDetectorProvider) as Arc<dyn MimeDetectorProvider>,
                Arc::new(FileCommandMimeDetectorProvider) as Arc<dyn MimeDetectorProvider>,
            ],
        }
    }

    /// Gets a snapshot of the process-wide default detector registry.
    ///
    /// The returned registry is cloned from the global default registry, so
    /// callers can inspect or create detectors without holding a global lock.
    ///
    /// # Returns
    /// Snapshot of the default registry.
    ///
    /// # Errors
    /// Returns [`MimeError::DetectorBackend`] when the global registry lock has
    /// been poisoned by another thread.
    pub fn default_registry() -> MimeResult<Self> {
        let registry = read_default_registry()?;
        Ok(registry.clone())
    }

    /// Registers a provider in the process-wide default detector registry.
    ///
    /// Detectors created through [`BoxMimeDetector::from_config`] and
    /// [`BoxMimeDetector::from_name`](crate::BoxMimeDetector::from_name) use
    /// this registry, so successfully registered providers become visible to
    /// default wrapper constructors throughout the current process.
    ///
    /// # Parameters
    /// - `provider`: Provider to register globally.
    ///
    /// # Errors
    /// Returns [`MimeError::DuplicateDetectorName`] when the provider id or one
    /// of its aliases already exists in the default registry. Returns
    /// [`MimeError::DetectorBackend`] when the global registry lock has been
    /// poisoned by another thread.
    pub fn register_default<P>(provider: P) -> MimeResult<()>
    where
        P: MimeDetectorProvider + 'static,
    {
        let mut registry = write_default_registry()?;
        registry.register(provider)
    }

    /// Registers a provider.
    ///
    /// # Parameters
    /// - `provider`: Provider to register.
    ///
    /// # Errors
    /// Returns [`MimeError::DuplicateDetectorName`] when the provider id or one
    /// of its aliases conflicts with an existing provider.
    pub fn register<P>(&mut self, provider: P) -> MimeResult<()>
    where
        P: MimeDetectorProvider + 'static,
    {
        self.register_arc(Arc::new(provider))
    }

    /// Registers a shared provider.
    ///
    /// # Parameters
    /// - `provider`: Shared provider to register.
    ///
    /// # Errors
    /// Returns [`MimeError::DuplicateDetectorName`] when the provider id or one
    /// of its aliases conflicts with an existing provider.
    pub fn register_arc(&mut self, provider: Arc<dyn MimeDetectorProvider>) -> MimeResult<()> {
        for name in provider_names(provider.as_ref()) {
            if self.find_provider(&name).is_some() {
                return Err(MimeError::DuplicateDetectorName { name });
            }
        }
        self.providers.push(provider);
        Ok(())
    }

    /// Gets canonical provider names in registration order.
    ///
    /// # Returns
    /// Provider ids.
    pub fn provider_names(&self) -> Vec<&'static str> {
        self.providers
            .iter()
            .map(|provider| provider.id())
            .collect()
    }

    /// Finds a provider by id or alias.
    ///
    /// # Parameters
    /// - `name`: Provider id or alias. Matching is case-insensitive.
    ///
    /// # Returns
    /// Matching provider, or `None`.
    pub fn find_provider(&self, name: &str) -> Option<&dyn MimeDetectorProvider> {
        self.providers
            .iter()
            .map(Arc::as_ref)
            .find(|provider| provider_matches(*provider, name))
    }

    /// Creates a detector from a provider name.
    ///
    /// # Parameters
    /// - `name`: Provider id or alias.
    /// - `config`: MIME configuration passed to the provider.
    ///
    /// # Returns
    /// Boxed MIME detector.
    ///
    /// # Errors
    /// Returns [`MimeError::UnknownDetector`] when no provider matches `name`,
    /// [`MimeError::DetectorUnavailable`] when the provider is unavailable, or
    /// another [`MimeError`] when provider initialization fails.
    pub fn create(&self, name: &str, config: &MimeConfig) -> MimeResult<BoxMimeDetector> {
        let provider = self
            .find_provider(name)
            .ok_or_else(|| MimeError::UnknownDetector {
                name: name.to_owned(),
            })?;
        match provider.availability(config) {
            MimeDetectorAvailability::Available => {
                provider.create(config).map(BoxMimeDetector::new)
            }
            MimeDetectorAvailability::Unavailable { reason } => {
                Err(MimeError::DetectorUnavailable {
                    name: name.to_owned(),
                    reason,
                })
            }
        }
    }

    /// Creates a detector from the configured default and fallback chain.
    ///
    /// # Parameters
    /// - `config`: MIME configuration.
    ///
    /// # Returns
    /// First detector that can be created.
    ///
    /// # Errors
    /// Returns [`MimeError::NoAvailableDetector`] when all configured providers
    /// are unknown, unavailable, or fail during initialization.
    pub fn create_default(&self, config: &MimeConfig) -> MimeResult<BoxMimeDetector> {
        let candidates = self.default_candidates(config);
        if candidates.is_empty() {
            return Err(MimeError::NoAvailableDetector {
                reason: "detector registry is empty".to_owned(),
            });
        }
        let mut errors = Vec::new();
        for candidate in candidates {
            match self.create(&candidate, config) {
                Ok(detector) => return Ok(detector),
                Err(error) => errors.push(error.to_string()),
            }
        }
        Err(MimeError::NoAvailableDetector {
            reason: errors.join("; "),
        })
    }

    /// Builds the default provider candidate chain.
    ///
    /// # Parameters
    /// - `config`: MIME configuration.
    ///
    /// # Returns
    /// Ordered provider names to try.
    fn default_candidates(&self, config: &MimeConfig) -> Vec<String> {
        let configured = config.mime_detector_default().trim();
        if configured.is_empty() || configured.eq_ignore_ascii_case("auto") {
            return self.auto_candidates(config);
        }
        let mut candidates = vec![configured.to_owned()];
        candidates.extend(config.mime_detector_fallbacks().iter().cloned());
        candidates
    }

    /// Builds provider candidates for automatic selection.
    ///
    /// # Parameters
    /// - `config`: MIME configuration.
    ///
    /// # Returns
    /// Available provider ids ordered by descending priority.
    fn auto_candidates(&self, config: &MimeConfig) -> Vec<String> {
        let mut providers: Vec<&dyn MimeDetectorProvider> = self
            .providers
            .iter()
            .map(Arc::as_ref)
            .filter(|provider| provider.availability(config).is_available())
            .collect();
        providers.sort_by(|left, right| {
            right
                .priority()
                .cmp(&left.priority())
                .then_with(|| left.id().cmp(right.id()))
        });
        providers
            .into_iter()
            .map(|provider| provider.id().to_owned())
            .collect()
    }
}

/// Gets all names exposed by a provider.
///
/// # Parameters
/// - `provider`: Provider to inspect.
///
/// # Returns
/// Provider id and aliases.
fn provider_names(provider: &dyn MimeDetectorProvider) -> Vec<String> {
    let mut names = Vec::with_capacity(provider.aliases().len() + 1);
    names.push(provider.id().to_owned());
    names.extend(provider.aliases().iter().map(|alias| (*alias).to_owned()));
    names
}

/// Tells whether a provider matches a requested name.
///
/// # Parameters
/// - `provider`: Provider to inspect.
/// - `name`: Requested id or alias.
///
/// # Returns
/// `true` when `name` matches the provider id or any alias.
fn provider_matches(provider: &dyn MimeDetectorProvider, name: &str) -> bool {
    provider.id().eq_ignore_ascii_case(name)
        || provider
            .aliases()
            .iter()
            .any(|alias| alias.eq_ignore_ascii_case(name))
}

/// Locks the default registry for reading.
///
/// # Returns
/// Read guard for the default registry.
///
/// # Errors
/// Returns [`MimeError::DetectorBackend`] when the global registry lock has
/// been poisoned by another thread.
#[cfg(not(coverage))]
fn read_default_registry() -> MimeResult<RwLockReadGuard<'static, MimeDetectorRegistry>> {
    match DEFAULT_MIME_DETECTOR_REGISTRY.read() {
        Ok(registry) => Ok(registry),
        Err(_) => Err(MimeError::DetectorBackend {
            backend: BACKEND.into(),
            reason: LOCK_ERR.into(),
        }),
    }
}

/// Locks the default registry for reading during coverage runs.
///
/// Poisoning cannot be triggered reliably through public behavior, so coverage
/// runs recover the guard and keep the public API path covered.
///
/// # Returns
/// Read guard for the default registry.
#[cfg(coverage)]
fn read_default_registry() -> MimeResult<RwLockReadGuard<'static, MimeDetectorRegistry>> {
    Ok(DEFAULT_MIME_DETECTOR_REGISTRY
        .read()
        .unwrap_or_else(PoisonError::into_inner))
}

/// Locks the default registry for writing.
///
/// # Returns
/// Write guard for the default registry.
///
/// # Errors
/// Returns [`MimeError::DetectorBackend`] when the global registry lock has
/// been poisoned by another thread.
#[cfg(not(coverage))]
fn write_default_registry() -> MimeResult<RwLockWriteGuard<'static, MimeDetectorRegistry>> {
    match DEFAULT_MIME_DETECTOR_REGISTRY.write() {
        Ok(registry) => Ok(registry),
        Err(_) => Err(MimeError::DetectorBackend {
            backend: BACKEND.into(),
            reason: LOCK_ERR.into(),
        }),
    }
}

/// Locks the default registry for writing during coverage runs.
///
/// Poisoning cannot be triggered reliably through public behavior, so coverage
/// runs recover the guard and keep the public API path covered.
///
/// # Returns
/// Write guard for the default registry.
#[cfg(coverage)]
fn write_default_registry() -> MimeResult<RwLockWriteGuard<'static, MimeDetectorRegistry>> {
    Ok(DEFAULT_MIME_DETECTOR_REGISTRY
        .write()
        .unwrap_or_else(PoisonError::into_inner))
}