MCPcopy Create free account
hub / github.com/PerroEngine/Perro / emit_minified

Function emit_minified

perro_source/render_stack/perro_macros/src/lib.rs:20–70  ·  view source on GitHub ↗
(input: TokenStream)

Source from the content-addressed store, hash-verified

18}
19
20fn 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 }

Callers 3

include_str_strippedFunction · 0.85
include_min_strFunction · 0.85
minified_wgslFunction · 0.85

Calls 5

parse_inputFunction · 0.85
minify_textFunction · 0.85
compile_error_tokensFunction · 0.85
extendMethod · 0.80
resolve_pathFunction · 0.70

Tested by

no test coverage detected