bfy/src/utils.rs

27 lines
813 B
Rust
Raw Normal View History

pub(crate) fn read_brainfuck_code_if_any(source: &Option<String>) -> Option<String> {
2022-10-07 19:54:49 +00:00
match source {
Some(source) => {
info!("Reading brainfuck source code from file: {}", source);
match std::fs::read_to_string(source) {
Ok(source) => Some(clean(source)),
2022-10-07 19:54:49 +00:00
Err(e) => {
error!("Failed to read source code file: {}", e);
eprintln!("Failed to read source code file: {}", e);
std::process::exit(1);
}
}
}
2022-10-07 23:06:35 +00:00
None => None,
2022-10-07 19:54:49 +00:00
}
2022-10-07 23:06:35 +00:00
}
fn clean(source: String) -> String {
source
.chars()
.filter(|c| match c {
'+' | '-' | '<' | '>' | '[' | ']' | '.' | ',' => true,
_ => false,
})
.collect()
}