include_folder_shared/lib.rs
1pub use globbing::*;
2use std::fmt::Debug;
3mod globbing;
4
5#[derive(Debug, Clone)]
6pub struct File {
7 pub path: String,
8 pub data: FileData,
9}
10
11#[derive(Debug, Clone)]
12pub enum FileData {
13 Blob(Vec<u8>),
14 Text(String),
15}
16
17impl FileData {
18 pub fn _type(&self) -> String {
19 match self {
20 Self::Blob(_) => "Vec<u8>",
21 Self::Text(_) => "String",
22 }
23 .to_string()
24 }
25}
26
27/// All generated structs implement this.
28pub trait Directory {
29 /// Gives you all the files in a directory or other such struct recursively.
30 ///
31 /// The paths returned from this will use periods as seperators as it looks when you are
32 /// accessing the files as fields on a struct.
33 ///
34 /// A generated implementation may look like this:
35 ///
36 /// ```
37 /// impl ::include_folder::Directory for TestDirSrc {
38 /// fn files(&self) -> Vec<::include_folder::File> {
39 /// use ::include_folder::Data;
40 /// <[_]>::into_vec(
41 /// #[rustc_box]
42 /// ::alloc::boxed::Box::new([
43 /// ::include_folder::File {
44 /// path: "nested.folders.test.txt".to_string(),
45 /// data: self.nested.folders.test.txt.clone().to_file_data(),
46 /// },
47 /// ::include_folder::File {
48 /// path: "parsing.lexer.txt".to_string(),
49 /// data: self.parsing.lexer.txt.clone().to_file_data(),
50 /// },
51 /// ::include_folder::File {
52 /// path: "parsing.lexer.rs".to_string(),
53 /// data: self.parsing.lexer.rs.clone().to_file_data(),
54 /// },
55 /// ::include_folder::File {
56 /// path: "main.rs".to_string(),
57 /// data: self.main.rs.clone().to_file_data(),
58 /// },
59 /// ]),
60 /// )
61 /// }
62 /// }
63 /// ```
64 fn files(&self) -> Vec<File>;
65 fn glob(&self, search: &str) -> Result<Vec<File>, globset::Error> {
66 glob(self.files(), search)
67 }
68}
69
70pub trait Data {
71 fn to_file_data(self) -> FileData;
72}
73
74impl Data for String {
75 fn to_file_data(self) -> FileData {
76 FileData::Text(self)
77 }
78}
79
80impl Data for Vec<u8> {
81 fn to_file_data(self) -> FileData {
82 FileData::Blob(self)
83 }
84}