File: inspect.rs

package info (click to toggle)
rust-content-inspector 0.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 156 kB
  • sloc: python: 15; makefile: 2
file content (36 lines) | stat: -rw-r--r-- 755 bytes parent folder | download | duplicates (21)
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
extern crate content_inspector;

use std::env;
use std::fs::File;
use std::io::{Error, Read};
use std::path::Path;
use std::process::exit;

const MAX_PEEK_SIZE: usize = 1024;

fn main() -> Result<(), Error> {
    let mut args = env::args();

    if args.len() < 2 {
        eprintln!("USAGE: inspect FILE [FILE...]");
        exit(1);
    }

    args.next();

    for filename in args {
        if !Path::new(&filename).is_file() {
            continue;
        }

        let file = File::open(&filename)?;
        let mut buffer: Vec<u8> = vec![];

        file.take(MAX_PEEK_SIZE as u64).read_to_end(&mut buffer)?;

        let content_type = content_inspector::inspect(&buffer);
        println!("{}: {}", filename, content_type);
    }

    Ok(())
}