qubit_mime/classifier/media_stream_classifier_backend.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2026 Haixing Hu.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! Core media stream classifier backend contract.
11//!
12
13use std::fmt::Debug;
14use std::io::Read;
15use std::path::Path;
16
17use crate::MimeResult;
18
19use super::MediaStreamClassifier;
20use super::MediaStreamType;
21use super::media_stream_classifier_helpers::validate_readable_file;
22
23/// Core implementation contract for classifiers that can handle local files and streams.
24pub trait MediaStreamClassifierBackend: Debug + Send + Sync {
25 /// Classifies one validated local file.
26 ///
27 /// # Parameters
28 /// - `file`: Readable local media file.
29 ///
30 /// # Returns
31 /// Media stream classification.
32 ///
33 /// # Errors
34 /// Returns [`MimeError::Io`](crate::MimeError::Io) or another [`MimeError`](crate::MimeError)
35 /// when backend classification fails.
36 fn classify_by_local_file(&self, file: &Path) -> MimeResult<MediaStreamType>;
37
38 /// Classifies media bytes from a stream.
39 ///
40 /// # Parameters
41 /// - `reader`: Media stream to classify.
42 ///
43 /// # Returns
44 /// Media stream classification.
45 ///
46 /// # Errors
47 /// Returns [`MimeError::Io`](crate::MimeError::Io) or another [`MimeError`](crate::MimeError)
48 /// when backend classification fails.
49 fn classify_by_content(&self, reader: &mut dyn Read) -> MimeResult<MediaStreamType>;
50}
51
52impl<T> MediaStreamClassifier for T
53where
54 T: MediaStreamClassifierBackend,
55{
56 /// Validates the local file and delegates to the backend hook.
57 fn classify_file(&self, file: &Path) -> MimeResult<MediaStreamType> {
58 validate_readable_file(file)?;
59 self.classify_by_local_file(file)
60 }
61
62 /// Delegates stream classification to the backend hook.
63 fn classify_reader(&self, reader: &mut dyn Read) -> MimeResult<MediaStreamType> {
64 self.classify_by_content(reader)
65 }
66}