File tree 4 files changed +40
-9
lines changed
4 files changed +40
-9
lines changed Original file line number Diff line number Diff line change @@ -58,15 +58,15 @@ fn rename_all_overrides() {
58
58
#[ derive( Debug , ToSql , FromSql , PartialEq ) ]
59
59
#[ postgres( name = "mood" , rename_all = "snake_case" ) ]
60
60
enum Mood {
61
- Sad ,
61
+ VerySad ,
62
62
#[ postgres( name = "okay" ) ]
63
63
Ok ,
64
- Happy ,
64
+ VeryHappy ,
65
65
}
66
66
67
67
let mut conn = Client :: connect ( "user=postgres host=localhost port=5433" , NoTls ) . unwrap ( ) ;
68
68
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 ')" ,
70
70
& [ ] ,
71
71
)
72
72
. unwrap ( ) ;
@@ -75,9 +75,9 @@ fn rename_all_overrides() {
75
75
& mut conn,
76
76
"mood" ,
77
77
& [
78
- ( Mood :: Sad , "'sad '" ) ,
78
+ ( Mood :: VerySad , "'very_sad '" ) ,
79
79
( Mood :: Ok , "'okay'" ) ,
80
- ( Mood :: Happy , "'happy '" ) ,
80
+ ( Mood :: VeryHappy , "'very_happy '" ) ,
81
81
] ,
82
82
) ;
83
83
}
Original file line number Diff line number Diff line change @@ -17,10 +17,10 @@ use crate::overrides::Overrides;
17
17
pub fn expand_derive_fromsql ( input : DeriveInput ) -> Result < TokenStream , Error > {
18
18
let overrides = Overrides :: extract ( & input. attrs , true ) ?;
19
19
20
- if overrides. name . is_some ( ) && overrides. transparent {
20
+ if ( overrides. name . is_some ( ) || overrides . rename_all . is_some ( ) ) && overrides. transparent {
21
21
return Err ( Error :: new_spanned (
22
22
& input,
23
- "#[postgres(transparent)] is not allowed with #[postgres(name = \" ...\" )]" ,
23
+ "#[postgres(transparent)] is not allowed with #[postgres(name = \" ...\" )] or #[postgres(rename_all = \" ... \" )] " ,
24
24
) ) ;
25
25
}
26
26
Original file line number Diff line number Diff line change @@ -15,10 +15,10 @@ use crate::overrides::Overrides;
15
15
pub fn expand_derive_tosql ( input : DeriveInput ) -> Result < TokenStream , Error > {
16
16
let overrides = Overrides :: extract ( & input. attrs , true ) ?;
17
17
18
- if overrides. name . is_some ( ) && overrides. transparent {
18
+ if ( overrides. name . is_some ( ) || overrides . rename_all . is_some ( ) ) && overrides. transparent {
19
19
return Err ( Error :: new_spanned (
20
20
& input,
21
- "#[postgres(transparent)] is not allowed with #[postgres(name = \" ...\" )]" ,
21
+ "#[postgres(transparent)] is not allowed with #[postgres(name = \" ...\" )] or #[postgres(rename_all = \" ... \" )] " ,
22
22
) ) ;
23
23
}
24
24
Original file line number Diff line number Diff line change 125
125
//! Happy,
126
126
//! }
127
127
//! ```
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
+
128
159
#![ doc( html_root_url = "https://docs.rs/postgres-types/0.2" ) ]
129
160
#![ warn( clippy:: all, rust_2018_idioms, missing_docs) ]
130
161
You can’t perform that action at this time.
0 commit comments