Browse Source
This adds a subcommand to the `clif-util` CLI for exposing the Souper->Peepmatic translation machinery that was introduced in #2192.pull/2199/head
Nick Fitzgerald
4 years ago
committed by
GitHub
2 changed files with 68 additions and 0 deletions
@ -0,0 +1,32 @@ |
|||
use std::io::{Read, Write}; |
|||
use std::path::Path; |
|||
|
|||
pub fn run(input: &Path, output: &Path) -> Result<(), String> { |
|||
let peepmatic_dsl = if input == Path::new("-") { |
|||
let stdin = std::io::stdin(); |
|||
let mut stdin = stdin.lock(); |
|||
let mut souper_dsl = vec![]; |
|||
stdin |
|||
.read_to_end(&mut souper_dsl) |
|||
.map_err(|e| format!("failed to read from stdin: {}", e))?; |
|||
let souper_dsl = |
|||
String::from_utf8(souper_dsl).map_err(|e| format!("stdin is not UTF-8: {}", e))?; |
|||
peepmatic_souper::convert_str(&souper_dsl, Some(Path::new("stdin"))) |
|||
.map_err(|e| e.to_string())? |
|||
} else { |
|||
peepmatic_souper::convert_file(input).map_err(|e| e.to_string())? |
|||
}; |
|||
|
|||
if output == Path::new("-") { |
|||
let stdout = std::io::stdout(); |
|||
let mut stdout = stdout.lock(); |
|||
stdout |
|||
.write_all(peepmatic_dsl.as_bytes()) |
|||
.map_err(|e| format!("error writing to stdout: {}", e))?; |
|||
} else { |
|||
std::fs::write(output, peepmatic_dsl.as_bytes()) |
|||
.map_err(|e| format!("error writing to {}: {}", output.display(), e))?; |
|||
} |
|||
|
|||
Ok(()) |
|||
} |
Loading…
Reference in new issue