|
1 | | -use std::{ |
2 | | - env, fs, |
3 | | - path::{Path, PathBuf}, |
4 | | -}; |
5 | | - |
6 | | -use anyhow::Result; |
7 | | - |
8 | | -fn main() -> Result<()> { |
9 | | - let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR not set")); |
10 | | - |
11 | | - // Locate repo root from this crate: rust/examples/basic |
12 | | - let mut repo_root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); |
13 | | - repo_root.pop(); // rust/examples/basic -> rust/examples |
14 | | - repo_root.pop(); // rust/examples -> rust |
15 | | - repo_root.pop(); // rust -> repo root |
16 | | - // Now at repo root |
17 | | - |
18 | | - let fixtures_dir = repo_root.join("fixtures/tests/basic"); |
19 | | - println!("cargo:rerun-if-changed={}", fixtures_dir.display()); |
20 | | - |
21 | | - // Normalize the schemas into a temp dir we control, as the fixtures use a slightly different |
22 | | - // flavor than our grammar expects. |
23 | | - let schema_dir = out_dir.join("normalized_schemas"); |
24 | | - fs::create_dir_all(&schema_dir)?; |
25 | | - |
26 | | - for v in ["v1", "v2", "v3"] { |
27 | | - let src = fixtures_dir.join(format!("{v}.bare")); |
28 | | - let dst = schema_dir.join(format!("{v}.bare")); |
29 | | - |
30 | | - let content_raw = fs::read_to_string(&src)?; |
31 | | - |
32 | | - // Normalize fixture syntax to match current grammar: |
33 | | - // - Strip '//' comments |
34 | | - // - Insert missing 'type' for top-level enums (e.g., `enum X {` -> `type X enum {`) |
35 | | - // - string -> str |
36 | | - // - []Todo -> list<Todo> |
37 | | - // - map<K, V> -> map<K><V> |
38 | | - let mut normalized = String::new(); |
39 | | - for line in content_raw.lines() { |
40 | | - let line_wo_comment = match line.find("//") { |
41 | | - Some(i) => &line[..i], |
42 | | - None => line, |
43 | | - }; |
44 | | - let trimmed = line_wo_comment.trim_start(); |
45 | | - let converted = if trimmed.starts_with("enum ") { |
46 | | - let rest = &trimmed["enum ".len()..]; |
47 | | - if let Some(brace_idx) = rest.find('{') { |
48 | | - let name = rest[..brace_idx].trim(); |
49 | | - format!("type {name} enum {{") |
50 | | - } else { |
51 | | - line_wo_comment.to_string() |
52 | | - } |
53 | | - } else { |
54 | | - line_wo_comment.to_string() |
55 | | - }; |
56 | | - if !converted.trim().is_empty() { |
57 | | - normalized.push_str(&converted); |
58 | | - normalized.push('\n'); |
59 | | - } |
60 | | - } |
61 | | - let mut normalized = normalized |
62 | | - .replace("string", "str") |
63 | | - .replace("[]Todo", "list<Todo>") |
64 | | - .replace("map<str, str>", "map<str><str>") |
65 | | - .replace("map<TodoId, Todo>", "map<TodoId><Todo>") |
66 | | - .replace("map<TagId, Tag>", "map<TagId><Tag>") |
67 | | - .replace("map<BoardId, Board>", "map<BoardId><Board>") |
68 | | - .replace("map<str, list<TodoId>>", "map<str><list<TodoId>>"); |
69 | | - |
70 | | - if v == "v3" { |
71 | | - // Ensure ChangeKind enum is defined before Change struct |
72 | | - let mut lines_all: Vec<&str> = normalized.lines().collect(); |
73 | | - |
74 | | - fn extract_block<'a>( |
75 | | - lines: &mut Vec<&'a str>, |
76 | | - start_pred: &str, |
77 | | - ) -> Option<Vec<&'a str>> { |
78 | | - let start = lines |
79 | | - .iter() |
80 | | - .position(|l| l.trim_start().starts_with(start_pred))?; |
81 | | - let mut end = start; |
82 | | - let mut brace_count = 0i32; |
83 | | - let mut seen_open = false; |
84 | | - for i in start..lines.len() { |
85 | | - let l = lines[i]; |
86 | | - if l.contains('{') { |
87 | | - brace_count += 1; |
88 | | - seen_open = true; |
89 | | - } |
90 | | - if l.contains('}') { |
91 | | - brace_count -= 1; |
92 | | - } |
93 | | - end = i; |
94 | | - if seen_open && brace_count == 0 { |
95 | | - break; |
96 | | - } |
97 | | - } |
98 | | - let block: Vec<&str> = lines[start..=end].to_vec(); |
99 | | - lines.drain(start..=end); |
100 | | - Some(block) |
101 | | - } |
102 | | - |
103 | | - let change_block = extract_block(&mut lines_all, "type Change struct"); |
104 | | - let kind_block = extract_block(&mut lines_all, "type ChangeKind enum"); |
105 | | - |
106 | | - if change_block.is_some() && kind_block.is_some() { |
107 | | - let insert_at = lines_all |
108 | | - .iter() |
109 | | - .position(|l| l.trim_start().starts_with("type Todo struct")) |
110 | | - .unwrap_or(lines_all.len()); |
111 | | - let mut rebuilt: Vec<&str> = Vec::new(); |
112 | | - rebuilt.extend_from_slice(&lines_all[..insert_at]); |
113 | | - for l in kind_block.unwrap() { |
114 | | - rebuilt.push(l); |
115 | | - } |
116 | | - for l in change_block.unwrap() { |
117 | | - rebuilt.push(l); |
118 | | - } |
119 | | - rebuilt.extend_from_slice(&lines_all[insert_at..]); |
120 | | - normalized = rebuilt.join("\n"); |
121 | | - } |
122 | | - } |
123 | | - |
124 | | - fs::write(&dst, normalized)?; |
125 | | - } |
126 | | - |
127 | | - // Generate Rust from schemas: write one file per schema + combined_imports.rs |
128 | | - let out_path = &out_dir; |
129 | | - let mut all_names = Vec::new(); |
130 | | - for entry in fs::read_dir(&schema_dir)?.flatten() { |
131 | | - let path = entry.path(); |
132 | | - if path.is_dir() { |
133 | | - continue; |
134 | | - } |
135 | | - let bare_name = path |
136 | | - .file_name() |
137 | | - .and_then(|s| s.to_str()) |
138 | | - .and_then(|s| s.rsplit_once('.')) |
139 | | - .map(|(n, _)| n) |
140 | | - .expect("valid file name"); |
141 | | - |
142 | | - // Use HashMap instead of rivet_util::HashableMap to avoid extra dependency here. |
143 | | - let tokens = vbare_gen::bare_schema( |
144 | | - &path, |
145 | | - vbare_gen::Config { |
146 | | - use_hashable_map: false, |
147 | | - }, |
148 | | - ); |
149 | | - let ast = syn::parse2(tokens).expect("parse generated code"); |
150 | | - let content = prettyplease::unparse(&ast); |
151 | | - fs::write(out_path.join(format!("{bare_name}_generated.rs")), content)?; |
152 | | - all_names.push(bare_name.to_string()); |
153 | | - } |
154 | | - |
155 | | - let mut mod_content = String::from("// Auto-generated module file for schemas\n\n"); |
156 | | - for name in all_names { |
157 | | - mod_content.push_str(&format!( |
158 | | - "pub mod {name} {{\n include!(concat!(env!(\"OUT_DIR\"), \"/{name}_generated.rs\"));\n}}\n" |
159 | | - )); |
160 | | - } |
161 | | - fs::write(out_path.join("combined_imports.rs"), mod_content)?; |
| 1 | +use std::path::Path; |
162 | 2 |
|
| 3 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 4 | + let schemas = Path::new("schemas"); |
| 5 | + vbare_compiler::process_schemas(schemas)?; |
163 | 6 | Ok(()) |
164 | 7 | } |
0 commit comments