use super::ir::{Attribute, TelemetryIR};
fn const_name(key: &str) -> String {
let mut out = String::with_capacity(key.len());
for ch in key.chars() {
if ch.is_ascii_alphanumeric() {
out.push(ch.to_ascii_uppercase());
} else {
out.push('_');
}
}
if out.is_empty() {
return "_".to_string();
}
if out.starts_with(|c: char| c.is_ascii_digit()) {
out.insert(0, '_');
}
out
}
fn doc_line(brief: &str) -> String {
brief.replace(['\n', '\r'], " ")
}
fn escape_lit(s: &str) -> String {
s.replace('\\', "\\\\").replace('"', "\\\"")
}
pub fn render_rust(ir: &TelemetryIR, header: &str) -> String {
let mut s = rust_header(ir, header);
for a in ir.all_attributes() {
s.push_str(&emit_rust(a));
}
s
}
fn rust_header(ir: &TelemetryIR, header: &str) -> String {
format!(
"{}//! OpenTelemetry attribute keys projected from model `{}`.\n\
//! Model hash: {}. Do not edit by hand.\n\n",
line_comment(header, "//"),
escape_lit(&ir.model_ref),
ir.model_hash
)
}
fn emit_rust(a: &Attribute) -> String {
format!(
"/// {}\npub const {}: &str = \"{}\";\n",
doc_line(&a.brief),
const_name(&a.key),
escape_lit(&a.key)
)
}
pub fn render_python(ir: &TelemetryIR, header: &str) -> String {
let mut s = format!(
"{}\"\"\"OpenTelemetry attribute keys projected from model `{}`.\n\n\
Model hash: {}. Do not edit by hand.\n\"\"\"\n\n",
line_comment(header, "#"),
escape_lit(&ir.model_ref),
ir.model_hash
);
for a in ir.all_attributes() {
s.push_str(&format!(
"# {}\n{}: str = \"{}\"\n",
doc_line(&a.brief),
const_name(&a.key),
escape_lit(&a.key)
));
}
s
}
pub fn render_typescript(ir: &TelemetryIR, header: &str) -> String {
let mut s = format!(
"{}/**\n * OpenTelemetry attribute keys projected from model `{}`.\n \
* Model hash: {}. Do not edit by hand.\n */\n\n",
line_comment(header, "//"),
escape_lit(&ir.model_ref),
ir.model_hash
);
for a in ir.all_attributes() {
s.push_str(&format!(
"/** {} */\nexport const {} = \"{}\";\n",
doc_line(&a.brief),
const_name(&a.key),
escape_lit(&a.key)
));
}
s
}
fn line_comment(header: &str, marker: &str) -> String {
let mut out = String::new();
for line in header.lines() {
let body = line
.strip_prefix("# ")
.or_else(|| line.strip_prefix('#'))
.unwrap_or(line);
if body.is_empty() {
out.push_str(marker);
out.push('\n');
} else {
out.push_str(marker);
out.push(' ');
out.push_str(body);
out.push('\n');
}
}
out
}