(input: TokenStream)
| 18 | } |
| 19 | |
| 20 | fn emit_minified(input: TokenStream) -> TokenStream { |
| 21 | match parse_input(input) |
| 22 | .and_then(|paths| { |
| 23 | let mut resolved_sources = Vec::with_capacity(paths.len()); |
| 24 | for path in paths { |
| 25 | let resolved = resolve_path(&path)?; |
| 26 | let source = fs::read_to_string(&resolved).map_err(|e| { |
| 27 | format!( |
| 28 | "include_str_stripped! failed read `{}`: {e}", |
| 29 | resolved.display() |
| 30 | ) |
| 31 | })?; |
| 32 | resolved_sources.push((resolved, source)); |
| 33 | } |
| 34 | Ok(resolved_sources) |
| 35 | }) |
| 36 | .map(|resolved_sources| { |
| 37 | // Expand to `{ const _: &[u8] = include_bytes!("abs path"); ...; "min" }` |
| 38 | // so cargo tracks the source file and rebuilds on change; a bare |
| 39 | // literal would go stale silently. |
| 40 | let source = resolved_sources |
| 41 | .iter() |
| 42 | .map(|(_, source)| source.as_str()) |
| 43 | .collect::<String>(); |
| 44 | let lit = Literal::string(&perro_wgsl::optimize_source(&source)); |
| 45 | let mut inner = TokenStream::new(); |
| 46 | for (resolved, _) in resolved_sources { |
| 47 | let abs = resolved.to_string_lossy().replace('\\', "/"); |
| 48 | inner.extend([ |
| 49 | TokenTree::Ident(Ident::new("const", Span::call_site())), |
| 50 | TokenTree::Ident(Ident::new("_", Span::call_site())), |
| 51 | TokenTree::Punct(Punct::new(':', Spacing::Alone)), |
| 52 | TokenTree::Punct(Punct::new('&', Spacing::Alone)), |
| 53 | TokenTree::Group(Group::new(Delimiter::Bracket, { |
| 54 | let mut b = TokenStream::new(); |
| 55 | b.extend([TokenTree::Ident(Ident::new("u8", Span::call_site()))]); |
| 56 | b |
| 57 | })), |
| 58 | TokenTree::Punct(Punct::new('=', Spacing::Alone)), |
| 59 | TokenTree::Punct(Punct::new(':', Spacing::Joint)), |
| 60 | TokenTree::Punct(Punct::new(':', Spacing::Alone)), |
| 61 | TokenTree::Ident(Ident::new("core", Span::call_site())), |
| 62 | TokenTree::Punct(Punct::new(':', Spacing::Joint)), |
| 63 | TokenTree::Punct(Punct::new(':', Spacing::Alone)), |
| 64 | TokenTree::Ident(Ident::new("include_bytes", Span::call_site())), |
| 65 | TokenTree::Punct(Punct::new('!', Spacing::Alone)), |
| 66 | TokenTree::Group(Group::new(Delimiter::Parenthesis, { |
| 67 | let mut p = TokenStream::new(); |
| 68 | p.extend([TokenTree::Literal(Literal::string(&abs))]); |
| 69 | p |
| 70 | })), |
| 71 | TokenTree::Punct(Punct::new(';', Spacing::Alone)), |
| 72 | ]); |
| 73 | } |
no test coverage detected