summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKostya Shishkov <[email protected]>2020-02-17 16:29:32 +0100
committerKostya Shishkov <[email protected]>2020-02-17 16:29:32 +0100
commit1b5ebe1d28318556f667530d0437041b905170ce (patch)
tree9c26ca331a5fb6f7a0c74f521fa439175293bf08
parent6c9578bed2944ca6e21aec8d97c8a2e73b24b5fc (diff)
core: document detect module
-rw-r--r--nihav-core/src/detect.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/nihav-core/src/detect.rs b/nihav-core/src/detect.rs
index 926348a..a693124 100644
--- a/nihav-core/src/detect.rs
+++ b/nihav-core/src/detect.rs
@@ -1,14 +1,41 @@
+//! Container format detection.
+//!
+//! Usually user does not know the container format of the opened file.
+//! That is why format detection functionality is needed.
+//! This module contains the set of rules to detect container not merely by file extension but also by its content if possible.
+//!
+//! # Examples
+//!
+//! ```no_run
+//! use nihav_core::detect::detect_format;
+//! use std::fs::File;
+//! use nihav_core::io::byteio::*;
+//!
+//! let name = "mediafile.ogv";
+//! let mut file = File::open(name).unwrap();
+//! let mut filereader = FileReader::new_read(&mut file);
+//! let mut br = ByteReader::new(&mut filereader);
+//! let result = detect_format(name, &mut br);
+//! if let Some((name, score)) = result {
+//! println!("detected format {} with score {:?}", name, score);
+//! }
+//! ```
use std::io::SeekFrom;
use crate::io::byteio::ByteReader;
+/// Format detection score.
#[derive(Debug,Clone,Copy,PartialEq)]
pub enum DetectionScore {
+ /// Format is not detected.
No,
+ /// Format matched by file extension.
ExtensionMatches,
+ /// Format matches by markers inside the file.
MagicMatches,
}
impl DetectionScore {
+ /// Checks whether current detection score is less than a value it is compared against.
pub fn less(self, other: DetectionScore) -> bool {
(self as i32) < (other as i32)
}
@@ -223,6 +250,11 @@ const DETECTORS: &[DetectConditions] = &[
},
];
+/// Tries to detect container format.
+///
+/// This function tries to determine container format using both file extension and checking against container specific markers inside.
+/// In case of success the function returns short container name and the detection score.
+/// Result should have the highest detection score among tested.
pub fn detect_format(name: &str, src: &mut ByteReader) -> Option<(&'static str, DetectionScore)> {
let mut result = None;
let lname = name.to_lowercase();