diff --git a/crates/weaver_forge/codegen_examples/templates/registry/rust/README.md b/crates/weaver_forge/codegen_examples/templates/registry/rust/README.md new file mode 100644 index 00000000..e5f4ef77 --- /dev/null +++ b/crates/weaver_forge/codegen_examples/templates/registry/rust/README.md @@ -0,0 +1,20 @@ +# Semantic Conventions for Rust + +# Usage + +```rust +fn main() { + // Display the KeyValue of the attribute CLIENT_ADDRESS initialized with the value "145.34.23.56" + println!("{:?}", semconv::client::CLIENT_ADDRESS.value("145.34.23.56".into())); + // Display the key of the attribute CLIENT_ADDRESS + println!("{:?}", semconv::client::CLIENT_ADDRESS.key()); + + // Display the KeyValue of the attribute CLIENT_PORT initialized with the value 8080 + println!("{:?}", semconv::client::CLIENT_PORT.value(8080)); + // Display the key of the attribute CLIENT_PORT + println!("{:?}", semconv::client::CLIENT_PORT.key()); + + // Display the string representation of the enum variant HttpRequestMethod::Connect + println!("{}", semconv::http::HttpRequestMethod::Connect); +} +``` \ No newline at end of file diff --git a/crates/weaver_forge/codegen_examples/templates/registry/rust/semantic_attributes.rs.j2 b/crates/weaver_forge/codegen_examples/templates/registry/rust/semantic_attributes.rs.j2 index 3fe80687..a9000b34 100644 --- a/crates/weaver_forge/codegen_examples/templates/registry/rust/semantic_attributes.rs.j2 +++ b/crates/weaver_forge/codegen_examples/templates/registry/rust/semantic_attributes.rs.j2 @@ -36,6 +36,7 @@ use crate::AttributeKey; {% if attribute.type.allow_custom_values is defined %}pub const {{ attribute.name | screaming_snake_case }}: AttributeKey<{{ attribute.name | pascal_case }}> = AttributeKey::new("{{ attribute.name }}"); {% elif attribute.type == "string" %}pub const {{ attribute.name | screaming_snake_case }}: AttributeKey<{{ attribute.type | type_mapping }}Value> = AttributeKey::new("{{ attribute.name }}"); {% else %}pub const {{ attribute.name | screaming_snake_case }}: AttributeKey<{{ attribute.type | type_mapping }}> = AttributeKey::new("{{ attribute.name }}");{% endif %} + {% if attribute.type.allow_custom_values is defined %} {%- if attribute.brief %} {{ attribute.brief | comment_with_prefix("/// ") }} @@ -50,5 +51,29 @@ pub enum {{ attribute.name | pascal_case }} { {{ variant.id | pascal_case }}, {% endfor %} } + +impl {{ attribute.name | pascal_case }} { + /// Returns the string representation of the [`{{ attribute.name | pascal_case }}`]. + pub fn as_str(&self) -> &'static str { + match self { + {%- for variant in attribute.type.members %} + {%- if variant is experimental %} + #[cfg(feature = "semconv_experimental")] {% endif %} + {{ attribute.name | pascal_case }}::{{ variant.id | pascal_case }} => "{{ variant.value }}", + {%- endfor %} + /// Without this default case, the match expression would not + /// contain any variants if all variants are annotated with the + /// 'semconv_experimental' feature and the feature is not enabled. + _ => unreachable!(), + } + } +} + +impl core::fmt::Display for {{ attribute.name | pascal_case }} { + /// Formats the value using the given formatter. + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "{}", self.as_str()) + } +} {% endif %} {% endfor %} \ No newline at end of file diff --git a/crates/weaver_forge/codegen_examples/templates/registry/rust/weaver.yaml b/crates/weaver_forge/codegen_examples/templates/registry/rust/weaver.yaml index 7b3d872e..c451b703 100644 --- a/crates/weaver_forge/codegen_examples/templates/registry/rust/weaver.yaml +++ b/crates/weaver_forge/codegen_examples/templates/registry/rust/weaver.yaml @@ -8,6 +8,9 @@ type_mapping: template[string[]]: Vec # Not yet properly handled in codegen templates: + - pattern: README.md + filter: . + application_mode: single - pattern: lib.rs.j2 # The following JQ filter extracts the id, type, brief, and prefix of groups matching the following criteria: # - groups with an id starting with the prefix `registry.`