Skip to content

Commit f4b181a

Browse files
committed
Rename_all attribute documentation
1 parent d509b3b commit f4b181a

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

postgres-derive-test/src/enums.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ fn rename_all_overrides() {
5858
#[derive(Debug, ToSql, FromSql, PartialEq)]
5959
#[postgres(name = "mood", rename_all = "snake_case")]
6060
enum Mood {
61-
Sad,
61+
VerySad,
6262
#[postgres(name = "okay")]
6363
Ok,
64-
Happy,
64+
VeryHappy,
6565
}
6666

6767
let mut conn = Client::connect("user=postgres host=localhost port=5433", NoTls).unwrap();
6868
conn.execute(
69-
"CREATE TYPE pg_temp.mood AS ENUM ('sad', 'okay', 'happy')",
69+
"CREATE TYPE pg_temp.mood AS ENUM ('very_sad', 'okay', 'very_happy')",
7070
&[],
7171
)
7272
.unwrap();
@@ -75,9 +75,9 @@ fn rename_all_overrides() {
7575
&mut conn,
7676
"mood",
7777
&[
78-
(Mood::Sad, "'sad'"),
78+
(Mood::VerySad, "'very_sad'"),
7979
(Mood::Ok, "'okay'"),
80-
(Mood::Happy, "'happy'"),
80+
(Mood::VeryHappy, "'very_happy'"),
8181
],
8282
);
8383
}

postgres-derive/src/fromsql.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ use crate::overrides::Overrides;
1717
pub fn expand_derive_fromsql(input: DeriveInput) -> Result<TokenStream, Error> {
1818
let overrides = Overrides::extract(&input.attrs, true)?;
1919

20-
if overrides.name.is_some() && overrides.transparent {
20+
if (overrides.name.is_some() || overrides.rename_all.is_some()) && overrides.transparent {
2121
return Err(Error::new_spanned(
2222
&input,
23-
"#[postgres(transparent)] is not allowed with #[postgres(name = \"...\")]",
23+
"#[postgres(transparent)] is not allowed with #[postgres(name = \"...\")] or #[postgres(rename_all = \"...\")]",
2424
));
2525
}
2626

postgres-derive/src/tosql.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ use crate::overrides::Overrides;
1515
pub fn expand_derive_tosql(input: DeriveInput) -> Result<TokenStream, Error> {
1616
let overrides = Overrides::extract(&input.attrs, true)?;
1717

18-
if overrides.name.is_some() && overrides.transparent {
18+
if (overrides.name.is_some() || overrides.rename_all.is_some()) && overrides.transparent {
1919
return Err(Error::new_spanned(
2020
&input,
21-
"#[postgres(transparent)] is not allowed with #[postgres(name = \"...\")]",
21+
"#[postgres(transparent)] is not allowed with #[postgres(name = \"...\")] or #[postgres(rename_all = \"...\")]",
2222
));
2323
}
2424

postgres-types/src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,37 @@
125125
//! Happy,
126126
//! }
127127
//! ```
128+
//!
129+
//! Alternatively, the `#[postgres(rename_all = "...")]` attribute can be used to rename all fields or variants
130+
//! with the chosen casing convention. This will not affect the struct or enum's type name. Note that
131+
//! `#[postgres(name = "...")]` takes precendence when used in conjunction with `#[postgres(rename_all = "...")]`:
132+
//!
133+
//! ```rust
134+
//! # #[cfg(feature = "derive")]
135+
//! use postgres_types::{ToSql, FromSql};
136+
//!
137+
//! # #[cfg(feature = "derive")]
138+
//! #[derive(Debug, ToSql, FromSql)]
139+
//! #[postgres(name = "mood", rename_all = "snake_case")]
140+
//! enum Mood {
141+
//! VerySad, // very_sad
142+
//! #[postgres(name = "ok")]
143+
//! Ok, // ok
144+
//! VeryHappy, // very_happy
145+
//! }
146+
//! ```
147+
//!
148+
//! The following case conventions are supported:
149+
//! - `"lowercase"`
150+
//! - `"UPPERCASE"`
151+
//! - `"PascalCase"`
152+
//! - `"camelCase"`
153+
//! - `"snake_case"`
154+
//! - `"SCREAMING_SNAKE_CASE"`
155+
//! - `"kebab-case"`
156+
//! - `"SCREAMING-KEBAB-CASE"`
157+
//! - `"Train-Case"`
158+
128159
#![doc(html_root_url = "https://docs.rs/postgres-types/0.2")]
129160
#![warn(clippy::all, rust_2018_idioms, missing_docs)]
130161

0 commit comments

Comments
 (0)