1use std::path::{Path, PathBuf};
2use std::time::Duration;
3
4#[derive(Clone)]
6pub struct FileSource {
7 pub path: PathBuf,
9
10 pub optional: bool,
13
14 pub reload_on_change: bool,
17
18 pub reload_delay: Duration,
25}
26
27impl FileSource {
28 pub fn new(path: PathBuf, optional: bool, reload_on_change: bool, reload_delay: Option<Duration>) -> Self {
37 Self {
38 path,
39 optional,
40 reload_on_change,
41 reload_delay: reload_delay.unwrap_or(Duration::from_millis(250)),
42 }
43 }
44
45 pub fn optional<P: AsRef<Path>>(path: P) -> Self {
51 Self::new(path.as_ref().to_path_buf(), true, false, None)
52 }
53}
54
55impl From<PathBuf> for FileSource {
56 fn from(value: PathBuf) -> Self {
57 Self::new(value, false, false, None)
58 }
59}
60
61impl From<&PathBuf> for FileSource {
62 fn from(value: &PathBuf) -> Self {
63 Self::from(value.clone())
64 }
65}
66
67impl From<&Path> for FileSource {
68 fn from(value: &Path) -> Self {
69 Self::from(value.to_path_buf())
70 }
71}
72
73impl From<&str> for FileSource {
74 fn from(value: &str) -> Self {
75 Self::from(PathBuf::from(value))
76 }
77}
78
79impl From<String> for FileSource {
80 fn from(value: String) -> Self {
81 Self::from(PathBuf::from(value))
82 }
83}
84
85impl From<&String> for FileSource {
86 fn from(value: &String) -> Self {
87 Self::from(PathBuf::from(value))
88 }
89}
90
91pub struct FileSourceBuilder {
93 path: PathBuf,
94 optional: bool,
95 reload_on_change: bool,
96 reload_delay: Option<Duration>,
97}
98
99impl FileSourceBuilder {
100 pub fn new(path: PathBuf) -> Self {
106 Self {
107 path,
108 optional: false,
109 reload_on_change: false,
110 reload_delay: None,
111 }
112 }
113
114 pub fn optional(mut self) -> Self {
116 self.optional = true;
117 self
118 }
119
120 pub fn reloadable(mut self) -> Self {
122 self.reload_on_change = true;
123 self
124 }
125
126 pub fn reload_delay(mut self, delay: Duration) -> Self {
128 self.reload_delay = Some(delay);
129 self
130 }
131
132 pub fn build(&self) -> FileSource {
134 FileSource::new(
135 self.path.clone(),
136 self.optional,
137 self.reload_on_change,
138 self.reload_delay,
139 )
140 }
141}
142
143impl From<FileSourceBuilder> for FileSource {
144 fn from(value: FileSourceBuilder) -> Self {
145 value.build()
146 }
147}
148
149impl From<&FileSourceBuilder> for FileSource {
150 fn from(value: &FileSourceBuilder) -> Self {
151 value.build()
152 }
153}
154
155pub mod ext {
156
157 use super::*;
158
159 pub trait FileSourceBuilderExtensions {
161 fn is(&self) -> FileSourceBuilder;
162 }
163
164 impl<T: AsRef<Path>> FileSourceBuilderExtensions for T {
165 fn is(&self) -> FileSourceBuilder {
166 FileSourceBuilder::new(self.as_ref().to_path_buf())
167 }
168 }
169}