Move iterate_files to the utils module
This commit is contained in:
@@ -1,13 +1,12 @@
|
|||||||
//! CLI tool to compile Cranelift IR files to native code in memory and execute them.
|
//! CLI tool to compile Cranelift IR files to native code in memory and execute them.
|
||||||
|
|
||||||
use crate::utils::read_to_string;
|
use crate::utils::{iterate_files, read_to_string};
|
||||||
use cranelift_codegen::isa::{CallConv, TargetIsa};
|
use cranelift_codegen::isa::{CallConv, TargetIsa};
|
||||||
use cranelift_filetests::SingleFunctionCompiler;
|
use cranelift_filetests::SingleFunctionCompiler;
|
||||||
use cranelift_native::builder as host_isa_builder;
|
use cranelift_native::builder as host_isa_builder;
|
||||||
use cranelift_reader::{parse_run_command, parse_test, Details, IsaSpec, ParseOptions};
|
use cranelift_reader::{parse_run_command, parse_test, Details, IsaSpec, ParseOptions};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use target_lexicon::Triple;
|
use target_lexicon::Triple;
|
||||||
use walkdir::WalkDir;
|
|
||||||
|
|
||||||
pub fn run(files: Vec<String>, flag_print: bool) -> Result<(), String> {
|
pub fn run(files: Vec<String>, flag_print: bool) -> Result<(), String> {
|
||||||
let stdin_exist = files.iter().find(|file| *file == "-").is_some();
|
let stdin_exist = files.iter().find(|file| *file == "-").is_some();
|
||||||
@@ -54,29 +53,6 @@ pub fn run(files: Vec<String>, flag_print: bool) -> Result<(), String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterate over all of the files passed as arguments, recursively iterating through directories
|
|
||||||
fn iterate_files(files: Vec<String>) -> impl Iterator<Item = PathBuf> {
|
|
||||||
files
|
|
||||||
.into_iter()
|
|
||||||
.flat_map(WalkDir::new)
|
|
||||||
.filter(|f| match f {
|
|
||||||
Ok(d) => {
|
|
||||||
// filter out hidden files (starting with .)
|
|
||||||
!d.file_name().to_str().map_or(false, |s| s.starts_with('.'))
|
|
||||||
// filter out directories
|
|
||||||
&& !d.file_type().is_dir()
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
println!("Unable to read file: {}", e);
|
|
||||||
false
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.map(|f| {
|
|
||||||
f.expect("This should not happen: we have already filtered out the errors")
|
|
||||||
.into_path()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Run all functions in a file that are succeeded by "run:" comments
|
/// Run all functions in a file that are succeeded by "run:" comments
|
||||||
fn run_single_file(path: &PathBuf) -> Result<(), String> {
|
fn run_single_file(path: &PathBuf) -> Result<(), String> {
|
||||||
let file_contents = read_to_string(&path).map_err(|e| e.to_string())?;
|
let file_contents = read_to_string(&path).map_err(|e| e.to_string())?;
|
||||||
|
|||||||
@@ -6,9 +6,10 @@ use cranelift_codegen::settings::{self, FlagsOrIsa};
|
|||||||
use cranelift_reader::{parse_options, Location, ParseError, ParseOptionError};
|
use cranelift_reader::{parse_options, Location, ParseError, ParseOptionError};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, Read};
|
use std::io::{self, Read};
|
||||||
use std::path::Path;
|
use std::path::{Path, PathBuf};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use target_lexicon::Triple;
|
use target_lexicon::Triple;
|
||||||
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
/// Read an entire file into a string.
|
/// Read an entire file into a string.
|
||||||
pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
|
pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
|
||||||
@@ -105,3 +106,26 @@ pub fn parse_sets_and_triple(
|
|||||||
Ok(OwnedFlagsOrIsa::Flags(settings::Flags::new(flag_builder)))
|
Ok(OwnedFlagsOrIsa::Flags(settings::Flags::new(flag_builder)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Iterate over all of the files passed as arguments, recursively iterating through directories.
|
||||||
|
pub fn iterate_files(files: Vec<String>) -> impl Iterator<Item = PathBuf> {
|
||||||
|
files
|
||||||
|
.into_iter()
|
||||||
|
.flat_map(WalkDir::new)
|
||||||
|
.filter(|f| match f {
|
||||||
|
Ok(d) => {
|
||||||
|
// Filter out hidden files (starting with .).
|
||||||
|
!d.file_name().to_str().map_or(false, |s| s.starts_with('.'))
|
||||||
|
// Filter out directories.
|
||||||
|
&& !d.file_type().is_dir()
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
println!("Unable to read file: {}", e);
|
||||||
|
false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.map(|f| {
|
||||||
|
f.expect("this should not happen: we have already filtered out the errors")
|
||||||
|
.into_path()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user