Skip to content

Commit 5ed7005

Browse files
committed
Optimize
1 parent 29b35c2 commit 5ed7005

9 files changed

Lines changed: 64 additions & 31 deletions

File tree

crates/vespera_macro/src/parser/schema/serde_attrs.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ pub fn extract_doc_comment(attrs: &[syn::Attribute]) -> Option<String> {
3030
}
3131
}
3232

33-
3433
/// Strips the `r#` prefix from raw identifiers, returning an owned `String`.
3534
/// For the 99% case (no `r#` prefix), returns the input directly with zero extra allocation.
3635
#[allow(clippy::option_if_let_else)] // clippy suggestion doesn't compile: borrow-move conflict

crates/vespera_macro/src/router_codegen.rs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,13 @@ pub fn get_users() -> String {
12121212
let metadata = CollectedMetadata::new();
12131213
let spec = r#"{"openapi":"3.1.0"}"#;
12141214

1215-
let result = generate_router_code(&metadata, Some("/docs"), None, Some(quote::quote!(#spec)), &[]);
1215+
let result = generate_router_code(
1216+
&metadata,
1217+
Some("/docs"),
1218+
None,
1219+
Some(quote::quote!(#spec)),
1220+
&[],
1221+
);
12161222
let code = result.to_string();
12171223

12181224
assert!(code.contains("/docs"));
@@ -1226,7 +1232,13 @@ pub fn get_users() -> String {
12261232
let metadata = CollectedMetadata::new();
12271233
let spec = r#"{"openapi":"3.1.0"}"#;
12281234

1229-
let result = generate_router_code(&metadata, None, Some("/redoc"), Some(quote::quote!(#spec)), &[]);
1235+
let result = generate_router_code(
1236+
&metadata,
1237+
None,
1238+
Some("/redoc"),
1239+
Some(quote::quote!(#spec)),
1240+
&[],
1241+
);
12301242
let code = result.to_string();
12311243

12321244
assert!(code.contains("/redoc"));
@@ -1240,8 +1252,13 @@ pub fn get_users() -> String {
12401252
let metadata = CollectedMetadata::new();
12411253
let spec = r#"{"openapi":"3.1.0"}"#;
12421254

1243-
let result =
1244-
generate_router_code(&metadata, Some("/docs"), Some("/redoc"), Some(quote::quote!(#spec)), &[]);
1255+
let result = generate_router_code(
1256+
&metadata,
1257+
Some("/docs"),
1258+
Some("/redoc"),
1259+
Some(quote::quote!(#spec)),
1260+
&[],
1261+
);
12451262
let code = result.to_string();
12461263

12471264
assert!(code.contains("/docs"));
@@ -1530,7 +1547,13 @@ pub fn get_users() -> String {
15301547
let spec = r#"{"openapi":"3.1.0"}"#;
15311548
let merge_apps: Vec<syn::Path> = vec![syn::parse_quote!(app::MyApp)];
15321549

1533-
let result = generate_router_code(&metadata, Some("/docs"), None, Some(quote::quote!(#spec)), &merge_apps);
1550+
let result = generate_router_code(
1551+
&metadata,
1552+
Some("/docs"),
1553+
None,
1554+
Some(quote::quote!(#spec)),
1555+
&merge_apps,
1556+
);
15341557
let code = result.to_string();
15351558

15361559
// Should have merge code for docs
@@ -1555,7 +1578,13 @@ pub fn get_users() -> String {
15551578
let spec = r#"{"openapi":"3.1.0"}"#;
15561579
let merge_apps: Vec<syn::Path> = vec![syn::parse_quote!(other::OtherApp)];
15571580

1558-
let result = generate_router_code(&metadata, None, Some("/redoc"), Some(quote::quote!(#spec)), &merge_apps);
1581+
let result = generate_router_code(
1582+
&metadata,
1583+
None,
1584+
Some("/redoc"),
1585+
Some(quote::quote!(#spec)),
1586+
&merge_apps,
1587+
);
15591588
let code = result.to_string();
15601589

15611590
// Should have merge code for redoc

crates/vespera_macro/src/schema_macro/circular.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
66
use std::collections::HashMap;
77

8+
use super::type_utils::normalize_token_str;
89
use proc_macro2::TokenStream;
910
use quote::quote;
10-
use super::type_utils::normalize_token_str;
1111

1212
use super::{
1313
seaorm::extract_belongs_to_from_field,

crates/vespera_macro/src/schema_macro/codegen.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use crate::{
1313
metadata::StructMetadata,
1414
parser::{
1515
extract_default, extract_field_rename, extract_rename_all, extract_skip,
16-
extract_skip_serializing_if, parse_type_to_schema_ref, rename_field, strip_raw_prefix_owned,
16+
extract_skip_serializing_if, parse_type_to_schema_ref, rename_field,
17+
strip_raw_prefix_owned,
1718
},
1819
};
1920

crates/vespera_macro/src/schema_macro/file_cache.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use std::collections::HashMap;
1616
use std::path::{Path, PathBuf};
1717
use std::time::SystemTime;
1818

19-
use super::file_lookup::collect_rs_files_recursive;
2019
use super::circular::CircularAnalysis;
20+
use super::file_lookup::collect_rs_files_recursive;
2121
use crate::metadata::StructMetadata;
2222

2323
/// Internal cache state.
@@ -221,7 +221,10 @@ pub fn get_circular_analysis(source_module_path: &[String], definition: &str) ->
221221

222222
// 3. Store — new borrow
223223
FILE_CACHE.with(|cache| {
224-
cache.borrow_mut().circular_analysis.insert(key, result.clone());
224+
cache
225+
.borrow_mut()
226+
.circular_analysis
227+
.insert(key, result.clone());
225228
});
226229

227230
result
@@ -244,7 +247,10 @@ pub fn get_struct_from_schema_path(path_str: &str) -> Option<StructMetadata> {
244247

245248
// 3. Store — new borrow
246249
FILE_CACHE.with(|cache| {
247-
cache.borrow_mut().struct_lookup.insert(path_str.to_string(), result.clone());
250+
cache
251+
.borrow_mut()
252+
.struct_lookup
253+
.insert(path_str.to_string(), result.clone());
248254
});
249255

250256
result
@@ -269,7 +275,10 @@ pub fn get_fk_column(schema_path: &str, via_rel: &str) -> Option<String> {
269275

270276
// 3. Store — new borrow
271277
FILE_CACHE.with(|cache| {
272-
cache.borrow_mut().fk_column_lookup.insert(key, result.clone());
278+
cache
279+
.borrow_mut()
280+
.fk_column_lookup
281+
.insert(key, result.clone());
273282
});
274283

275284
result
@@ -308,7 +317,10 @@ pub fn get_module_path_from_schema_path(schema_path: &proc_macro2::TokenStream)
308317

309318
// 3. Store — new borrow
310319
FILE_CACHE.with(|cache| {
311-
cache.borrow_mut().module_path_cache.insert(path_str, result.clone());
320+
cache
321+
.borrow_mut()
322+
.module_path_cache
323+
.insert(path_str, result.clone());
312324
});
313325

314326
result
@@ -369,7 +381,6 @@ mod tests {
369381

370382
use super::*;
371383

372-
373384
#[test]
374385
fn test_get_struct_candidates_filters_correctly() {
375386
let temp_dir = TempDir::new().unwrap();

crates/vespera_macro/src/schema_macro/file_lookup.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ pub fn find_struct_by_name_in_all_files(
155155
// This avoids parsing ALL files for the common same-file pattern:
156156
// schema_type!(Schema from Model, name = "UserSchema") in user.rs
157157
if let Some(prefix_normalized) = &prefix_normalized {
158-
159158
// Partition files: candidate files (filename matches hint prefix) vs rest
160159
let (candidates, rest): (Vec<_>, Vec<_>) = rs_files.into_iter().partition(|path| {
161160
path.file_stem()
@@ -201,9 +200,7 @@ pub fn find_struct_by_name_in_all_files(
201200
.filter(|(path, _)| {
202201
path.file_stem()
203202
.and_then(|s| s.to_str())
204-
.is_some_and(|name| {
205-
normalize_name(name) == *prefix_normalized
206-
})
203+
.is_some_and(|name| normalize_name(name) == *prefix_normalized)
207204
})
208205
.collect();
209206

@@ -221,8 +218,7 @@ pub fn find_struct_by_name_in_all_files(
221218
path.file_stem()
222219
.and_then(|s| s.to_str())
223220
.is_some_and(|name| {
224-
normalize_name(name)
225-
.contains(prefix_normalized.as_str())
221+
normalize_name(name).contains(prefix_normalized.as_str())
226222
})
227223
})
228224
.collect();
@@ -276,15 +272,12 @@ pub fn find_struct_by_name_in_all_files(
276272
// Multiple matches without hint (or hint didn't match candidates above).
277273
// Re-use hint disambiguation logic for full-scan results.
278274
if let Some(prefix_normalized) = &prefix_normalized {
279-
280275
let exact_match: Vec<_> = found_structs
281276
.iter()
282277
.filter(|(path, _)| {
283278
path.file_stem()
284279
.and_then(|s| s.to_str())
285-
.is_some_and(|name| {
286-
normalize_name(name) == *prefix_normalized
287-
})
280+
.is_some_and(|name| normalize_name(name) == *prefix_normalized)
288281
})
289282
.collect();
290283

@@ -300,8 +293,7 @@ pub fn find_struct_by_name_in_all_files(
300293
path.file_stem()
301294
.and_then(|s| s.to_str())
302295
.is_some_and(|name| {
303-
normalize_name(name)
304-
.contains(prefix_normalized.as_str())
296+
normalize_name(name).contains(prefix_normalized.as_str())
305297
})
306298
})
307299
.collect();

crates/vespera_macro/src/schema_macro/from_model.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
55
use std::collections::HashMap;
66

7+
use super::type_utils::normalize_token_str;
78
use proc_macro2::TokenStream;
89
use quote::quote;
910
use syn::Type;
10-
use super::type_utils::normalize_token_str;
1111

1212
use super::{
1313
circular::{generate_inline_struct_construction, generate_inline_type_construction},

crates/vespera_macro/src/schema_macro/seaorm.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,10 @@ pub fn extract_sea_orm_default_value(attrs: &[syn::Attribute]) -> Option<String>
259259
}
260260

261261
// If quoted string, strip quotes and return inner value
262-
if let Some(inner) = raw_value.strip_prefix('"').and_then(|s| s.strip_suffix('"')) {
262+
if let Some(inner) = raw_value
263+
.strip_prefix('"')
264+
.and_then(|s| s.strip_suffix('"'))
265+
{
263266
return Some(inner.to_string());
264267
}
265268
// Numeric, bool, or other literal — return as-is

crates/vespera_macro/src/schema_macro/type_utils.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ pub fn resolve_type_to_absolute_path(ty: &Type, source_module_path: &[String]) -
184184
quote! { #(#path_idents)::* :: #type_ident #args }
185185
}
186186

187-
188187
/// Extract the module path from a type (excluding the type name itself).
189188
/// e.g., `crate::models::memo::Model` -> `["crate", "models", "memo"]`
190189
pub fn extract_module_path(ty: &Type) -> Vec<String> {
@@ -699,5 +698,4 @@ mod tests {
699698
let ty: syn::Type = syn::parse_str("Vec<DateTime<Utc>>").unwrap();
700699
assert!(is_primitive_like(&ty));
701700
}
702-
703701
}

0 commit comments

Comments
 (0)