Skip to content

Commit

Permalink
docs, changelog, struct/variant tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ModProg committed Mar 7, 2025
1 parent 1b5a483 commit d531b16
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 10 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Added

- Support `#[display(rename_all = "<casing>")]` to change output for implicit
naming of enum variants whend deriving `Display`
([#443](https://github.com/JelteF/derive_more/pull/443)).

## 2.0.1 - 2025-02-03

### Added
Expand Down
33 changes: 33 additions & 0 deletions impl/doc/display.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ For enums, you can either specify it on each variant, or on the enum as a whole.

For variants that don't have a format specified, it will simply defer to the format of the
inner variable. If there is no such variable, or there is more than 1, an error is generated.
The only exception are unit variants, for these the name of the variant will be used if no
format is specified. This can be controlled with the [`#[display(rename_all = "...")]`](#The-`rename_all`-attribute).



Expand Down Expand Up @@ -224,6 +226,37 @@ assert_eq!(Enum::B(2).to_string(), "Variant: 2 & 2");
assert_eq!(Enum::C.to_string(), "c");
```

### The `rename_all` attribute

When no format is specified, deriving `Display` uses the variant name verbatim as format.
To control this the `#[display(rename_all)]` can be placed on structs, enums and variants.

The availible casings are:

- `lowercase`
- `UPPERCASE`
- `PascalCase`
- `camelCase`
- `snake_case`
- `SCREAMING_SNAKE_CASE`
- `kebab-case`
- `SCREAMING-KEBAB-CASE`

```rust
# use derive_more::Display;
#
#[derive(Display)]
#[display(rename_all = "lowercase")]
enum Enum {
VariantOne,
#[display(rename_all = "kebab-case")]
VariantTwo
}

assert_eq!(Enum::VariantOne.to_string(), "variantone");
assert_eq!(Enum::VariantTwo.to_string(), "variant-two");
```




Expand Down
2 changes: 1 addition & 1 deletion impl/src/fmt/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ enum RenameAllAttribute {
impl RenameAllAttribute {
fn convert_case(&self, ident: &syn::Ident) -> String {
let case = match self {
Self::Lower => convert_case::Case::Lower,
Self::Lower => convert_case::Case::Flat,
Self::Upper => convert_case::Case::UpperFlat,
Self::Pascal => convert_case::Case::Pascal,
Self::Camel => convert_case::Case::Camel,
Expand Down
50 changes: 41 additions & 9 deletions tests/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1803,17 +1803,49 @@ mod enums {

macro_rules! casing_test {
($name:ident, $casing:literal, $VariantOne:literal, $Two:literal) => {
#[test]
fn $name() {
#[derive(Display)]
#[display(rename_all = $casing)]
enum Enum {
VariantOne,
Two,
mod $name {
use super::*;
#[test]
fn enum_() {
#[derive(Display)]
#[display(rename_all = $casing)]
enum Enum {
VariantOne,
Two,
}

assert_eq!(Enum::VariantOne.to_string(), $VariantOne);
assert_eq!(Enum::Two.to_string(), $Two);
}

assert_eq!(Enum::VariantOne.to_string(), $VariantOne);
assert_eq!(Enum::Two.to_string(), $Two);
#[test]
fn variant() {
#[derive(Display)]
// ignored
#[display(rename_all = "lowercase")]
enum Enum {
#[display(rename_all = $casing)]
VariantOne,
#[display(rename_all = $casing)]
Two,
}

assert_eq!(Enum::VariantOne.to_string(), $VariantOne);
assert_eq!(Enum::Two.to_string(), $Two);
}

#[test]
fn struct_() {
#[derive(Display)]
#[display(rename_all = $casing)]
struct VariantOne;

#[derive(Display)]
#[display(rename_all = $casing)]
struct Two;
assert_eq!(VariantOne.to_string(), $VariantOne);
assert_eq!(Two.to_string(), $Two);
}
}
};
}
Expand Down

0 comments on commit d531b16

Please sign in to comment.