use crate::codegen::generators::{AsyncPattern, RustBindingConfig};
pub(crate) fn resolve_err_conv(cfg: &RustBindingConfig<'_>, error_type: Option<&str>) -> String {
if cfg.async_pattern == AsyncPattern::Pyo3FutureIntoPy
&& let Some(converters) = cfg.error_converters
&& let Some(error_type) = error_type
{
let short_name = error_type.rsplit("::").next().unwrap_or(error_type);
let candidate = format!("{}_to_py_err", crate::codegen::naming::pascal_to_snake(short_name));
if converters.iter().any(|c| c == &candidate) {
return format!(".map_err({candidate})");
}
}
match cfg.async_pattern {
AsyncPattern::Pyo3FutureIntoPy => {
".map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))".to_string()
}
AsyncPattern::NapiNativeAsync => {
".map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))".to_string()
}
AsyncPattern::WasmNativeAsync => ".map_err(|e| JsValue::from_str(&e.to_string()))".to_string(),
AsyncPattern::TokioBlockOn => {
".map_err(|e| extendr_api::Error::Other(e.to_string().replace(\":\", \"_\").replace(\"/\", \"_\").replace(\"-\", \"_\").chars().take(255).collect::<String>()))".to_string()
}
_ => ".map_err(|e| e.to_string())".to_string(),
}
}