import os
import re
import sys
from pathlib import Path
def add_bon_builder_to_all_structs(root_dir):
models_dir = Path(root_dir) / "src" / "models"
model_files = list(models_dir.glob("*.rs"))
for file_path in model_files:
if file_path.name == "mod.rs":
continue
with open(file_path, 'r') as f:
content = f.read()
if 'bon::Builder' in content:
continue
lines = content.split('\n')
modified = False
i = 0
while i < len(lines):
line = lines[i]
if line.startswith('#[derive(') and i + 1 < len(lines):
next_line = lines[i + 1]
if re.match(r'^pub struct \w+ \{', next_line):
closing_paren = line.rfind(')')
if closing_paren != -1:
lines[i] = line[:closing_paren] + ', bon::Builder' + line[closing_paren:]
modified = True
struct_name = re.search(r'pub struct (\w+)', next_line).group(1)
print(f"Added bon::Builder to {struct_name} in {file_path.name}")
i += 1
if modified:
with open(file_path, 'w') as f:
f.write('\n'.join(lines))
def add_bon_builder_to_other_structs(root_dir):
models_dir = Path(root_dir) / "src" / "models"
other_patterns = [
"create_message_request.rs",
"create_thread_request.rs",
"create_run_request.rs",
"submit_tool_outputs_run_request.rs",
"modify_assistant_request.rs",
"modify_thread_request.rs",
"modify_message_request.rs",
"modify_run_request.rs",
]
for pattern in other_patterns:
file_path = models_dir / pattern
if not file_path.exists():
continue
with open(file_path, 'r') as f:
content = f.read()
if 'bon::Builder' in content:
continue
struct_match = re.search(r'^pub struct (\w+) \{', content, re.MULTILINE)
if not struct_match:
continue
struct_name = struct_match.group(1)
lines = content.split('\n')
modified = False
for i, line in enumerate(lines):
if line.startswith('#[derive(') and i + 1 < len(lines):
next_line = lines[i + 1]
if next_line.startswith(f'pub struct {struct_name} {{'):
closing_paren = line.rfind(')')
if closing_paren != -1:
lines[i] = line[:closing_paren] + ', bon::Builder' + line[closing_paren:]
modified = True
print(f"Added bon::Builder to {struct_name} in {file_path.name}")
break
if modified:
with open(file_path, 'w') as f:
f.write('\n'.join(lines))
if __name__ == "__main__":
root_dir = sys.argv[1] if len(sys.argv) > 1 else "crates/openai-patched"
print(f"Adding bon::Builder to ALL structs in {root_dir}")
add_bon_builder_to_all_structs(root_dir)
print("Done adding bon::Builder derives")