Skip to content

Commit 0274b22

Browse files
committed
Streamline Iden quoting
1 parent 095cb1b commit 0274b22

File tree

13 files changed

+84
-216
lines changed

13 files changed

+84
-216
lines changed

sea-query-derive/src/enum_def.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,6 @@ pub fn expand(args: TokenStream, input: TokenStream) -> TokenStream {
8989
}
9090
}
9191

92-
impl #import_name::Iden for #enum_name {
93-
fn unquoted(&self) -> &str {
94-
<Self as #import_name::IdenStatic>::as_str(&self)
95-
}
96-
}
97-
9892
impl ::std::convert::AsRef<str> for #enum_name {
9993
fn as_ref(&self) -> &str {
10094
<Self as #import_name::IdenStatic>::as_str(&self)

sea-query-derive/src/iden/mod.rs

Lines changed: 10 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,10 @@ fn impl_iden_for_unit_struct(
5757
table_name: &str,
5858
) -> proc_macro2::TokenStream {
5959
let sea_query_path = sea_query_path();
60-
61-
if is_static_iden(table_name) {
62-
quote! {
63-
impl #sea_query_path::Iden for #ident {
64-
fn quoted(&self) -> std::borrow::Cow<'static, str> {
65-
std::borrow::Cow::Borrowed(#table_name)
66-
}
67-
68-
fn unquoted(&self) -> &str {
69-
#table_name
70-
}
71-
}
72-
}
73-
} else {
74-
quote! {
75-
impl #sea_query_path::Iden for #ident {
76-
fn unquoted(&self) -> &str {
77-
#table_name
78-
}
60+
quote! {
61+
impl #sea_query_path::IdenStatic for #ident {
62+
fn as_str(&self) -> &'static str {
63+
#table_name
7964
}
8065
}
8166
}
@@ -91,43 +76,19 @@ where
9176
{
9277
let sea_query_path = sea_query_path();
9378

94-
let mut is_all_static_iden = true;
95-
9679
let match_arms = match variants
97-
.map(|v| {
98-
let v = IdenVariant::<DeriveIden>::try_from((table_name, v))?;
99-
is_all_static_iden &= v.is_static_iden();
100-
Ok(v)
101-
})
80+
.map(|v| IdenVariant::<DeriveIden>::try_from((table_name, v)))
10281
.collect::<syn::Result<Vec<_>>>()
10382
{
10483
Ok(v) => v,
10584
Err(e) => return e.to_compile_error(),
10685
};
10786

108-
if is_all_static_iden {
109-
quote! {
110-
impl #sea_query_path::Iden for #ident {
111-
fn quoted(&self) -> std::borrow::Cow<'static, str> {
112-
std::borrow::Cow::Borrowed(match self {
113-
#(#match_arms),*
114-
})
115-
}
116-
117-
fn unquoted(&self) -> &str {
118-
match self {
119-
#(#match_arms),*
120-
}
121-
}
122-
}
123-
}
124-
} else {
125-
quote! {
126-
impl #sea_query_path::Iden for #ident {
127-
fn unquoted(&self) -> &str {
128-
match self {
129-
#(#match_arms),*
130-
}
87+
quote! {
88+
impl #sea_query_path::IdenStatic for #ident {
89+
fn as_str(&self) -> &'static str {
90+
match self {
91+
#(#match_arms),*
13192
}
13293
}
13394
}
@@ -190,11 +151,3 @@ fn find_attr(attrs: &[Attribute]) -> Option<&Attribute> {
190151
attr.path().is_ident(&IdenPath::Iden) || attr.path().is_ident(&IdenPath::Method)
191152
})
192153
}
193-
194-
pub fn is_static_iden(name: &str) -> bool {
195-
// can only begin with [a-z_]
196-
name.chars()
197-
.take(1)
198-
.all(|c| c == '_' || c.is_ascii_alphabetic())
199-
&& name.chars().all(|c| c == '_' || c.is_ascii_alphanumeric())
200-
}

sea-query-derive/src/iden/write_arm.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use proc_macro2::{Span, TokenStream};
66
use quote::{ToTokens, TokenStreamExt, quote};
77
use syn::{Error, Fields, FieldsNamed, Ident, Variant};
88

9-
use super::{
10-
DeriveIden, DeriveIdenStatic, attr::IdenAttr, error::ErrorMsg, find_attr, is_static_iden,
11-
};
9+
use super::{DeriveIden, DeriveIdenStatic, attr::IdenAttr, error::ErrorMsg, find_attr};
1210

1311
pub(crate) trait WriteArm {
1412
fn variant(variant: TokenStream, name: TokenStream) -> TokenStream;
@@ -180,19 +178,6 @@ where
180178

181179
T::variant(variant, name)
182180
}
183-
184-
pub(crate) fn is_static_iden(&self) -> bool {
185-
let name: String = match &self.attr {
186-
Some(a) => match a {
187-
IdenAttr::Rename(name) => name.to_owned(),
188-
IdenAttr::Method(_) => return false,
189-
IdenAttr::Flatten => return false,
190-
},
191-
None => self.table_or_snake_case(),
192-
};
193-
194-
is_static_iden(&name)
195-
}
196181
}
197182

198183
struct Delegated;

