use std::ffi::OsStr;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::path::PathBuf;
use std::str::FromStr;
use argh::FromArgs;
use cros_codecs::DecodedFormat;
use cros_codecs::EncodedFormat;
use cros_codecs::FrameMemoryType;
#[derive(Debug, Eq, PartialEq)]
pub enum Md5Computation {
Stream,
Frame,
}
impl FromStr for Md5Computation {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"stream" => Ok(Md5Computation::Stream),
"frame" => Ok(Md5Computation::Frame),
_ => Err("unrecognized MD5 computation option. Valid values: stream, frame"),
}
}
}
#[derive(Debug, FromArgs)]
pub struct Args {
#[argh(positional)]
pub input: PathBuf,
#[argh(option)]
pub output: Option<PathBuf>,
#[argh(switch)]
pub multiple_output_files: bool,
#[argh(option)]
pub input_format: EncodedFormat,
#[argh(option, default = "DecodedFormat::I420")]
pub output_format: DecodedFormat,
#[allow(dead_code)]
#[argh(option, default = "FrameMemoryType::Managed")]
pub frame_memory: FrameMemoryType,
#[allow(dead_code)]
#[argh(option)]
pub gbm_device: Option<PathBuf>,
#[argh(option)]
#[allow(dead_code)]
pub libva_device: Option<PathBuf>,
#[argh(option)]
pub compute_md5: Option<Md5Computation>,
#[argh(option)]
pub golden: Option<PathBuf>,
}
pub fn decide_output_file_name<'a>(output: &'a Path, index: i32) -> PathBuf {
let extract_str = |s: Option<&'a OsStr>| s.and_then(|s| s.to_str()).expect("malformed file");
let [file_name, stem] = [output.file_name(), output.file_stem()].map(extract_str);
if output.extension().is_some() {
let [extension] = [output.extension()].map(extract_str);
let new_file_name = format!("{}_{}.{}", stem, index, extension);
PathBuf::from(String::from(output.to_str().unwrap()).replace(file_name, &new_file_name))
} else {
let new_file_name = format!("{}_{}", stem, index);
PathBuf::from(String::from(output.to_str().unwrap()).replace(file_name, &new_file_name))
}
}
pub fn golden_md5s(path: &Option<PathBuf>) -> Vec<String> {
let golden_md5s: Vec<String> = match path {
None => vec![],
Some(ref path) => {
let mut golden_file_content = String::new();
File::open(path)
.expect("error opening golden file")
.read_to_string(&mut golden_file_content)
.expect("error reading golden file");
let parsed_json: serde_json::Value =
serde_json::from_str(&golden_file_content).expect("error parsing golden file");
match &parsed_json["md5_checksums"] {
serde_json::Value::Array(checksums) => checksums
.iter()
.map(|x| match x {
serde_json::Value::String(checksum) => String::from(checksum),
_ => panic!("error parsing golden file"),
})
.collect(),
_ => panic!("error parsing golden file"),
}
}
};
golden_md5s
}