### Inspect Files with Rust Content Inspector Source: https://context7.com/sharkdp/content_inspector/llms.txt This Rust code snippet demonstrates how to inspect files using the `content_inspector` crate. It opens a file, reads the first 1024 bytes into a buffer, and then uses the `inspect` function to determine the content type. The example iterates through a list of filenames, checks if they exist, inspects their content, and prints the detected type along with whether it can be displayed as text. ```rust use content_inspector::{ContentType, inspect}; use std::fs::File; use std::io::{Error, Read}; use std::path::Path; const MAX_PEEK_SIZE: usize = 1024; fn inspect_file(filename: &str) -> Result { let file = File::open(filename)?; let mut buffer: Vec = vec![]; // Read only first 1024 bytes for performance file.take(MAX_PEEK_SIZE as u64).read_to_end(&mut buffer)?; Ok(inspect(&buffer)) } fn main() -> Result<(), Error> { let files = vec!["src/main.rs", "image.png", "document.pdf"]; for filename in files { if Path::new(filename).is_file() { let content_type = inspect_file(filename)?; println!("{}: {}", filename, content_type); if content_type.is_text() { // Process as text file println!(" -> Can be displayed as text"); } else { // Handle as binary file println!(" -> Binary content, handle accordingly"); } } } Ok(()) } // Example output: // src/main.rs: UTF-8 // -> Can be displayed as text // image.png: binary // -> Binary content, handle accordingly // document.pdf: binary // -> Binary content, handle accordingly ``` -------------------------------- ### Inspect Binary Buffer Content Type in Rust Source: https://github.com/sharkdp/content_inspector/blob/master/README.md Demonstrates how to use the `content_inspector` library in Rust to determine the content type of a byte slice. It checks for UTF-8 and binary content, and shows how to verify if the content is text. ```rust use content_inspector::{ContentType, inspect}; assert_eq!(ContentType::UTF_8, inspect(b"Hello")); assert_eq!(ContentType::BINARY, inspect(b"\xFF\xE0\x00\x10\x4A\x46\x49\x46\x00")); assert!(inspect(b"Hello").is_text()); ``` -------------------------------- ### Inspect Buffer Content Type using Rust's inspect Function Source: https://context7.com/sharkdp/content_inspector/llms.txt The `inspect` function in Rust takes a byte slice and returns the detected `ContentType`. It utilizes BOM detection, NULL byte scanning, and magic number matching for formats like PDF and PNG. Handles various text encodings, binary data, and empty buffers. ```rust use content_inspector::{ContentType, inspect}; // Basic text detection let text_buffer = b"Hello, World!"; let result = inspect(text_buffer); assert_eq!(result, ContentType::UTF_8); // Binary detection (JPEG header with NULL bytes) let binary_buffer = b"\xFF\xE0\x00\x10\x4A\x46\x49\x46\x00"; let result = inspect(binary_buffer); assert_eq!(result, ContentType::BINARY); // UTF-8 with BOM detection let utf8_bom = b"\xEF\xBB\xBFHello"; let result = inspect(utf8_bom); assert_eq!(result, ContentType::UTF_8_BOM); // UTF-16 Little Endian detection let utf16le = b"\xFF\xFEH\x00e\x00l\x00l\x00o\x00"; let result = inspect(utf16le); assert_eq!(result, ContentType::UTF_16LE); // Empty buffer returns UTF-8 let empty: &[u8] = b""; assert_eq!(inspect(empty), ContentType::UTF_8); // PDF detection (magic number) let pdf_buffer = b"%PDF-1.4 some content"; assert_eq!(inspect(pdf_buffer), ContentType::BINARY); // PNG detection (magic number) let png_buffer = b"\x89PNG\r\n\x1a\n"; assert_eq!(inspect(png_buffer), ContentType::BINARY); ``` -------------------------------- ### Check if ContentType is Text using is_text() in Rust Source: https://context7.com/sharkdp/content_inspector/llms.txt The `is_text()` method, available on the `ContentType` enum in Rust, returns `true` if the content is any recognized text encoding (UTF-8, UTF-16LE, etc.) and `false` if it's binary. This is useful for conditionally processing file content. ```rust use content_inspector::{ContentType, inspect}; let text_content = b"Some text content"; let binary_content = b"\x00\x01\x02\x03"; if inspect(text_content).is_text() { println!("Content is text, safe to display"); } if !inspect(binary_content).is_text() { println!("Content is binary, handle accordingly"); } // All text types return true for is_text() assert!(ContentType::UTF_8.is_text()); assert!(ContentType::UTF_8_BOM.is_text()); assert!(ContentType::UTF_16LE.is_text()); assert!(ContentType::UTF_16BE.is_text()); assert!(ContentType::UTF_32LE.is_text()); assert!(ContentType::UTF_32BE.is_text()); assert!(!ContentType::BINARY.is_text()); ``` -------------------------------- ### Define ContentType Enum for Buffer Inspection in Rust Source: https://context7.com/sharkdp/content_inspector/llms.txt The `ContentType` enum in Rust categorizes buffer content as binary or specific text encodings (UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE) identified by BOMs. It also supports display formatting for easy output. ```rust use content_inspector::ContentType; // Available content types: // ContentType::BINARY - Binary data (contains NULL bytes or magic numbers) // ContentType::UTF_8 - UTF-8 encoded text (no BOM) // ContentType::UTF_8_BOM - UTF-8 encoded text with BOM (0xEF, 0xBB, 0xBF) // ContentType::UTF_16LE - UTF-16 Little Endian (BOM: 0xFF, 0xFE) // ContentType::UTF_16BE - UTF-16 Big Endian (BOM: 0xFE, 0xFF) // ContentType::UTF_32LE - UTF-32 Little Endian (BOM: 0xFF, 0xFE, 0x00, 0x00) // ContentType::UTF_32BE - UTF-32 Big Endian (BOM: 0x00, 0x00, 0xFE, 0xFF) // Display formatting let content_type = ContentType::UTF_16LE; println!("{}", content_type); // Output: "UTF-16LE" ``` -------------------------------- ### Check if ContentType is Binary using is_binary() in Rust Source: https://context7.com/sharkdp/content_inspector/llms.txt The `is_binary()` method for the `ContentType` enum in Rust returns `true` specifically when the content is classified as `BINARY`. This method complements `is_text()` and is useful for explicit binary content detection. ```rust use content_inspector::{ContentType, inspect}; let jpeg_header = b"\xFF\xD8\xFF\xE0\x00\x10JFIF\x00"; let source_code = b"fn main() { println!(\"Hello\"); }"; if inspect(jpeg_header).is_binary() { println!("Binary file detected, skipping text processing"); } assert!(ContentType::BINARY.is_binary()); assert!(!ContentType::UTF_8.is_binary()); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.