Add docs for new type enum variants to generated settings - #266
Add docs for new type enum variants to generated settings#266MaxFreedomPollard wants to merge 2 commits into
Conversation
`#[settings]` on an enum emitted an empty `Settings` impl, so `add_docs` fell back to the no-op default in foundations/src/settings/mod.rs. A new type variant is the one variant kind that gets a key of its own in the serialized YAML, and everything under that key was written without its doc comments. `TracingSettings::output` defaults to `TracesOutput::JaegerThriftUdp` (foundations/src/telemetry/settings/tracing.rs:80), so a config written by `Cli`'s `--generate` listed `server_addr`, `reporter_bind_addr`, `num_tasks` and `max_batch_size` with no documentation at all. Same for `UserTracesOutput::OtlpUds` and `SamplingStrategy::Active`. `expand_enum` now builds an `add_docs` that matches on the variants and, for each new type variant serde actually serializes, pushes the variant's key and forwards to the wrapped value before recording the variant's own doc comment. Unit variants serialize as a bare value and have no key to document. `serde(skip)` variants are left out so the type they wrap does not have to implement `Settings`, which `LogOutput::Custom` (an `Arc<dyn Drain>`) does not.
|
Hi Max, thanks for the PR! This is a known issue with the The problem with fixing this is that its a breaking change: Right now, the inner type of a newtype variant does not need to
I'm fine with any of these options, so I'll leave it up to you to pick. |
Documenting a new type variant makes the wrapped type reachable through add_docs, so that type now has to implement Settings. That is a new requirement on existing code, so the generated impl stays behind --cfg foundations_unstable until the next breaking release. Without the flag the macro emits the same empty impl as before.
|
Went with the cfg flag, thanks for laying out the options. The generated Tested both ways: 18 macro tests / 12 integration tests without the flag, 19 / 13 with it. |
#[settings]on an enum throws away the documentation of everything the enum wraps.expand_enumemits a bareimpl Settings for #ident { }(foundations-macros/src/settings.rs:136), soadd_docsfalls back to the no-op default at foundations/src/settings/mod.rs:418. A new type variant is the one variant kind that gets a key of its own in the YAML, and every field under that key comes out with no comments.This hits foundations' own settings.
TracingSettings::outputdefaults toTracesOutput::JaegerThriftUdp(foundations/src/telemetry/settings/tracing.rs:84), which wrapsJaegerThriftUdpOutputSettings, whose four fields all have doc comments. Here is whatto_yaml_string(&TracingSettings::default())prints today, which is what a service writes out withCli's--generate:UserTracesOutput::OtlpUds(user_tracing.rs:56) andSamplingStrategy::Active(tracing.rs:222) lose their documentation the same way, and both are the default variant of their enum.With this change the same call prints:
expand_enumnow generates anadd_docsthat matches on the variants. For each new type variant that serde serializes it pushes the variant's key, forwards to the wrapped value, then records the variant's own doc comment, which is what the macro already does for a struct field. Unit variants serialize as a bare value, so there is no line to attach a comment to and their arm is empty.serde(skip)variants are left out as well, so the type behind one still does not need to implementSettings:LogOutput::Customwraps anArc<dyn Drain>and would otherwise stop compiling. The key honoursserde(rename = "...")and otherwise follows therename_all = "snake_case"the macro puts on every settings enum. Generated code is a match rather than oneif letper variant becauseif lettripsirrefutable_let_patternson a single variant enum, which is whatTracesOutputis withouttelemetry-otlp-grpc.Verified on macOS with cargo 1.95.0.
cargo test -p foundations --no-default-features --features settings --test settings, and the same with--features settings,serde-saphyr: 13 passed, 0 failed on both. The newenum_new_type_variant_fieldstest fails on main with the four comment lines missing from the output and passes here, and there is a golden file for each YAML backend. No existing golden file changed.cargo test -p foundations-macros: 69 passed, 0 failed. Three existingexpand_enum*expansion tests needed their expected output updated, andexpand_enum_with_variant_docsis new and covers a plain new type variant, a renamed one, a skipped one wrapping a type that is notSettings, and a unit variant.RUSTFLAGS=-Dwarnings cargo check -p foundations --no-default-features --features settings,client-telemetry,user-tracing,telemetry-otlp-grpc: clean. That is the set that coversLogOutput,TracesOutput,UserTracesOutput,SamplingStrategyandServiceNameFormat.cargo clippywith the flags from AGENTS.md on-p foundations-macros --all-targetsand on-p foundations --no-default-features --features settings --lib --test settings: clean.rustfmt --check --edition 2024on both changed Rust files: clean.I left RELEASE_NOTES.md alone since git-cliff generates it from commit subjects at release time.