Skip to content

Commit

Permalink
Rename_all attribute documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
JaydenElliott committed Mar 28, 2023
1 parent d509b3b commit f4b181a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
10 changes: 5 additions & 5 deletions postgres-derive-test/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ fn rename_all_overrides() {
#[derive(Debug, ToSql, FromSql, PartialEq)]
#[postgres(name = "mood", rename_all = "snake_case")]
enum Mood {
Sad,
VerySad,
#[postgres(name = "okay")]
Ok,
Happy,
VeryHappy,
}

let mut conn = Client::connect("user=postgres host=localhost port=5433", NoTls).unwrap();
conn.execute(
"CREATE TYPE pg_temp.mood AS ENUM ('sad', 'okay', 'happy')",
"CREATE TYPE pg_temp.mood AS ENUM ('very_sad', 'okay', 'very_happy')",
&[],
)
.unwrap();
Expand All @@ -75,9 +75,9 @@ fn rename_all_overrides() {
&mut conn,
"mood",
&[
(Mood::Sad, "'sad'"),
(Mood::VerySad, "'very_sad'"),
(Mood::Ok, "'okay'"),
(Mood::Happy, "'happy'"),
(Mood::VeryHappy, "'very_happy'"),
],
);
}
Expand Down
4 changes: 2 additions & 2 deletions postgres-derive/src/fromsql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ use crate::overrides::Overrides;
pub fn expand_derive_fromsql(input: DeriveInput) -> Result<TokenStream, Error> {
let overrides = Overrides::extract(&input.attrs, true)?;

if overrides.name.is_some() && overrides.transparent {
if (overrides.name.is_some() || overrides.rename_all.is_some()) && overrides.transparent {
return Err(Error::new_spanned(
&input,
"#[postgres(transparent)] is not allowed with #[postgres(name = \"...\")]",
"#[postgres(transparent)] is not allowed with #[postgres(name = \"...\")] or #[postgres(rename_all = \"...\")]",
));
}

Expand Down
4 changes: 2 additions & 2 deletions postgres-derive/src/tosql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use crate::overrides::Overrides;
pub fn expand_derive_tosql(input: DeriveInput) -> Result<TokenStream, Error> {
let overrides = Overrides::extract(&input.attrs, true)?;

if overrides.name.is_some() && overrides.transparent {
if (overrides.name.is_some() || overrides.rename_all.is_some()) && overrides.transparent {
return Err(Error::new_spanned(
&input,
"#[postgres(transparent)] is not allowed with #[postgres(name = \"...\")]",
"#[postgres(transparent)] is not allowed with #[postgres(name = \"...\")] or #[postgres(rename_all = \"...\")]",
));
}

Expand Down
31 changes: 31 additions & 0 deletions postgres-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,37 @@
//! Happy,
//! }
//! ```
//!
//! Alternatively, the `#[postgres(rename_all = "...")]` attribute can be used to rename all fields or variants
//! with the chosen casing convention. This will not affect the struct or enum's type name. Note that
//! `#[postgres(name = "...")]` takes precendence when used in conjunction with `#[postgres(rename_all = "...")]`:
//!
//! ```rust
//! # #[cfg(feature = "derive")]
//! use postgres_types::{ToSql, FromSql};
//!
//! # #[cfg(feature = "derive")]
//! #[derive(Debug, ToSql, FromSql)]
//! #[postgres(name = "mood", rename_all = "snake_case")]
//! enum Mood {
//! VerySad, // very_sad
//! #[postgres(name = "ok")]
//! Ok, // ok
//! VeryHappy, // very_happy
//! }
//! ```
//!
//! The following case conventions are supported:
//! - `"lowercase"`
//! - `"UPPERCASE"`
//! - `"PascalCase"`
//! - `"camelCase"`
//! - `"snake_case"`
//! - `"SCREAMING_SNAKE_CASE"`
//! - `"kebab-case"`
//! - `"SCREAMING-KEBAB-CASE"`
//! - `"Train-Case"`
#![doc(html_root_url = "https://docs.rs/postgres-types/0.2")]
#![warn(clippy::all, rust_2018_idioms, missing_docs)]

Expand Down

0 comments on commit f4b181a

Please sign in to comment.