Skip to content

Commit

Permalink
feat(forge): Add enum string representation support.
Browse files Browse the repository at this point in the history
  • Loading branch information
lquerel committed Apr 30, 2024
1 parent 30876fc commit 9483d63
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -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("/// ") }}
Expand All @@ -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 %}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ type_mapping:
template[string[]]: Vec<String> # 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.`
Expand Down

0 comments on commit 9483d63

Please sign in to comment.