use std::path::PathBuf;
use serde::de::DeserializeOwned;
pub fn resolve_source<T, F>(
simple: Option<String>,
inline: Option<String>,
file: Option<PathBuf>,
python_inline: Option<String>,
python_file: Option<PathBuf>,
parse_simple: F,
) -> Result<T, crate::error::Error>
where
T: DeserializeOwned,
F: FnOnce(String) -> T,
{
if let Some(text) = simple {
return Ok(parse_simple(text));
}
if let Some(inline) = inline {
let mut de = serde_json::Deserializer::from_str(&inline);
return serde_path_to_error::deserialize(&mut de)
.map_err(crate::error::Error::InlineDeserialize);
}
if let Some(path) = file {
let bytes = std::fs::read(&path)
.map_err(|e| crate::error::Error::PromptFileRead(path.clone(), e))?;
let mut de = serde_json::Deserializer::from_slice(&bytes);
return serde_path_to_error::deserialize(&mut de)
.map_err(crate::error::Error::InlineDeserialize);
}
if let Some(code) = python_inline {
return crate::python::exec_code(&code);
}
if let Some(path) = python_file {
return crate::python::exec_file(&path);
}
unreachable!("clap group ensures one of the five flags is set")
}