(
source: &dyn ShaderSource,
current_path: &str,
referrer: &str,
out: &mut String,
seen: &mut HashSet<String>,
stack: &mut Vec<String>,
)
| 72 | } |
| 73 | |
| 74 | fn expand( |
| 75 | source: &dyn ShaderSource, |
| 76 | current_path: &str, |
| 77 | referrer: &str, |
| 78 | out: &mut String, |
| 79 | seen: &mut HashSet<String>, |
| 80 | stack: &mut Vec<String>, |
| 81 | ) -> Result<(), IncludeError> { |
| 82 | if seen.contains(current_path) { |
| 83 | // Already included — emit nothing. This is the include-guard |
| 84 | // mechanism; headers are idempotent. |
| 85 | return Ok(()); |
| 86 | } |
| 87 | if stack.iter().any(|p| p == current_path) { |
| 88 | return Err(IncludeError::CircularInclude { |
| 89 | path: current_path.to_string(), |
| 90 | }); |
| 91 | } |
| 92 | seen.insert(current_path.to_string()); |
| 93 | stack.push(current_path.to_string()); |
| 94 | |
| 95 | let body = source.fetch(current_path).ok_or_else(|| IncludeError::Missing { |
| 96 | referrer: referrer.to_string(), |
| 97 | included: current_path.to_string(), |
| 98 | })?; |
| 99 | |
| 100 | for (line_idx, line) in body.lines().enumerate() { |
| 101 | let trimmed = line.trim_start(); |
| 102 | if let Some(rest) = trimmed.strip_prefix("#include") { |
| 103 | let include_path = parse_include_arg(rest).ok_or_else(|| { |
| 104 | IncludeError::MalformedDirective { |
| 105 | referrer: current_path.to_string(), |
| 106 | line: format!("line {}: {}", line_idx + 1, line), |
| 107 | } |
| 108 | })?; |
| 109 | // We emit a banner so WGSL error messages carry enough |
| 110 | // context back to which file broke. |
| 111 | out.push_str(&format!("// --- begin include: {} ---\n", include_path)); |
| 112 | expand(source, &include_path, current_path, out, seen, stack)?; |
| 113 | out.push_str(&format!("// --- end include: {} ---\n", include_path)); |
| 114 | } else { |
| 115 | out.push_str(line); |
| 116 | out.push('\n'); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | stack.pop(); |
| 121 | Ok(()) |
| 122 | } |
| 123 | |
| 124 | fn parse_include_arg(rest: &str) -> Option<String> { |
| 125 | let rest = rest.trim(); |
no test coverage detected