-
-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a container-level
rename_all
attribute to FromSql
/ToSql
.
It works both with composite types' fields and enum variants, although arguably it will be the most useful for the latter as that's where the naming conventions between Rust and a typical PostgreSQL schema differ. Right now it supports the following casing styles: * `"camelCase"` * `"kebab-case"` * `"lowercase"` * `"PascalCase"` * `"SCREAMING-KEBAB-CASE"` * `"SCREAMING_SNAKE_CASE"` * `"snake_case"` * `"UPPERCASE"` mimicking Serde.
- Loading branch information
1 parent
71668b7
commit 4b6e704
Showing
12 changed files
with
281 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use syn::{Attribute, Error, Lit, Meta, NestedMeta}; | ||
|
||
#[derive(Default)] | ||
pub struct FieldVariantOverrides { | ||
pub name: Option<String>, | ||
} | ||
|
||
impl FieldVariantOverrides { | ||
pub fn extract(attrs: &[Attribute]) -> Result<FieldVariantOverrides, Error> { | ||
let mut overrides: FieldVariantOverrides = Default::default(); | ||
|
||
for attr in attrs { | ||
let attr = attr.parse_meta()?; | ||
|
||
if !attr.path().is_ident("postgres") { | ||
continue; | ||
} | ||
|
||
let list = match attr { | ||
Meta::List(ref list) => list, | ||
bad => return Err(Error::new_spanned(bad, "expected a #[postgres(...)]")), | ||
}; | ||
|
||
for item in &list.nested { | ||
match item { | ||
NestedMeta::Meta(Meta::NameValue(meta)) => { | ||
if meta.path.is_ident("name") { | ||
let value = match &meta.lit { | ||
Lit::Str(s) => s.value(), | ||
bad => { | ||
return Err(Error::new_spanned( | ||
bad, | ||
"expected a string literal", | ||
)) | ||
} | ||
}; | ||
overrides.name = Some(value); | ||
} else { | ||
return Err(Error::new_spanned(&meta.path, "unknown override")); | ||
} | ||
} | ||
bad => return Err(Error::new_spanned(bad, "unknown attribute")), | ||
} | ||
} | ||
} | ||
|
||
Ok(overrides) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use strum_macros::{Display, EnumIter, EnumString}; | ||
|
||
#[derive(Clone, Copy, Display, EnumIter, EnumString)] | ||
pub enum RenameRule { | ||
#[strum(serialize = "camelCase")] | ||
CamelCase, | ||
#[strum(serialize = "kebab-case")] | ||
KebabCase, | ||
#[strum(serialize = "lowercase")] | ||
LowerCase, | ||
#[strum(serialize = "PascalCase")] | ||
PascalCase, | ||
#[strum(serialize = "SCREAMING-KEBAB-CASE")] | ||
ScreamingKebabCase, | ||
#[strum(serialize = "SCREAMING_SNAKE_CASE")] | ||
ScreamingSnakeCase, | ||
#[strum(serialize = "snake_case")] | ||
SnakeCase, | ||
#[strum(serialize = "UPPERCASE")] | ||
UpperCase, | ||
} | ||
|
||
impl From<RenameRule> for convert_case::Case { | ||
fn from(rule: RenameRule) -> Self { | ||
match rule { | ||
RenameRule::CamelCase => Self::Camel, | ||
RenameRule::KebabCase => Self::Kebab, | ||
RenameRule::LowerCase => Self::Lower, | ||
RenameRule::PascalCase => Self::Pascal, | ||
RenameRule::ScreamingKebabCase => Self::UpperKebab, | ||
RenameRule::ScreamingSnakeCase => Self::UpperSnake, | ||
RenameRule::SnakeCase => Self::Snake, | ||
RenameRule::UpperCase => Self::Upper, | ||
} | ||
} | ||
} |
Oops, something went wrong.