MCPcopy Create free account
hub / github.com/Bloom-Engine/engine / expand

Function expand

native/shared/src/renderer/shader_include.rs:74–122  ·  view source on GitHub ↗
(
    source: &dyn ShaderSource,
    current_path: &str,
    referrer: &str,
    out: &mut String,
    seen: &mut HashSet<String>,
    stack: &mut Vec<String>,
)

Source from the content-addressed store, hash-verified

72}
73
74fn 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
124fn parse_include_arg(rest: &str) -> Option<String> {
125 let rest = rest.trim();

Callers 1

processFunction · 0.85

Calls 4

parse_include_argFunction · 0.85
iterMethod · 0.80
fetchMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected