use super::{pyi_docstring, python_safe_name, substitute_capsule_type};
use crate::backends::pyo3::type_map::python_type;
use crate::codegen::shared::substitute_excluded_types;
use crate::core::config::TraitBridgeConfig;
use crate::core::ir::ApiSurface;
pub(super) fn gen_visitor_protocol_stub(
bridge: &TraitBridgeConfig,
api: &ApiSurface,
capsule_names: &std::collections::HashSet<&str>,
emit_docstrings: bool,
) -> Option<String> {
let methods = bridge.resolve_methods(api);
if methods.is_empty() {
return None;
}
let trait_def = api.types.iter().find(|t| t.name == bridge.trait_name)?;
let excluded: std::collections::HashSet<&str> = api
.excluded_type_paths
.keys()
.map(String::as_str)
.chain(api.types.iter().filter(|t| t.binding_excluded).map(|t| t.name.as_str()))
.collect();
let mut lines = vec![format!("class {}(Protocol):", bridge.trait_name)];
if emit_docstrings {
if let Some(docstring) = pyi_docstring(&trait_def.doc, " ") {
lines.push(docstring);
}
}
let mut body_emitted = false;
for method in methods {
if method.binding_excluded {
continue;
}
body_emitted = true;
let mut params: Vec<String> = vec!["self".to_string()];
for p in &method.params {
let param_type = substitute_capsule_type(
&python_type(&substitute_excluded_types(&p.ty, &excluded)),
capsule_names,
);
params.push(format!("{}: {}", p.name, param_type));
}
let return_type = substitute_capsule_type(
&python_type(&substitute_excluded_types(&method.return_type, &excluded)),
capsule_names,
);
let safe_name = python_safe_name(&method.name);
let signature = format!(" def {}({}) -> {}: ...", safe_name, params.join(", "), return_type);
lines.push(signature);
}
if !body_emitted {
lines.push(" ...".to_string());
}
Some(lines.join("\n"))
}