forked from 91zgaoge/StoryForge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_subscription.py
More file actions
41 lines (34 loc) · 1.28 KB
/
Copy pathfix_subscription.py
File metadata and controls
41 lines (34 loc) · 1.28 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
import re
with open('src/subscription/mod.rs', 'r', encoding='utf-8') as f:
content = f.read()
# 1. Add AppError import
if 'use crate::error::AppError;' not in content:
content = content.replace(
'use crate::db::DbPool;',
'use crate::db::DbPool;\nuse crate::error::AppError;'
)
# 2. Replace function return types: Result<T, String> -> Result<T, AppError>
# But NOT inside FromStr impl where Err = String
lines = content.split('\n')
new_lines = []
in_from_str_impl = False
for i, line in enumerate(lines):
stripped = line.strip()
if 'impl std::str::FromStr for SubscriptionTier' in stripped:
in_from_str_impl = True
if in_from_str_impl and stripped.startswith('}'):
in_from_str_impl = False
new_lines.append(line)
continue
if in_from_str_impl:
new_lines.append(line)
continue
# Replace Result<..., String> in function signatures and other places
line = re.sub(r'Result<([^>]+(?:<[^>]+>)?), String>', r'Result<\1, AppError>', line)
new_lines.append(line)
content = '\n'.join(new_lines)
# 3. Remove .map_err(|e| e.to_string())
content = re.sub(r'\.map_err\(\|e\| e\.to_string\(\)\)', '', content)
with open('src/subscription/mod.rs', 'w', encoding='utf-8') as f:
f.write(content)
print('Done')