3 ms·
I actually already use the include_bytes! macro within the build.rs. Looking at the source I see that I create arrays of arrays of bytes, not just arrays of byt
by codetrotter 9y ago
I actually already use the include_bytes! macro within the build.rs. Looking at the source I see that I create arrays of arrays of bytes, not just arrays of bytes as opposed to what I originally said. It was a few months since I wrote that code so I hope I would be forgiven for not remembering such details ;)
My eventual plan that I remember now was to make it so that I could reference static files (CSS, JS, icons and images, fonts) in my static HTML files and HTML templates, and build.rs would automatically include the bytes of those files.
- codetrotter 9y agoI have now rewritten my code so that I include bytes directly in my src/main.rs like so: #[get("/fonts/<fname>")] fn fonts<'a> (fname: String) -> Result<Content<Vec<u8>>, NotFound<String>> { match fname.as_ref() { "autobahn.woff" => Ok(Content(ContentType::WOFF, include_bytes!("../static/fonts/Autobahn/autobahn.woff").to_vec())), "autobahn.ttf" => Ok(Content(ContentType::TTF, include_bytes!("../static/fonts/Autobahn/autobahn.ttf").to_vec())), "grobe-deutschmeister.woff" => Ok(Content(ContentType::WOFF, include_bytes!("../static/fonts/Grobe-Deutschmeister/grobe-deutschmeister.woff").to_vec())), "grobe-deutschmeister.ttf" => Ok(Content(ContentType::TTF, include_bytes!("../static/fonts/Grobe-Deutschmeister/grobe-deutschmeister.ttf").to_vec())), _ => Err(NotFound(format!("No such file: /fonts/{}", fname))) } } In the meantime since I first wrote my original code, the web framework I was using called Iron [1] has become unmaintained and they now urge people to pick a different framework. I chose to use Rocket because it seemed to have the features I want and the documentation seemed good enough to get started (and it was). For templating I found Askama [2], which I am using from git master in order to be able to use it together with Rocket. https://github.com/djc/askama/issues/71 https://github.com/djc/askama/issues/71 Eventually I will generate the routes for the included bytes with build.rs again. My new code is better both short-term and for when I will create said build.rs. Thank you masklinn for your comment about include_bytes! which made me look at my code again and rewrite it. [1]: https://rocket.rs/ https://rocket.rs/ [2]: https://docs.rs/crate/askama/ https://docs.rs/crate/askama/