Use Cow version of to_camel_case in codegen
This commit is contained in:
parent
a826b22ffb
commit
176ac2ee9e
3 changed files with 14 additions and 8 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use syn;
|
use syn;
|
||||||
use syn::*;
|
use syn::*;
|
||||||
use quote::Tokens;
|
use quote::Tokens;
|
||||||
|
@ -110,7 +112,7 @@ pub fn impl_input_object(ast: &syn::DeriveInput) -> Tokens {
|
||||||
let name = match field_attrs.name {
|
let name = match field_attrs.name {
|
||||||
Some(ref name) => {
|
Some(ref name) => {
|
||||||
// Custom name specified.
|
// Custom name specified.
|
||||||
name.to_string()
|
Cow::Borrowed(name.as_ref())
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
// Note: auto camel casing when no custom name specified.
|
// Note: auto camel casing when no custom name specified.
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use syn;
|
use syn;
|
||||||
use syn::*;
|
use syn::*;
|
||||||
use quote::Tokens;
|
use quote::Tokens;
|
||||||
|
@ -106,7 +108,7 @@ pub fn impl_object(ast: &syn::DeriveInput) -> Tokens {
|
||||||
let name = match field_attrs.name {
|
let name = match field_attrs.name {
|
||||||
Some(ref name) => {
|
Some(ref name) => {
|
||||||
// Custom name specified.
|
// Custom name specified.
|
||||||
name.to_string()
|
Cow::Borrowed(name.as_ref())
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
// Note: auto camel casing when no custom name specified.
|
// Note: auto camel casing when no custom name specified.
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use syn::*;
|
use syn::*;
|
||||||
|
|
||||||
pub fn get_graphl_attr(attrs: &Vec<Attribute>) -> Option<&Vec<NestedMetaItem>> {
|
pub fn get_graphl_attr(attrs: &Vec<Attribute>) -> Option<&Vec<NestedMetaItem>> {
|
||||||
|
@ -44,12 +46,12 @@ pub fn keyed_item_value(item: &NestedMetaItem, name: &str, must_be_string: bool)
|
||||||
|
|
||||||
// Note: duplicated from juniper crate!
|
// Note: duplicated from juniper crate!
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn to_camel_case(s: &str) -> String {
|
pub fn to_camel_case<'a>(s: &'a str) -> Cow<'a, str> {
|
||||||
let mut dest = String::new();
|
let mut dest = Cow::Borrowed(s);
|
||||||
|
|
||||||
for (i, part) in s.split('_').enumerate() {
|
for (i, part) in s.split('_').enumerate() {
|
||||||
if i > 0 && part.len() == 1 {
|
if i > 0 && part.len() == 1 {
|
||||||
dest.push_str(&part.to_uppercase());
|
dest += Cow::Owned(part.to_uppercase());
|
||||||
} else if i > 0 && part.len() > 1 {
|
} else if i > 0 && part.len() > 1 {
|
||||||
let first = part.chars()
|
let first = part.chars()
|
||||||
.next()
|
.next()
|
||||||
|
@ -58,10 +60,10 @@ pub fn to_camel_case(s: &str) -> String {
|
||||||
.collect::<String>();
|
.collect::<String>();
|
||||||
let second = &part[1..];
|
let second = &part[1..];
|
||||||
|
|
||||||
dest.push_str(&first);
|
dest += Cow::Owned(first);
|
||||||
dest.push_str(second);
|
dest += second;
|
||||||
} else if i == 0 {
|
} else if i == 0 {
|
||||||
dest.push_str(part);
|
dest = Cow::Borrowed(part);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue