import os
import re
def fix_rust_examples():
examples_dir = "/workspaces/graph-sp/examples/rs"
for filename in os.listdir(examples_dir):
if filename.endswith('.rs'):
filepath = os.path.join(examples_dir, filename)
with open(filepath, 'r') as f:
content = f.read()
if 'use std::sync::Arc;' not in content:
content = content.replace(
'use std::collections::HashMap;',
'use std::collections::HashMap;\nuse std::sync::Arc;'
)
pattern = r'(\s+graph\.add\(\s*)\n(\s+)(\|[^|]*\|[^{]*\{)'
def replacement(match):
indent = match.group(2)
return match.group(1) + '\n' + indent + 'Arc::new(' + match.group(3)
new_content = re.sub(pattern, replacement, content, flags=re.MULTILINE)
lines = new_content.split('\n')
result_lines = []
arc_new_depth = 0
brace_depth = 0
for i, line in enumerate(lines):
result_lines.append(line)
arc_new_depth += line.count('Arc::new(|')
if arc_new_depth > 0:
brace_depth += line.count('{') - line.count('}')
if brace_depth == 0 and line.strip().endswith('},'):
result_lines[-1] = line.replace('},', '}),')
arc_new_depth -= 1
new_content = '\n'.join(result_lines)
if new_content != content:
with open(filepath, 'w') as f:
f.write(new_content)
print(f"Updated {filename}")
if __name__ == "__main__":
fix_rust_examples()