src/backend/mod.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Translating the SQL AST into engine-specific SQL statements.
22
33
use crate::*;
4-
use std::borrow::Cow;
54

65
#[cfg(feature = "backend-mysql")]
76
#[cfg_attr(docsrs, doc(cfg(feature = "backend-mysql")))]
@@ -46,17 +45,12 @@ pub trait QuotedBuilder {
4645
let qq = q.1 as char;
4746

4847
sql.write_char(q.left()).unwrap();
49-
match &iden.0 {
50-
Cow::Borrowed(s) => sql.write_str(s).unwrap(),
51-
Cow::Owned(s) => {
52-
for char in s.chars() {
53-
if char == qq {
54-
sql.write_char(char).unwrap()
55-
}
56-
sql.write_char(char).unwrap()
57-
}
48+
for char in iden.0.chars() {
49+
if char == qq {
50+
sql.write_char(char).unwrap()
5851
}
59-
};
52+
sql.write_char(char).unwrap()
53+
}
6054
sql.write_char(q.right()).unwrap();
6155
}
6256
}

src/extension/mysql/column.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::Iden;
1+
use crate::IdenStatic;
22

33
#[derive(Debug, Copy, Clone)]
44
#[non_exhaustive]
@@ -8,8 +8,8 @@ pub enum MySqlType {
88
LongBlob,
99
}
1010

11-
impl Iden for MySqlType {
12-
fn unquoted(&self) -> &str {
11+
impl IdenStatic for MySqlType {
12+
fn as_str(&self) -> &'static str {
1313
match self {
1414
Self::TinyBlob => "tinyblob",
1515
Self::MediumBlob => "mediumblob",

src/extension/postgres/ltree.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::Iden;
1+
use crate::{Iden, IdenStatic};
22

33
/// PostgreSQL `ltree` extension type.
44
///
@@ -50,8 +50,8 @@ use crate::Iden;
5050
#[derive(Debug, Clone, Eq, PartialEq)]
5151
pub struct PgLTree;
5252

53-
impl Iden for PgLTree {
54-
fn unquoted(&self) -> &str {
53+
impl IdenStatic for PgLTree {
54+
fn as_str(&self) -> &'static str {
5555
"ltree"
5656
}
5757
}

src/extension/postgres/types.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ impl TypeDropStatement {
154154
///
155155
/// struct FontFamily;
156156
///
157-
/// impl Iden for FontFamily {
158-
/// fn unquoted(&self) -> &str {
157+
/// impl IdenStatic for FontFamily {
158+
/// fn as_str(&self) -> &'static str {
159159
/// "font_family"
160160
/// }
161161
/// }
@@ -254,8 +254,8 @@ impl TypeAlterStatement {
254254
/// Monospace,
255255
/// }
256256
///
257-
/// impl Iden for FontFamily {
258-
/// fn unquoted(&self) -> &str {
257+
/// impl IdenStatic for FontFamily {
258+
/// fn as_str(&self) -> &'static str {
259259
/// match self {
260260
/// Self::Type => "font_family",
261261
/// Self::Serif => "serif",

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,8 +664,8 @@
664664
//! # use sea_query::{*, tests_cfg::*};
665665
//! struct MyFunction;
666666
//!
667-
//! impl Iden for MyFunction {
668-
//! fn unquoted(&self) -> &str {
667+
//! impl IdenStatic for MyFunction {
668+
//! fn as_str(&self) -> &'static str {
669669
//! "MY_FUNCTION"
670670
//! }
671671
//! }

src/tests_cfg.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#[cfg(feature = "with-json")]
44
pub use serde_json::json;
55

6-
use crate::Iden;
6+
use crate::IdenStatic;
77

88
/// Representation of a database table named `Character`.
99
///
@@ -27,8 +27,8 @@ pub enum Character {
2727
/// A shorthand for [`Character`]
2828
pub type Char = Character;
2929

30-
impl Iden for Character {
31-
fn unquoted(&self) -> &str {
30+
impl IdenStatic for Character {
31+
fn as_str(&self) -> &'static str {
3232
match self {
3333
Self::Table => "character",
3434
Self::Id => "id",
@@ -58,8 +58,8 @@ pub enum Font {
5858
Language,
5959
}
6060

61-
impl Iden for Font {
62-
fn unquoted(&self) -> &str {
61+
impl IdenStatic for Font {
62+
fn as_str(&self) -> &'static str {
6363
match self {
6464
Self::Table => "font",
6565
Self::Id => "id",
@@ -84,8 +84,8 @@ pub enum Glyph {
8484
Tokens,
8585
}
8686

87-
impl Iden for Glyph {
88-
fn unquoted(&self) -> &str {
87+
impl IdenStatic for Glyph {
88+
fn as_str(&self) -> &'static str {
8989
match self {
9090
Self::Table => "glyph",
9191
Self::Id => "id",
@@ -108,8 +108,8 @@ pub enum Task {
108108
IsDone,
109109
}
110110

111-
impl Iden for Task {
112-
fn unquoted(&self) -> &str {
111+
impl IdenStatic for Task {
112+
fn as_str(&self) -> &'static str {
113113
match self {
114114
Self::Table => "task",
115115
Self::Id => "id",

0 commit comments

Comments
 (0)