Add mixedCase and CamelCase to rename-all, added rename-all to FromRow

This commit is contained in:
Federico Rampazzo
2020-11-27 16:55:32 +00:00
committed by Ryan Leckey
parent e584618041
commit 00a999145f
4 changed files with 99 additions and 11 deletions

View File

@@ -33,6 +33,8 @@ pub enum RenameAll {
UpperCase,
ScreamingSnakeCase,
KebabCase,
MixedCase,
CamelCase,
}
pub struct SqlxContainerAttributes {
@@ -77,7 +79,8 @@ pub fn parse_container_attributes(input: &[Attribute]) -> syn::Result<SqlxContai
"UPPERCASE" => RenameAll::UpperCase,
"SCREAMING_SNAKE_CASE" => RenameAll::ScreamingSnakeCase,
"kebab-case" => RenameAll::KebabCase,
"mixedCase" => RenameAll::MixedCase,
"CamelCase" => RenameAll::CamelCase,
_ => fail!(meta, "unexpected value for rename_all"),
};

View File

@@ -10,7 +10,7 @@ pub(crate) use r#type::expand_derive_type;
pub(crate) use row::expand_derive_from_row;
use self::attributes::RenameAll;
use heck::{KebabCase, ShoutySnakeCase, SnakeCase};
use heck::{KebabCase, ShoutySnakeCase, SnakeCase, CamelCase, MixedCase};
use std::iter::FromIterator;
use syn::DeriveInput;
@@ -35,5 +35,8 @@ pub(crate) fn rename_all(s: &str, pattern: RenameAll) -> String {
RenameAll::UpperCase => s.to_uppercase(),
RenameAll::ScreamingSnakeCase => s.to_shouty_snake_case(),
RenameAll::KebabCase => s.to_kebab_case(),
RenameAll::MixedCase => s.to_mixed_case(),
RenameAll::CamelCase => s.to_camel_case(),
}
}

View File

@@ -5,7 +5,7 @@ use syn::{
Fields, FieldsNamed, FieldsUnnamed, Lifetime, Stmt,
};
use super::attributes::parse_child_attributes;
use super::{attributes::{parse_child_attributes, parse_container_attributes}, rename_all};
pub fn expand_derive_from_row(input: &DeriveInput) -> syn::Result<proc_macro2::TokenStream> {
match &input.data {
@@ -69,13 +69,21 @@ fn expand_derive_from_row_struct(
let (impl_generics, _, where_clause) = generics.split_for_impl();
let container_attributes = parse_container_attributes(&input.attrs)?;
let reads = fields.iter().filter_map(|field| -> Option<Stmt> {
let id = &field.ident.as_ref()?;
let attributes = parse_child_attributes(&field.attrs).unwrap();
let id_s = match attributes.rename {
Some(rename) => rename,
None => id.to_string().trim_start_matches("r#").to_owned(),
};
let id_s = attributes.rename
.or_else(|| Some(id.to_string().trim_start_matches("r#").to_owned()))
.map(|s| match container_attributes.rename_all {
Some(pattern) => rename_all(&s, pattern),
None => s
})
.unwrap();
let ty = &field.ty;
if attributes.default {