-
-
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 f09d450
Showing
12 changed files
with
280 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")] | ||
Camel, | ||
#[strum(serialize = "kebab-case")] | ||
Kebab, | ||
#[strum(serialize = "lowercase")] | ||
Lower, | ||
#[strum(serialize = "PascalCase")] | ||
Pascal, | ||
#[strum(serialize = "SCREAMING-KEBAB-CASE")] | ||
ScreamingKebab, | ||
#[strum(serialize = "SCREAMING_SNAKE_CASE")] | ||
ScreamingSnake, | ||
#[strum(serialize = "snake_case")] | ||
Snake, | ||
#[strum(serialize = "UPPERCASE")] | ||
Upper, | ||
} | ||
|
||
impl From<RenameRule> for convert_case::Case { | ||
fn from(rule: RenameRule) -> Self { | ||
match rule { | ||
RenameRule::Camel => Self::Camel, | ||
RenameRule::Kebab => Self::Kebab, | ||
RenameRule::Lower => Self::Lower, | ||
RenameRule::Pascal => Self::Pascal, | ||
RenameRule::ScreamingKebab => Self::UpperKebab, | ||
RenameRule::ScreamingSnake => Self::UpperSnake, | ||
RenameRule::Snake => Self::Snake, | ||
RenameRule::Upper => Self::Upper, | ||
} | ||
} | ||
} |
Oops, something went wrong.