1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
use anyhow::Error; use proc_macro2::TokenStream; use quote::{quote, ToTokens}; use std::path::{Path, PathBuf}; #[derive(Debug, Clone, PartialEq)] pub(crate) struct File { root_rel_path: PathBuf, abs_path: PathBuf, } impl File { pub fn from_disk<Q: AsRef<Path>, P: Into<PathBuf>>(root: Q, path: P) -> Result<File, Error> { let abs_path = path.into(); let root = root.as_ref(); let root_rel_path = abs_path.strip_prefix(&root).unwrap().to_path_buf(); Ok(File { abs_path, root_rel_path, }) } } impl ToTokens for File { fn to_tokens(&self, tokens: &mut TokenStream) { let root_rel_path = self.root_rel_path.display().to_string(); let abs_path = self.abs_path.display().to_string(); let tok = quote! { $crate::File { path: #root_rel_path, contents: include_bytes!(#abs_path), } }; tok.to_tokens(tokens); } }