// // spec.rs // Copyright (C) 2023 Maxime “pep” Buquet // Distributed under terms of the GPLv3+ license. // use std::env::args; use std::fs::File; use std::io::{self, Read}; use std::path::{Path, PathBuf}; use scansion_dsl::parse_spec; fn main() -> io::Result<()> { let args: Vec = args().collect(); if args.len() != 2 { println!("Usage: {} ", args[0]); return Err(io::Error::new( io::ErrorKind::Other, "Invalid argument count", )); } let path = Path::new(&args[1]); if !path.exists() { return Err(io::Error::new( io::ErrorKind::Other, "Specified path doesn't exist", )); } let files: Vec = if path.is_file() { vec![path.to_path_buf()] } else if path.is_dir() { path.read_dir()? .filter_map(|entry| { let p = entry.unwrap().path(); match p.extension() { Some(ext) if ext == "scs" => Some(p), _ => None, } }) .collect::>() } else { return Err(io::Error::new( io::ErrorKind::Other, "Only files and dirs are supported", )); }; for path in files { let mut file = File::open(path.clone())?; let mut contents = String::new(); file.read_to_string(&mut contents)?; print!("Path: {path:?}: "); match parse_spec(&contents) { Ok(_) => println!("\x1b[32m OK\x1b[0m"), Err(err) => println!("\x1b[31mERR\x1b[0m\n{err:?}"), } } Ok(()) }