Skip to main content

config/
file.rs

1use std::path::{Path, PathBuf};
2
3/// Represents a file configuration source.
4#[derive(Clone, Default)]
5pub struct FileSource {
6    /// Gets or sets the source file path.
7    pub path: PathBuf,
8
9    /// Gets or sets a value indicating whether the file is optional.
10    ///
11    /// # Remarks
12    ///
13    /// The default value is false.
14    pub optional: bool,
15
16    /// Gets or sets a value indicating whether the file will be loaded if the underlying file changes.
17    ///
18    /// # Remarks
19    ///
20    /// The default value is false.
21    pub reload_on_change: bool,
22}
23
24impl FileSource {
25    /// Initializes a new [FileSource].
26    ///
27    /// # Arguments
28    ///
29    /// * `path` - The source file path
30    /// * `optional` - Indicates whether the source file must exist
31    /// * `reload_on_change` - Indicates if a reload should occur if the source file changes
32    #[inline]
33    pub fn new(path: PathBuf, optional: bool, reload_on_change: bool) -> Self {
34        Self {
35            path,
36            optional,
37            reload_on_change,
38        }
39    }
40
41    /// Initializes a new, optional file configuration source.
42    ///
43    /// # Arguments
44    ///
45    /// * `path` - The source file path
46    #[inline]
47    pub fn optional<P: AsRef<Path>>(path: P) -> Self {
48        Self::new(path.as_ref().to_path_buf(), true, false)
49    }
50}
51
52impl From<PathBuf> for FileSource {
53    #[inline]
54    fn from(value: PathBuf) -> Self {
55        Self::new(value, false, false)
56    }
57}
58
59impl From<&PathBuf> for FileSource {
60    #[inline]
61    fn from(value: &PathBuf) -> Self {
62        Self::from(value.clone())
63    }
64}
65
66impl From<&Path> for FileSource {
67    #[inline]
68    fn from(value: &Path) -> Self {
69        Self::from(value.to_path_buf())
70    }
71}
72
73impl From<&str> for FileSource {
74    #[inline]
75    fn from(value: &str) -> Self {
76        Self::from(PathBuf::from(value))
77    }
78}
79
80impl From<String> for FileSource {
81    #[inline]
82    fn from(value: String) -> Self {
83        Self::from(PathBuf::from(value))
84    }
85}
86
87impl From<&String> for FileSource {
88    #[inline]
89    fn from(value: &String) -> Self {
90        Self::from(PathBuf::from(value))
91    }
92}
93
94/// Represents a builder for a [file source](FileSource).
95pub struct FileSourceBuilder {
96    path: PathBuf,
97    optional: bool,
98    reload_on_change: bool,
99}
100
101impl FileSourceBuilder {
102    /// Initializes a new file source builder.
103    ///
104    /// # Arguments
105    ///
106    /// * `path` - The path to build a file source for
107    #[inline]
108    pub fn new(path: PathBuf) -> Self {
109        Self {
110            path,
111            optional: false,
112            reload_on_change: false,
113        }
114    }
115
116    /// Indicates the file source is optional.
117    pub fn optional(mut self) -> Self {
118        self.optional = true;
119        self
120    }
121
122    /// Indicates the file source can be reloaded.
123    pub fn reloadable(mut self) -> Self {
124        self.reload_on_change = true;
125        self
126    }
127
128    /// Creates and returns a new [file source](FileSource).
129    #[inline]
130    pub fn build(&self) -> FileSource {
131        FileSource::new(self.path.clone(), self.optional, self.reload_on_change)
132    }
133}
134
135impl From<FileSourceBuilder> for FileSource {
136    #[inline]
137    fn from(value: FileSourceBuilder) -> Self {
138        value.build()
139    }
140}
141
142impl From<&FileSourceBuilder> for FileSource {
143    #[inline]
144    fn from(value: &FileSourceBuilder) -> Self {
145        value.build()
146    }
147}