forked from DreamLab-AI/origin-logseq-AR
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathrefactor_responses_phase3.py
More file actions
executable file
·113 lines (82 loc) · 3.79 KB
/
refactor_responses_phase3.py
File metadata and controls
executable file
·113 lines (82 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
"""
HTTP Response Standardization - Phase 3
Handles special status codes: TooManyRequests, ServiceUnavailable, PayloadTooLarge
"""
import re
from pathlib import Path
def refactor_special_status_codes(content: str) -> tuple[str, int]:
"""Refactor special HTTP status codes."""
replacements = 0
# Pattern 1: HttpResponse::TooManyRequests().json(json!({...}))
pattern1 = r'HttpResponse::TooManyRequests\(\)\.json\(json!\(\{[^}]*"error":\s*"([^"]+)"[^}]*\}\)\)'
content, count1 = re.subn(pattern1, r'too_many_requests!("\1")', content)
replacements += count1
# Pattern 2: HttpResponse::ServiceUnavailable().json(json!({...}))
pattern2 = r'HttpResponse::ServiceUnavailable\(\)\.json\(json!\(\{[^}]*"error":\s*"([^"]+)"[^}]*\}\)\)'
content, count2 = re.subn(pattern2, r'service_unavailable!("\1")', content)
replacements += count2
# Pattern 3: HttpResponse::PayloadTooLarge().json(json!({...}))
pattern3 = r'HttpResponse::PayloadTooLarge\(\)\.json\(json!\(\{[^}]*"error":\s*"([^"]+)"[^}]*\}\)\)'
content, count3 = re.subn(pattern3, r'payload_too_large!("\1")', content)
replacements += count3
return content, replacements
def update_imports(content: str) -> str:
"""Update imports to include new macros."""
# Check if imports need updating
if 'use crate::{ok_json' in content and 'too_many_requests' not in content:
# Find the import line and update it
pattern = r'use crate::\{([^}]+)\};'
match = re.search(pattern, content)
if match:
current_imports = match.group(1)
if 'too_many_requests' not in current_imports:
new_imports = current_imports + ', too_many_requests, service_unavailable, payload_too_large'
content = content.replace(match.group(0), f'use crate::{{{new_imports}}};')
return content
def main():
handlers_dir = Path("/home/devuser/workspace/project/src/handlers")
print("=" * 70)
print("HTTP Response Standardization - Phase 3")
print("=" * 70)
print("Handling special status codes: 429, 503, 413\n")
total_replacements = 0
files_changed = 0
for rust_file in sorted(handlers_dir.rglob("*.rs")):
with open(rust_file, 'r', encoding='utf-8') as f:
content = f.read()
# Skip if no special status codes
if not any(code in content for code in ['TooManyRequests', 'ServiceUnavailable', 'PayloadTooLarge']):
continue
original_content = content
# Refactor special status codes
content, count = refactor_special_status_codes(content)
if count > 0:
# Update imports
content = update_imports(content)
# Write file
with open(rust_file, 'w', encoding='utf-8') as f:
f.write(content)
rel_path = rust_file.relative_to(handlers_dir)
print(f"✓ {rel_path}: {count} replacements")
total_replacements += count
files_changed += 1
print("\n" + "=" * 70)
print("PHASE 3 SUMMARY")
print("=" * 70)
print(f"Files changed: {files_changed}")
print(f"Total replacements: {total_replacements}")
# Final count
remaining = 0
for rust_file in handlers_dir.rglob("*.rs"):
with open(rust_file, 'r') as f:
for line in f:
if 'HttpResponse::' in line and \
'use actix' not in line and \
'response_macros' not in line and \
'handler_commons' not in line:
remaining += 1
print(f"\nFinal remaining direct HttpResponse usages: {remaining}")
return 0
if __name__ == "__main__":
exit(main())