Files
ansi_term
anyhow
atty
bcs
bincode
bitflags
clap
dtoa
glob
heck
include_dir
include_dir_impl
lazy_static
libc
linked_hash_map
maplit
once_cell
proc_macro2
proc_macro_error
proc_macro_error_attr
proc_macro_hack
quote
serde
serde_bytes
serde_derive
serde_generate
serde_name
serde_reflection
serde_yaml
serdegen
smawk
strsim
structopt
structopt_derive
syn
textwrap
thiserror
thiserror_impl
unicode_segmentation
unicode_width
unicode_xid
vec_map
yaml_rust
 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);
    }
}