Skip to content

Commit 51b3bbd

Browse files
authored
synv2 (#20)
* upgrade syn to v2 https://github.com/dtolnay/syn/releases/tag/2.0.0 syn v2 is already two years old this should speed up compilation in projects using this crate * fmt * clippy
1 parent 2e4495f commit 51b3bbd

File tree

11 files changed

+238
-184
lines changed

11 files changed

+238
-184
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## 0.6.42
9+
- Fix `QueryBuilder` for Microsoft SQL Server: https://github.com/sqlpage/sqlx-oldapi/issues/11
10+
- Add support for Microsoft SQL Server DateTime columns in sqlx macros: macros https://github.com/sqlpage/sqlx-oldapi/issues/16
11+
812
## 0.6.41
913
- Upgrade rustls to 0.23
1014
- Provide detailed error messages on TLS connection issues

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqlx-macros/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ url = { version = "2.2.2", default-features = false }
8585

8686
[dependencies.syn]
8787
# This is basically default features plus "full" but if they add more defaults later then we don't need to enable those.
88-
version = "1.0.109"
88+
version = "2.0.101"
8989
default-features = false
9090
features = ["full", "parsing", "printing", "derive", "clone-impls", "proc-macro"]
9191

sqlx-macros/src/common.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ pub(crate) fn resolve_path(path: impl AsRef<Path>, err_span: Span) -> syn::Resul
1414

1515
// requires `proc_macro::SourceFile::path()` to be stable
1616
// https://github.com/rust-lang/rust/issues/54725
17-
if path.is_relative()
18-
&& !path
19-
.parent()
20-
.map_or(false, |parent| !parent.as_os_str().is_empty())
21-
{
17+
if path.is_relative() && path.parent().is_none_or(|p| p.as_os_str().is_empty()) {
2218
return Err(syn::Error::new(
2319
err_span,
2420
"paths relative to the current file's directory are not currently supported",

sqlx-macros/src/derives/attributes.rs

Lines changed: 109 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use quote::{quote, quote_spanned};
33
use syn::punctuated::Punctuated;
44
use syn::spanned::Spanned;
55
use syn::token::Comma;
6-
use syn::{Attribute, DeriveInput, Field, Lit, Meta, MetaNameValue, NestedMeta, Variant};
6+
use syn::{
7+
Attribute, DeriveInput, Expr, Field, Lit, LitStr, Meta, MetaNameValue, Path, Token, Variant,
8+
};
79

810
macro_rules! assert_attribute {
911
($e:expr, $err:expr, $input:expr) => {
@@ -83,89 +85,94 @@ pub fn parse_container_attributes(input: &[Attribute]) -> syn::Result<SqlxContai
8385

8486
for attr in input
8587
.iter()
86-
.filter(|a| a.path.is_ident("sqlx") || a.path.is_ident("repr"))
88+
.filter(|a| a.path().is_ident("sqlx") || a.path().is_ident("repr"))
8789
{
88-
let meta = attr
89-
.parse_meta()
90-
.map_err(|e| syn::Error::new_spanned(attr, e))?;
91-
match meta {
90+
match &attr.meta {
9291
Meta::List(list) if list.path.is_ident("sqlx") => {
93-
for value in list.nested.iter() {
94-
match value {
95-
NestedMeta::Meta(meta) => match meta {
96-
Meta::Path(p) if p.is_ident("transparent") => {
97-
try_set!(transparent, true, value)
92+
let nested_metas =
93+
list.parse_args_with(Punctuated::<Meta, syn::token::Comma>::parse_terminated)?;
94+
for meta_item in nested_metas {
95+
match meta_item {
96+
Meta::Path(p) if p.is_ident("transparent") => {
97+
try_set!(transparent, true, p)
98+
}
99+
Meta::NameValue(mnv) if mnv.path.is_ident("rename_all") => {
100+
if let Expr::Lit(expr_lit) = &mnv.value {
101+
if let Lit::Str(val_str) = &expr_lit.lit {
102+
let val = match &*val_str.value() {
103+
"lowercase" => RenameAll::LowerCase,
104+
"snake_case" => RenameAll::SnakeCase,
105+
"UPPERCASE" => RenameAll::UpperCase,
106+
"SCREAMING_SNAKE_CASE" => RenameAll::ScreamingSnakeCase,
107+
"kebab-case" => RenameAll::KebabCase,
108+
"camelCase" => RenameAll::CamelCase,
109+
"PascalCase" => RenameAll::PascalCase,
110+
_ => fail!(val_str, "unexpected value for rename_all"),
111+
};
112+
try_set!(rename_all, val, &mnv.path)
113+
} else {
114+
fail!(expr_lit, "expected string literal for rename_all")
115+
}
116+
} else {
117+
fail!(&mnv.value, "expected literal expression for rename_all")
98118
}
99-
100-
Meta::NameValue(MetaNameValue {
101-
path,
102-
lit: Lit::Str(val),
103-
..
104-
}) if path.is_ident("rename_all") => {
105-
let val = match &*val.value() {
106-
"lowercase" => RenameAll::LowerCase,
107-
"snake_case" => RenameAll::SnakeCase,
108-
"UPPERCASE" => RenameAll::UpperCase,
109-
"SCREAMING_SNAKE_CASE" => RenameAll::ScreamingSnakeCase,
110-
"kebab-case" => RenameAll::KebabCase,
111-
"camelCase" => RenameAll::CamelCase,
112-
"PascalCase" => RenameAll::PascalCase,
113-
_ => fail!(meta, "unexpected value for rename_all"),
114-
};
115-
116-
try_set!(rename_all, val, value)
119+
}
120+
Meta::NameValue(mnv) if mnv.path.is_ident("type_name") => {
121+
if let Expr::Lit(expr_lit) = &mnv.value {
122+
if let Lit::Str(val_str) = &expr_lit.lit {
123+
try_set!(
124+
type_name,
125+
TypeName {
126+
val: val_str.value(),
127+
span: val_str.span(),
128+
deprecated_rename: false
129+
},
130+
&mnv.path
131+
)
132+
} else {
133+
fail!(expr_lit, "expected string literal for type_name")
134+
}
135+
} else {
136+
fail!(&mnv.value, "expected literal expression for type_name")
117137
}
118-
119-
Meta::NameValue(MetaNameValue {
120-
path,
121-
lit: Lit::Str(val),
122-
..
123-
}) if path.is_ident("type_name") => {
124-
try_set!(
125-
type_name,
126-
TypeName {
127-
val: val.value(),
128-
span: value.span(),
129-
deprecated_rename: false
130-
},
131-
value
132-
)
138+
}
139+
Meta::NameValue(mnv) if mnv.path.is_ident("rename") => {
140+
if let Expr::Lit(expr_lit) = &mnv.value {
141+
if let Lit::Str(val_str) = &expr_lit.lit {
142+
try_set!(
143+
type_name,
144+
TypeName {
145+
val: val_str.value(),
146+
span: val_str.span(),
147+
deprecated_rename: true
148+
},
149+
&mnv.path
150+
)
151+
} else {
152+
fail!(expr_lit, "expected string literal for rename")
153+
}
154+
} else {
155+
fail!(&mnv.value, "expected literal expression for rename")
133156
}
134-
135-
Meta::NameValue(MetaNameValue {
136-
path,
137-
lit: Lit::Str(val),
138-
..
139-
}) if path.is_ident("rename") => {
140-
try_set!(
141-
type_name,
142-
TypeName {
143-
val: val.value(),
144-
span: value.span(),
145-
deprecated_rename: true
146-
},
147-
value
148-
)
149-
}
150-
151-
u => fail!(u, "unexpected attribute"),
152-
},
153-
u => fail!(u, "unexpected attribute"),
157+
}
158+
u => fail!(u, "unexpected attribute inside sqlx(...)"),
154159
}
155160
}
156161
}
157162
Meta::List(list) if list.path.is_ident("repr") => {
158-
if list.nested.len() != 1 {
159-
fail!(&list.nested, "expected one value")
163+
let nested_metas =
164+
list.parse_args_with(Punctuated::<Meta, syn::token::Comma>::parse_terminated)?;
165+
if nested_metas.len() != 1 {
166+
fail!(&list.path, "expected one value for repr")
160167
}
161-
match list.nested.first().unwrap() {
162-
NestedMeta::Meta(Meta::Path(p)) if p.get_ident().is_some() => {
163-
try_set!(repr, p.get_ident().unwrap().clone(), list);
168+
match nested_metas.first().unwrap() {
169+
Meta::Path(p) if p.get_ident().is_some() => {
170+
try_set!(repr, p.get_ident().unwrap().clone(), &list.path);
164171
}
165-
u => fail!(u, "unexpected value"),
172+
u => fail!(u, "unexpected value for repr"),
166173
}
167174
}
168-
_ => {}
175+
_ => { /* Not an attribute we are interested in, or not a list */ }
169176
}
170177
}
171178

@@ -183,30 +190,37 @@ pub fn parse_child_attributes(input: &[Attribute]) -> syn::Result<SqlxChildAttri
183190
let mut try_from = None;
184191
let mut flatten = false;
185192

186-
for attr in input.iter().filter(|a| a.path.is_ident("sqlx")) {
187-
let meta = attr
188-
.parse_meta()
189-
.map_err(|e| syn::Error::new_spanned(attr, e))?;
190-
191-
if let Meta::List(list) = meta {
192-
for value in list.nested.iter() {
193-
match value {
194-
NestedMeta::Meta(meta) => match meta {
195-
Meta::NameValue(MetaNameValue {
196-
path,
197-
lit: Lit::Str(val),
198-
..
199-
}) if path.is_ident("rename") => try_set!(rename, val.value(), value),
200-
Meta::NameValue(MetaNameValue {
201-
path,
202-
lit: Lit::Str(val),
203-
..
204-
}) if path.is_ident("try_from") => try_set!(try_from, val.parse()?, value),
205-
Meta::Path(path) if path.is_ident("default") => default = true,
206-
Meta::Path(path) if path.is_ident("flatten") => flatten = true,
207-
u => fail!(u, "unexpected attribute"),
208-
},
209-
u => fail!(u, "unexpected attribute"),
193+
for attr in input.iter().filter(|a| a.path().is_ident("sqlx")) {
194+
if let Meta::List(list) = &attr.meta {
195+
let nested_metas =
196+
list.parse_args_with(Punctuated::<Meta, syn::token::Comma>::parse_terminated)?;
197+
for meta_item in nested_metas {
198+
match meta_item {
199+
Meta::NameValue(mnv) if mnv.path.is_ident("rename") => {
200+
if let Expr::Lit(expr_lit) = &mnv.value {
201+
if let Lit::Str(val_str) = &expr_lit.lit {
202+
try_set!(rename, val_str.value(), &mnv.path)
203+
} else {
204+
fail!(expr_lit, "expected string literal for rename")
205+
}
206+
} else {
207+
fail!(&mnv.value, "expected literal expression for rename")
208+
}
209+
}
210+
Meta::NameValue(mnv) if mnv.path.is_ident("try_from") => {
211+
if let Expr::Lit(expr_lit) = &mnv.value {
212+
if let Lit::Str(val_str) = &expr_lit.lit {
213+
try_set!(try_from, val_str.parse()?, &mnv.path)
214+
} else {
215+
fail!(expr_lit, "expected string literal for try_from")
216+
}
217+
} else {
218+
fail!(&mnv.value, "expected literal expression for try_from")
219+
}
220+
}
221+
Meta::Path(path) if path.is_ident("default") => default = true,
222+
Meta::Path(path) if path.is_ident("flatten") => flatten = true,
223+
u => fail!(u, "unexpected attribute inside sqlx(...)"),
210224
}
211225
}
212226
}

sqlx-macros/src/derives/encode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use syn::punctuated::Punctuated;
99
use syn::token::Comma;
1010
use syn::{
1111
parse_quote, Data, DataEnum, DataStruct, DeriveInput, Expr, Field, Fields, FieldsNamed,
12-
FieldsUnnamed, Lifetime, LifetimeDef, Stmt, Variant,
12+
FieldsUnnamed, Lifetime, LifetimeParam, Stmt, Variant,
1313
};
1414

1515
pub fn expand_derive_encode(input: &DeriveInput) -> syn::Result<TokenStream> {
@@ -66,7 +66,7 @@ fn expand_derive_encode_transparent(
6666
let mut generics = generics.clone();
6767
generics
6868
.params
69-
.insert(0, LifetimeDef::new(lifetime.clone()).into());
69+
.insert(0, LifetimeParam::new(lifetime.clone()).into());
7070

7171
generics
7272
.params

sqlx-macros/src/lib.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![allow(clippy::large_enum_variant)]
12
#![cfg_attr(
23
not(any(feature = "postgres", feature = "mysql", feature = "offline")),
34
allow(dead_code, unused_macros, unused_imports)
@@ -12,6 +13,10 @@ use proc_macro::TokenStream;
1213

1314
use quote::quote;
1415

16+
use syn::parse::{Parse, ParseStream};
17+
use syn::punctuated::Punctuated;
18+
use syn::{parse_macro_input, DeriveInput, ItemFn, LitStr, Meta, Token};
19+
1520
type Error = Box<dyn std::error::Error>;
1621

1722
type Result<T> = std::result::Result<T, Error>;
@@ -27,6 +32,16 @@ mod test_attr;
2732
#[cfg(feature = "migrate")]
2833
mod migrate;
2934

35+
struct ArgsParser(Punctuated<Meta, Token![,]>);
36+
37+
impl Parse for ArgsParser {
38+
fn parse(input: ParseStream) -> syn::Result<Self> {
39+
Ok(ArgsParser(Punctuated::<Meta, Token![,]>::parse_terminated(
40+
input,
41+
)?))
42+
}
43+
}
44+
3045
#[proc_macro]
3146
pub fn expand_query(input: TokenStream) -> TokenStream {
3247
let input = syn::parse_macro_input!(input as query::QueryMacroInput);
@@ -101,19 +116,12 @@ pub fn migrate(input: TokenStream) -> TokenStream {
101116
}
102117

103118
#[proc_macro_attribute]
104-
pub fn test(args: TokenStream, input: TokenStream) -> TokenStream {
105-
let args = syn::parse_macro_input!(args as syn::AttributeArgs);
106-
let input = syn::parse_macro_input!(input as syn::ItemFn);
107-
108-
match test_attr::expand(args, input) {
109-
Ok(ts) => ts.into(),
110-
Err(e) => {
111-
if let Some(parse_err) = e.downcast_ref::<syn::Error>() {
112-
parse_err.to_compile_error().into()
113-
} else {
114-
let msg = e.to_string();
115-
quote!(::std::compile_error!(#msg)).into()
116-
}
117-
}
118-
}
119+
pub fn test(
120+
args: proc_macro::TokenStream,
121+
input: proc_macro::TokenStream,
122+
) -> proc_macro::TokenStream {
123+
let args = parse_macro_input!(args as ArgsParser).0;
124+
let input = parse_macro_input!(input as ItemFn);
125+
126+
test_attr::expand(args, input)
119127
}

sqlx-macros/src/migrate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl ToTokens for QuotedMigrationType {
1919
quote! { ::sqlx_oldapi::migrate::MigrationType::ReversibleDown }
2020
}
2121
};
22-
tokens.append_all(ts.into_iter());
22+
tokens.append_all(ts);
2323
}
2424
}
2525

@@ -101,7 +101,7 @@ pub(crate) fn expand_migrator(path: &Path) -> crate::Result<TokenStream> {
101101
.replace('_', " ")
102102
.to_owned();
103103

104-
let sql = fs::read_to_string(&entry.path())?;
104+
let sql = fs::read_to_string(entry.path())?;
105105

106106
let checksum = Vec::from(Sha384::digest(sql.as_bytes()).as_slice());
107107

0 commit comments

Comments
 (0)