use super::*;
pub(super) struct RenderedSnippetBody {
pub body: String,
pub used_placeholder_sample_url: bool,
}
pub(super) fn render_snippet_body(
extensions: &[Box<dyn crate::Extension>],
generator: &dyn E2eCodegen,
fixture: &Fixture,
language: &str,
context: &SnippetRenderContext<'_>,
sample_base_url: DocsSampleBaseUrl<'_>,
) -> Result<RenderedSnippetBody> {
let docs_fixture = fixture.docs_call_fixture_with_sample_base_url(sample_base_url.base());
for extension in extensions {
if let Some(body) = extension
.render_e2e_snippet(
&docs_fixture,
context.e2e,
context.crate_config,
language,
context.type_defs,
context.enums,
)
.map_err(|error| anyhow::anyhow!("extension `{}` could not render snippet: {error:#}", extension.name()))?
{
if body.trim().is_empty() {
bail!("extension `{}` returned an empty snippet body", extension.name());
}
mock_harness_guard::reject_mock_harness_scaffolding(&body, &docs_fixture, language)?;
return Ok(rendered(body, sample_base_url));
}
}
let call = context.e2e.resolve_call_for_fixture(
docs_fixture.call.as_deref(),
&docs_fixture.id,
&docs_fixture.resolved_category(),
&docs_fixture.tags,
&docs_fixture.input,
);
let docs_fixture = mock_url_defaults::with_default_mock_url_literals(docs_fixture, call, sample_base_url);
let fixture = &docs_fixture;
if let Some(kind) = recipe_policy::extension_owned_recipe_kind(fixture, fixture.resolved_args(call)) {
bail!("{kind} fixture requires an extension-owned documentation recipe");
}
let effective_function = call
.effective_function(language)
.or_else(|| {
let skipped_for_language = fixture.skip.as_ref().is_some_and(|skip| skip.should_skip(language));
(!skipped_for_language && crate::e2e::fixture::canonical_language(language) == "c")
.then(|| crate::e2e::codegen::recipe::trait_bridge_function_identity(context.crate_config, fixture))
.flatten()
})
.unwrap_or_default();
if effective_function.trim().is_empty() {
bail!(
"built-in `{language}` snippet recipe has no function identity; configure a call function or provide an extension-owned documentation recipe"
);
}
let body = generator
.render_snippet_body_with_functions(
fixture,
context.e2e,
context.crate_config,
context.type_defs,
context.enums,
context.functions,
context.errors,
)
.map_err(|error| anyhow::anyhow!("built-in `{language}` snippet recipe is incompatible: {error:#}"))?;
if body.trim().is_empty() {
bail!("built-in `{language}` snippet recipe returned an empty body");
}
mock_harness_guard::reject_mock_harness_scaffolding(&body, fixture, language)?;
Ok(rendered(body, sample_base_url))
}
fn rendered(body: String, sample_base_url: DocsSampleBaseUrl<'_>) -> RenderedSnippetBody {
let used_placeholder_sample_url = sample_base_url.is_placeholder() && body.contains(sample_base_url.base());
RenderedSnippetBody {
body,
used_placeholder_sample_url,
}
}
const PLACEHOLDER_SAMPLE_URL_FIXTURES_NAMED: usize = 10;
pub(super) fn report_placeholder_sample_urls(occurrences: &[(String, String)], sample_base_url: DocsSampleBaseUrl<'_>) {
if occurrences.is_empty() {
return;
}
let named = occurrences
.iter()
.take(PLACEHOLDER_SAMPLE_URL_FIXTURES_NAMED)
.map(|(fixture_id, language)| {
format!(
"{fixture_id} ({language}, acknowledge with {})",
WarningAcknowledgement::config_entry_for(
AcknowledgeableWarningCategory::DocSnippetReservedDomain,
fixture_id,
language,
)
)
})
.collect::<Vec<_>>()
.join(", ");
let remaining = occurrences.len().saturating_sub(PLACEHOLDER_SAMPLE_URL_FIXTURES_NAMED);
tracing::warn!(
target: "alef::e2e::snippets",
fixtures = occurrences.len(),
base_url = sample_base_url.base(),
config_key = crate::core::config::e2e::SAMPLE_BASE_URL_CONFIG_KEY,
"{} documentation snippet fixture/language occurrence(s) publish the reserved placeholder \
address `{}`, which serves nothing: a reader who copies them gets a request that cannot \
succeed. Set `{}` to a host that really serves your sample inputs, or acknowledge a \
specific fixture/language pair below -- a stale acknowledgement (one that matches \
nothing) fails the run. Affected: {named}{}",
occurrences.len(),
sample_base_url.base(),
crate::core::config::e2e::SAMPLE_BASE_URL_CONFIG_KEY,
if remaining > 0 {
format!(" (+{remaining} more)")
} else {
String::new()
}
);
}
pub(super) fn report_acknowledged_warnings(report: &crate::core::warning_ack::AcknowledgementReport) {
if report.matched_count == 0 {
return;
}
tracing::info!(
target: "alef::e2e::snippets",
matched = report.matched_count,
"{} documentation snippet warning occurrence(s) acknowledged via \
[crates.e2e.snippets].acknowledged_warnings: {}",
report.matched_count,
report.matched_entries.join(", ")
);
}