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